Build Status

Dependency Injection (DI) framework

Installation

Add dependency to your pubspec.yaml.

dependencies:
  di: ">=3.3.3 <4.0.0"

Then, run pub install.

Import di.

import 'package:di/di.dart';

Example

import 'package:di/di.dart';

abstract class Engine {
  go();
}

class Fuel {}

class V8Engine implements Engine {
  Fuel fuel;
  V8Engine(this.fuel);

  go() {
    print('Vroom...');
  }
}

class ElectricEngine implements Engine {
  go() {
    print('Hum...');
  }
}

// Annotation
class Electric {
  const Electric();
}

class GenericCar {
  Engine engine;

  GenericCar(this.engine);

  drive() {
    engine.go();
  }
}

class ElectricCar {
  Engine engine;

  ElectricCar(@Electric() this.engine);

  drive() {
    engine.go();
  }
}

void main() {
  var injector = new ModuleInjector([new Module()
      ..bind(GenericCar)
      ..bind(ElectricCar)
      ..bind(Engine, toFactory: (fuel) => new V8Engine(fuel), inject: [Fuel])
      ..bind(Engine, toImplementation: ElectricEngine, withAnnotation: Electric)
  ]);
  injector.get(GenericCar).drive(); // Vroom...
  injector.get(ElectricCar).drive(); // Hum...
}

Contributing

Refer to the guidelines for contributing to AngularDart.

Libraries

di
di.annotations
di.check_bind_args
di.dynamic_injector
di.generator

Generates Factory & paramKeys maps into a file by crawling source files and finding all constructors that are annotated for injection. Does the same thing as transformer.dart for when transformers are not available, except without modifications to the main function and DI. As such, the user needs to import the generated file, and run Module.DEFAULT_REFLECTOR = new GeneratedTypeFactories(typeFactories, parameterKeys) imported, before any modules are initialized. Import 'di_static.dart' instead of 'di.dart' to avoid mirrors, and import 'reflector_static.dart' for GeneratedTypeFactories. See transformer.dart for more info.

di.key
di.transformer

Static injection transformer which generates, for each injectable type:

di.transformer.injector_generator
di.transformer.module_transformer
di.transformer.options
di.type_literal