Build Status

Dice

Lightweight dependency injection framework for Dart.

Injection Types

Dice supports the following injection forms

  • Fields starting with $ and _$ are injected
  • Setters starting $ are injected
  • Variables starting with $ and _$ are injected

Injection of parameters is unsupported as Dart's mirror system cannot handle these yet.

Quick Guide

  1. Add the folowing to your pubspec.yaml and run pub install
    dependencies:
      dice: any
  1. Create some classes and interfaces to inject
	class BillingServiceImpl implements BillingService {
	  // fields starting with $ and _$ gets injected
	  CreditCardProcessor _$processor;
	  
	  Receipt chargeOrder(Order order, CreditCard creditCard) {
	    if(!_$processor.validate(creditCard)) {
	      throw new ArgumentError("payment method not accepted");
	    }
	    // :
	  }
	}
  1. Register the type/class bindings in a module
	class ExampleModule extends Module {
	  configure() {
	    // bind CreditCardProcessor to a singleton
	    bind(CreditCardProcessor).toInstance(new CreditCardProcessorImpl());
	    // bind BillingService to a prototype
	    bind(BillingService).toType(new BillingServiceImpl());
	  }
	}
  1. Run it
    import "package:dice/dice.dart";
    main() {
	  var injector = new Injector(new ExampleModule());
	  injector.getInstance(BillingService).then((BillingService 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.

Libraries

dice

Lightweight dependency injection framework for Dart.