di 2.0.0-alpha.6 copy "di: ^2.0.0-alpha.6" to clipboard
di: ^2.0.0-alpha.6 copied to clipboard

discontinued
outdated

Dependency Injection framework

Build Status

Dependency Injection (DI) framework #

Installation #

Add dependency to your pubspec.yaml.

dependencies:
  di: ">=2.0.0 <3.0.0"

Then, run pub install.

Import di.

import 'package:di/di.dart';

Example #

import 'package:di/di.dart';
import 'package:di/di_dynamic.dart';

abstract class Engine {
  go();
}

class V8Engine implements Engine {
  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() {
  setupModuleTypeReflector();
  var injector = new ModuleInjector(modules: [new Module()
      ..bind(GenericCar)
      ..bind(ElectricCar)
      ..bind(Engine, toFactory: () => new V8Engine())
      ..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.

0
likes
0
pub points
45%
popularity

Publisher

unverified uploader

Dependency Injection framework

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, barback, code_transformers, path

More

Packages that depend on di