dime 0.3.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 73

Dime is Dart Dependency "injection framework", it is more like factory and static lookup but with nice wrap.

Dime allows to create modules that define injection types and their InjectFactory implementations. It can easily base on interfaces which allows to pick different implementations. Supports for tag tag based same type instances. Support for multiple modules and scopes with Closable interface to cleanup resources.

Get it from pub page: Pub dime page

Note: All examples below are from example file file. Go there for high level view.

Usage #

A simple usage example:

import 'package:dime/dime.dart';

void main() {
  /// Service Module does include details how to create the objects.   
  dimeInstall(ServiceModule());
     
  MyTitleService titleService = dimeGet();
  // or 
  var titleService = dimeGet<MyTitleService>();
  print(titleService.text());
  
}


Setup #

Add package dependency to pubspec.yaml: #

 depedency: 
   ...
   dime: ^0.3.4
   ...

Define module: #

Create a module and how it creates its dependencies:

class MyModule  extends BaseAppgetorModule {
    @override
    void updateInjections() {
        /// define injection factories - below for examples      
    }
}

Below are examples that can be used inside updateinjections method.

Singleton per type #

get single value by its class type:

  addSingle(MyTitleService());

get singleton value by implementing interface:

  addSingle<TitleService>(MyTitleService());

Singletons per type with tag #

get single value by its class type:

  addSingle(MyTitleService(), tag: "home-title");
  addSingle(MyTitleService(), tag: "details-title");

get singleton value by implementing interface:

  addSingle<TitleService>(MyTitleService(), tag: "home-title");

Creator on-demand injection, it uses type of Creator #

This is creator - which will create an object at time of injection.

typedef Creator<T> = T Function(String tag);

The Creator provides optional String tag that may be used to create the tagged instance.

addCreator<TextService>((tag) =>
        MyTitleService(title: "Test title: $tag: now: ${DateTime.now()}"));

Creator on-demand injection with singleton storage - delayed singleton. #

Similar to above example with addCreator, however created instance will be cached per tag.

addSingleByCreator((tag)=>MyDescriptionService());

Create your own factory. #

You can always create your own factory by extending getFactory<T> and add those to the module.

 addFactory(MyTooltipService, MyCustomFactory());

Note: There are some other Factories already provided to be used - like:

  • getTagFactory for create method with a Tag
  • TaggedSingletongetFactory - for tagged singletons with Closeable interface
  • SinglegetFactory - single get factory with Closable interface

Add modules to the scope (global too) #

You can add modules to the main global scope of Dime or into the opened scopes. When a scope closes all modules in that scope will also close (clean up) by calling each of its Closeable factory close() method to cleanup resources.

Add Module to Global Dime scope. #

  dimeInstall(ServiceModule());

Dime Scope fetch up the tree examples: #

Test class to be referenced by main scope_test.dart tests.

Modules: #

ModuleInstance
ModuleAAA()
AB()
AC()
ModuleBBA()
BB()
BC()
ModuleCCA()
CB()
CC()
ModuleXXAA()
BB()
CC()

Scope graph: #

Root ScopeModules/ScopesModules/ScopesModules/Scopes
Dime- ModuleC
- ModuleXX (override)
- scope1- ModuleA
- ModuleB
- scope2- ModuleA
- ModuleC
- scope 21- ModuleC
- scope 22- ModuleA
- ModuleB

Example injections: #

scope2.inject

Dime.inject

scope1.inject

scope1.inject

scope2.inject

scope2.inject

Dime.inject

Dime.inject

Dime.inject

scope21.inject

scope21.inject

scope22.inject

scope22.inject

scope21.inject

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.3.4 - Fix for Single by tag injection #

  • Bug fix for single by tag module injection.

0.3.3 - Upgrade Fimber version #

  • Upgrades Fimber version to not depend on dart:io

0.3.1 - Dime scopes updates #

  • added scopes usages
  • minor updates for dependency

0.3.0 - Dime static change #

  • refactored to use static methods as dime* not Dime. static class
  • Dime. still available via deprecated API notation
  • changed name from inject to get to better match name to function of the method
  • inject still available via deprecated API notation
  • Code styles updates based on effective-dart lint rules.
  • Adding global scope property - to be used with Dime Flutter package (in development)

0.2.0 - Bug fix #

  • added same module getion visible to next getions

0.1.1 - Scopes updates #

  • allow override module's instances for a types.
  • Unit tests update

0.1.0 - initial release #

  • Dependency getion with global scope.
  • get with modules and sub-scopes's modules.
  • Support for modules and scopes.
  • Creator types, on demand getion
  • Ability to add singletons and singleton of same type per tag.
  • Closable types will be properly disposed when closing scopes

example/dime_example.dart

import 'package:dime/dime.dart';
import 'package:fimber/fimber.dart';

import '../test/common.dart';

/// Example app showing Dime installation and usage
void main() {
  Fimber.plantTree(DebugTree.elapsed());
  Fimber.i("Started app");
  dimeInstall(ServiceModule());
  Fimber.i("Installed module");
  var scope = dimeOpenScope("test");
  scope.installModule(ScopeModule());

  // ignore: omit_local_variable_types
  MyTitleService titleService = dimeGet();
  print(titleService.text());

  // ignore: omit_local_variable_types
  MyTitleService titleService2 = dimeGetWithTag("Test tag");
  print(titleService2.text());

  var creatorService = dimeGet<TextService>();
  print(creatorService.text());

  creatorService = dimeGet<TextService>(tag: "TEST TAG A");
  print(creatorService.text());

  var scopeTitle = dimeGet<MyTitleService>();
  print(scopeTitle.text());

  scopeTitle = dimeGet<MyTitleService>();
  print(scopeTitle.text());

  var tooltip = dimeGet<MyTooltipService>();
  print(tooltip.text());

  var scopeDescription = scope.get<MyDescriptionService>();
  print(scopeDescription.text());

  dimeCloseScope(scope: scope);

  try {
    scopeDescription = scope.get<MyDescriptionService>();
    print(scopeDescription.text());
  } on DimeException catch (e, t) {
    // expected thrown exception
    print("Expected exception: $e,\n$t");
  }
}

class ScopeModule extends BaseDimeModule {
  @override
  void updateInjections() {
    addSingle(MyTitleService(title: "test scope service"));
  }
}

class ServiceModule extends BaseDimeModule {
  @override
  void updateInjections() {
    addSingle(MyTitleService());
    addSingle(MyTitleService(title: "second title"), tag: "Test tag");
    addSingleByCreator((tag) => MyDescriptionService());
    addCreator<TextService>((tag) =>
        MyTitleService(title: "Test title: $tag: now- ${DateTime.now()}"));
    addFactory(MyTooltipService, MyCustomFactory());
  }
}

class MyCustomFactory extends InjectFactory<TextService> {
  @override
  TextService create() {
    return MyTooltipService(tooltip: "My custom factory tooltip");
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dime: ^0.3.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dime/dime.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jan 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
fimber ^0.4.0 0.4.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0