This repository has been archived by the owner. It is now read-only.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
benchmark
bin
example
lib
scripts
test
test_assets
.analysis_options
.gitignore
.travis.yml
CHANGELOG.md
Gruntfile.coffee
LICENSE
README.md
TODO
package.json
pubspec.lock
pubspec.yaml
run-benchmarks.sh
run-tests.sh
test_tf_gen.sh

README.md

Build Status

Dependency Injection (DI) framework

Note: This package does not work with Flutter (because this package uses dart:mirrors, which Flutter does not support).

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.