Dependency Injection framework for Dart.
Dart JavaScript Shell Other
Latest commit 073f473 Apr 27, 2016 @hterkelsen hterkelsen committed with rkirov Return parent getter to Injector API
Permalink
Failed to load latest commit information.
benchmark Cleanup errors, warnings and hints from the analyzer and from strong … Apr 26, 2016
bin Cleanup errors, warnings and hints from the analyzer and from strong … Apr 26, 2016
example Return parent getter to Injector API Apr 28, 2016
lib Return parent getter to Injector API Apr 28, 2016
scripts Cleanup errors, warnings and hints from the analyzer and from strong … Apr 26, 2016
test chore: move to analyzer 0.27.1, guinness2 Jan 19, 2016
test_assets chore(deferred_libs): support spec'ed syntax for deferred libraries. Sep 11, 2014
.analysis_options Cleanup errors, warnings and hints from the analyzer and from strong … Apr 26, 2016
.gitignore chore(tests): fix travis runs. Apr 28, 2016
.travis.yml chore(travis): initial travis setup Feb 5, 2014
CHANGELOG.md chore(release): release 3.3.7 Feb 1, 2016
Gruntfile.coffee Initial super lame version Apr 9, 2013
LICENSE refactor: Simpler, Faster injector Jul 23, 2014
README.md docs(README): fix to Engine example Mar 17, 2015
TODO chore(TODO): cleaning up TODO items Dec 20, 2013
package.json chore(changelog): added changelog and script to generate it Apr 16, 2014
pubspec.lock chore(tests): fix travis runs. Apr 28, 2016
pubspec.yaml Return parent getter to Injector API Apr 28, 2016
run-benchmarks.sh fix(benchmarks): Get dart2js'ed benchmarks running on node Oct 30, 2014
run-tests.sh chore(tests): fix travis runs. Apr 28, 2016
test_tf_gen.sh refactor: Simpler, Faster injector Jul 23, 2014

README.md

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.