dice 1.8.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 84

Build Status Coverage 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

dependencies:
   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
    register(BillingService).toType(BillingServiceImpl);
  }
}

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 {
  @inject
  SomeClass field;

  @inject
  SomeOtherClass _privateField;
}
  • Injection of constructor parameters
class MyClass {
  @inject
  MyClass(this.field);

  MyOtherClass field;
}
  • Injection of public and private setters
class SomeClass {
  @inject
  set value(SomeOtherClass val) => _privateValue = val;

  @inject
  set _value(SomeOtherClass val) => _anotherPrivateValue = val;

  SomeOtherClass _privateValue, _anotherPrivateValue;
}

The injected objects are configured ether by extending the Module class and using one its register functions or directly on the Injector.

  • register type MyType to existing object (singleton injections)
register(MyType).toInstance(object)
  • register type MyType.
register(MyType)
  • register interface MyType to a class implementing it.
register(MyType).toType(MyTypeImpl)
  • register a typedef to a function matching it.
register(MyTypedef).toFunction(function)
  • register MyType to function that can build instances of it
register(MyType).toBuilder(() => new MyType())

Named Injections

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

class MyClass {
  @inject
  @Named('my-special-implementation')
  SomeClass _someClass;
}

The configuration is as before except you now provide an additional name parameter.

register(MyType, "my-name").toType(MyTypeImpl)

Advanced Features

  • Get instances directly Instead of using the @inject for injections you can use the injectors getInstance method.
MyClass instance = injector.getInstance(MyClass);
  • Get named instances directly Instead of using the @Named for named injections you can use the injectors getInstance method with its name parameter.
MyType instance = injector.getInstance(MyType, "my-name");
  • Injecting configuration values You can use named registrations to inject configuration values into your application.
class TestModule extends Module {
  configure() {
		register(String, "web-service-host").toInstace("http://test-service.name");
	}
}

// application code
String get webServiceHost => injector.getInstance(String, "web-service-host");
  • Registering dependencies at runtime You can register dependencies at runtime directly on the Injector.
 injector.register(User).toInstance(user);
 var user = injector.getInstance(User);
  • Unregistering dependencies at runtime You can unregister dependencies at runtime using the unregister method on the Injector.
injector.unregister(User);
  • Using multiple modules You can compose modules using the Injector.fromModules constructor.
class MyModule extends Module {
  	configure() {
		register(MyClass).toType(MyClass);
	}
}

class YourModule extends Module {
  	configure() {
		register(YourClass).toType(YourClass);
	}
}

var injector = new Injector.fromModules([new MyModule(), new YourModule()]);
var myClass = injector.getInstance(MyClass);
var yourClass = injector.getInstance(YourClass);
  • Joining injectors You can join multiple injector instances to one using the Injector.fromInjectors constructor.
var myInjector = new Injector();
myInjector.register(MyClass).toType(MyClass);

var yourInjector = new Injector();
yourInjector.register(YourClass).toType(YourClass);

var injector = new Injector.fromInjectors([myInjector, yourInjector]);
var myClass = injector.getInstance(MyClass);
var yourClass = injector.getInstance(YourClass);

1.8.0

  • Support Injectable

1.7.0

  • Singleton binding added, Module-installation added
  • Use type annotations
  • Make Named annotations work
  • Add bind method to support

1.6.5

  • Remove dependency on meta package

1.6.4

  • Add more tests of named injections

1.6.3

  • Bump versions and improve travis build
  • Update copyright

1.6.2

  • Added change log

1.6.1

  • Bumped dependencies
  • Switched to Travis build server
  • Run code coverage on test

1.6.0

  • Function injection now works

1.5.4

  • Bump version as pub.dartlang.com failed to update last version

1.5.3

  • Simplified binding

1.5.2

  • More tests

1.5.1

  • Updated documentation with new module system

1.5.0

  • New alternative to modules when registering dependencies

1.4.6

  • Document how inject configuration values

1. Depend on it

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


dependencies:
  dice: "^1.8.0"

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
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
1.5.3 May 23, 2015 Go to the documentation of dice 1.5.3 Download dice 1.5.3 archive

All 31 versions...

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
93 / 100
Health:
Code health derived from static analysis. [more]
60 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
98 / 100
Overall score:
Weighted score of the above. [more]
84
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:dice/dice.dart with components: mirrors.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 4 errors 8 hints.

    Strong-mode analysis of lib/src/injector.dart failed with the following error:

    line: 185 col: 17
    The function expression type '(MethodMirror, MethodMirror) → MethodMirror' isn't of type '(MethodMirror, DeclarationMirror) → MethodMirror'. This means its parameter or return type does not match what is expected. Consider changing parameter type(s) or the returned type(s).

    Strong-mode analysis of lib/src/mirror_util.dart failed with the following error:

    line: 30 col: 5
    Invalid override. The type of 'TypeMirrorWrapper.==' ('(TypeMirrorWrapper) → bool') isn't a subtype of 'Object.==' ('(dynamic) → bool').

    Similar analysis of the following files failed:

    • lib/dice.dart (hint)
    • lib/src/Registration.dart (hint)
    • lib/src/annotations.dart (hint)
    • lib/src/assert_messages.dart (hint)
    • lib/src/module.dart (hint)
  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dice.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.9.0 <2.0.0
logging ^0.11.0 0.11.3+1
Dev dependencies
browser ^0.10.0
console_log_handler ^0.2.0
logging_handlers ^0.8.0
test any