dice 1.5.4

  • README.md
  • Installing
  • Versions
  • --

Build Status

Dice #

Lightweight dependency injection framework for Dart.

Getting Started #

Dice consists of two parts.

  • Modules containing your class registrations.
  • Injectors that uses the Module to inject instances into your code.

The following example should get you startd:

1. Add the Dice 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 types and classes in a module

	class ExampleModule extends Module {
	  configure() {
	    // register [CreditProcessor] as a singleton
	    register(CreditProcessor).toInstance(new CreditProcessorImpl());
	    // register [BillingService] so a new version is created each time its requested

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 full example here.

Dependency Injection with Dice #

You can use the @inject annotation to mark objects and functions for injection the following ways:

  • Injection of public and private fields (object/instance variables)
	class MyOtherClass {
      	SomeClass field;
      	SomeOtherClass _privateField;
  • Injection of constructor parameters
	class MyClass {
 		MyOtherClass field;
  • Injection of public and private setters
	class SomeClass {
      	set value(SomeOtherClass val) => _privateValue = val;
      	set _value(SomeOtherClass val) => _anotherPrivateValue = val;

      	SomeOtherClass _privateValue, _anotherPrivateValue;

The injected objects are configured by extending the Module class and using one its register functions

Named Injections #

Dice supports named injections by using the @Named annotation. Currently this annotation works everywhere the @inject annotation works, except for constructors.

	class MyClass {
      	SomeClass _someClass;

The configuration is as before except you now use method namedRegister in your Module implementation.

Advanced Features #

  • Get instances directly Instead of using the @inject annotation to resolve injections you can use the injectors getInstance method
   MyClass instance = injector.getInstance(MyClass);
  • Get named instances directly Instead of using the @Named annotation to resolve named injections you can use the injectors getNamedInstance method
   MyType instance = injector.getNamedInstance(MyType, "my-name");
  • To register and resole configuration values You can use named registrations to inject configuration values into your application.
	class TestModule extends Module {
    	configure() {
			namedRegister(String, "web-service-host").toInstace("http://test-service.name");
	// application code
	String get webServiceHost => injector.getNamedInstance(String, "web-service-host");
  • Registrering dependencies at runtime You can register dependencies at runtime by accessing the module property on the Injector instance.
	 var user = injector.getInstance(User);
  • Using multiple modules You can compose mudules using the Injector.fromModules constructor
	class MyModule extends Module {
    	configure() {
	class YourModule extends Module {
    	configure() {
	var injector = new Injector.fromModules(new MyModule(), new YourModule());
	var myClass = injector.getInstance(MyClass);
	var yourClass = injector.getInstance(YourClass);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  dice: ^1.5.4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dice/dice.dart';
Version Uploaded Documentation Archive
2.0.0 Jun 28, 2018 Go to the documentation of dice 2.0.0 Download dice 2.0.0 archive
1.8.0 Mar 17, 2018 Go to the documentation of dice 1.8.0 Download dice 1.8.0 archive
1.7.0 May 31, 2017 Go to the documentation of dice 1.7.0 Download dice 1.7.0 archive
1.6.5 Feb 1, 2016 Go to the documentation of dice 1.6.5 Download dice 1.6.5 archive
1.6.4 Feb 1, 2016 Go to the documentation of dice 1.6.4 Download dice 1.6.4 archive
1.6.3 Feb 1, 2016 Go to the documentation of dice 1.6.3 Download dice 1.6.3 archive
1.6.2 Nov 10, 2015 Go to the documentation of dice 1.6.2 Download dice 1.6.2 archive
1.6.1 Aug 19, 2015 Go to the documentation of dice 1.6.1 Download dice 1.6.1 archive
1.6.0 Jun 6, 2015 Go to the documentation of dice 1.6.0 Download dice 1.6.0 archive
1.5.4 Jun 2, 2015 Go to the documentation of dice 1.5.4 Download dice 1.5.4 archive

All 32 versions...

Awaiting analysis to complete.