Dart DocumentationdiModule

Module class

A collection of type bindings. Once the module is passed into the injector, the injector creates a copy of the module and all subsequent changes to the module have no effect.

class Module {
 final Map<Type, _Provider> _providers = <Type, _Provider>{};
 final List<Module> _childModules = <Module>[];

 Map<Type, _Provider> _providersCache;

 /**
  * Compiles and returs bindings map by performing depth-first traversal of the
  * child (installed) modules.
  */
 Map<Type, _Provider> get _bindings {
   if (_isDirty) {
     _providersCache = <Type, _Provider>{};
     _childModules.forEach((child) => _providersCache.addAll(child._bindings));
     _providersCache.addAll(_providers);
   }
   return _providersCache;
 }

 /**
  * Register binding to a concrete value.
  *
  * The [value] is what actually will be injected.
  */
 void value(Type id, value,
     {CreationStrategy creation, Visibility visibility}) {
   _dirty();
   _providers[id] = new _ValueProvider(value, creation, visibility);
 }

 /**
  * Register binding to a [Type].
  *
  * The [implementedBy] will be instantiated using [new] operator and the
  * resulting instance will be injected. If no type is provided, then it's
  * implied that [id] should be instantiated.
  */
 void type(Type id, {Type implementedBy, CreationStrategy creation,
     Visibility visibility}) {
   _dirty();
   _providers[id] = new _TypeProvider(
       implementedBy == null ? id : implementedBy, creation, visibility);
 }

 /**
  * Register binding to a factory function.abstract
  *
  * The [factoryFn] will be called and all its arguments will get injected.
  * The result of that function is the value that will be injected.
  */
 void factory(Type id, FactoryFn factoryFn,
     {CreationStrategy creation, Visibility visibility}) {
   _dirty();
   _providers[id] = new _FactoryProvider(factoryFn, creation, visibility);
 }

 /**
  * Installs another module into this module. Bindings defined on this module
  * take precidence over the installed module.
  */
 void install(Module module) {
   _childModules.add(module);
   _dirty();
 }

 _dirty() {
   _providersCache = null;
 }

 bool get _isDirty =>
     _providersCache == null || _childModules.any((m) => m._isDirty);
}

Methods

void factory(Type id, FactoryFn factoryFn, {CreationStrategy creation, Visibility visibility}) #

Register binding to a factory function.abstract

The factoryFn will be called and all its arguments will get injected. The result of that function is the value that will be injected.

void factory(Type id, FactoryFn factoryFn,
   {CreationStrategy creation, Visibility visibility}) {
 _dirty();
 _providers[id] = new _FactoryProvider(factoryFn, creation, visibility);
}

void install(Module module) #

Installs another module into this module. Bindings defined on this module take precidence over the installed module.

void install(Module module) {
 _childModules.add(module);
 _dirty();
}

void type(Type id, {Type implementedBy, CreationStrategy creation, Visibility visibility}) #

Register binding to a Type.

The implementedBy will be instantiated using new operator and the resulting instance will be injected. If no type is provided, then it's implied that id should be instantiated.

void type(Type id, {Type implementedBy, CreationStrategy creation,
   Visibility visibility}) {
 _dirty();
 _providers[id] = new _TypeProvider(
     implementedBy == null ? id : implementedBy, creation, visibility);
}

void value(Type id, value, {CreationStrategy creation, Visibility visibility}) #

Register binding to a concrete value.

The value is what actually will be injected.

void value(Type id, value,
   {CreationStrategy creation, Visibility visibility}) {
 _dirty();
 _providers[id] = new _ValueProvider(value, creation, visibility);
}