Dependency Injection framework for Dart.

Clone this repo:
  1. 34649af Adds missing import for TypeLiteral by Winston Ewert · 8 years ago master
  2. 75ecace docs(README): fix to Engine example by Shawn Drape · 8 years ago
  3. 5631f09 chore(pubspec): Widen the analyzer constraint by Tim Blasi · 8 years ago 3.3.4
  4. d1f64bb chore(pubspec): releases 3.3.3 by Rado Kirov · 8 years ago 3.3.3
  5. 9b6a319 feat(pubspec): bumps dependencies to current versions. by Rado Kirov · 8 years ago

Build Status

Dependency Injection (DI) framework

Installation

Add dependency to your pubspec.yaml.

dependencies:
  di: ">=3.3.4 <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(Fuel)
      ..bind(GenericCar)
      ..bind(ElectricCar)
      ..bind(Engine, toFactory: (fuel) => new V8Engine(fuel), inject: [Fuel])
      ..bind(Engine, toImplementation: ElectricEngine, withAnnotation: const Electric())
  ]);
  injector.get(GenericCar).drive(); // Vroom...
  injector.get(ElectricCar).drive(); // Hum...
}

Contributing

Refer to the guidelines for contributing to AngularDart.