Build Status


Lightweight dependency injection framework for Dart.

Injection Types

Dice supports the following injection forms

  • Injection of public and private fields (object/instance variables)
	class MyOtherClass {
      	SomeClass field;
      	SomeOtherClass _privateField;
  • Injection of constructors (if no constructor is annotated with @Inject then the default is used)
	class MyClass {
 		MyOtherClass field;
  • Injection of public setters
	class SomeClass {
      	set value(SomeOtherClass val) => _privateVal = val;

      	SomeOtherClass _privateVal;

Named injections are not supported yet but we are working on fixing this.

Quick Guide

1. Add the folowing to your pubspec.yaml and run pub install

      dice: any

2. Create some classes and interfaces to inject

	class BillingServiceImpl implements BillingService {
	  CreditProcessor _processor;
	  Receipt chargeOrder(Order order, CreditCard creditCard) {
	    if(!_processor.validate(creditCard)) {
	      throw new ArgumentError("payment method not accepted");
	    // :

3. Register the type/class bindings in a module

	class ExampleModule extends Module {
	  configure() {
	    // bind CreditProcessor to a singleton
	    bind(CreditProcessor).toInstance(new CreditProcessorImpl());
	    // bind BillingService to a prototype

4. Run it

    import "package:dice/dice.dart";
	main() {
	  	var injector = new Injector(new ExampleModule());
	  	var billingService = injector.getInstance(BillingService);
	  	var creditCard = new CreditCard("VISA");
	  	var order = new Order("Dart: Up and Running");
	  	billingService.chargeOrder(order, creditCard);

for more information see the example application here.



Lightweight dependency injection framework for Dart.