dryice 2.0.15

  • README.md
  • Installing
  • Versions
  • 71

DryIce (built mirrors / reflectable)

Lightweight dependency injection framework for Dart.

Built mirrors / reflectable

This branch is in a very early stage and works only with Dart 2.x but if you want to try it:

git clone -b reflectable git@github.com:MikeMitterer/dryice.git reflectable
cd reflectable
pub update
dart tool/build.dart test/*_test.dart
pub run test test/dice_test.dart
pub run test test/dice_test.dart -p chrome

Getting Started

DryIce 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 started:

1. Add DryIce to your pubspec.yaml and run pub install

   dryice: any

2. Create some classes and interfaces to inject

abstract class BillingService {
    Receipt chargeOrder(Order order, CreditCard creditCard);

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 [BillingService] so a new version is created each time its requested

4. Run it

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

for more information see the full example here.

Dependency Injection with DryIce

You can use the @injectable annotation to mark classes as injectable, use @inject annotation to mark objects, functions and constructors for injection the following ways: (It is not necessary to mark a default constructor with @inject - only complex CTORs must be marked)

  • 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 either by extending the Module class and using one of its bind functions or directly on the Injector.

  • register type MyType.
  • register interface MyType to a class implementing it.
  • register a singleton
  • register type MyType to existing object (another way for singleton injections)
  • register a typedef to a function matching it.
  • register MyType to function that can build instances of it
bind(MyType).toBuilder(() => new MyType())
  • use Module to install other modules configuration
class MyApplicationModule extends Module {
  configure() {
    install(new ComponentModule());


Named Injections

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

class MyClass {
  SomeClass _someClass;

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

bind(MyType, named: "my-name").toType(MyTypeImpl)

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

Annotated (typed) Injections

You can also use other classes for annotation. works everywhere the @inject annotation works.

class UrlGoogle { const UrlGoogle(); }

class UrlFacebook { const UrlFacebook(); }

class MyModule extends Module {
  configure() {
    // annotated
    bind(String,annotatedWith: UrlGoogle ).toInstance("http://www.google.com/");
    bind(String,annotatedWith: UrlFacebook ).toInstance("http://www.facebook.com/");

class MyClass {
  String url;

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

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 getInstance method with its named parameter.
MyType instance = injector.getInstance(MyType, named: "my-name");
  • Get annotated instances directly Instead of using the appropriate annotation to resolve annotated injections you can use the injectors getInstance method with its annotatedWith parameter.
String url = injector.getInstance(MyType, annotatedWith: UrlGoogle);
  • To register and resole configuration values You can use named or annotated registrations to inject configuration values into your application.
class TestModule extends Module {
  configure() {
		bind(String, named: "web-service-host").toInstace("http://test-service.name");
		bind(String, annotatedWith: UrlGoogle ).toInstance("http://www.google.com/");

// application code
String get webServiceHost => injector.getInstance(String, named: "web-service-host");
String get webServiceHost2 => injector.getInstance(String, annotatedWith: UrlGoogle);
  • Constructor injection DryIce also support constructors with optional params.
class MyClass {
  String getName() => "MyClass";

class CTOROptionalInjection extends MyClass {
  final String url;
  final String lang;

  CTOROptionalInjection(@UrlGoogle() final String this.url,[ final String language ])
      : lang = language ?? "C++";

  String getName() => "CTORInjection - $url ($lang)";

final injector = new Injector()
  ..bind(String,annotatedWith: UrlGoogle ).toInstance("http://www.google.com/")
final MyClass mc = injector.getInstance(MyClass);
  • Registering dependencies at runtime You can bind dependencies at runtime directly on the Injector.
 var user = injector.getInstance(User);
  • Unregistering dependencies at runtime You can unregister dependencies at runtime using the unregister method on the Injector.
  • Using multiple modules You can compose modules 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);
  • Install other modules within main module
class MyModule extends Module {
  	configure() {

class MyMainModule extends Module {
  	configure() {
  	    install(new MyModule());

var injector = new Injector( new MyMainModule());
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();

var yourInjector = new Injector();

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

Compatibility / migration from di:package

To make migration easier we provide the following functions:

  • Injector.bind is the same as Injector.register.
  • Module.bind is the same as Module.register
  • Injector.get is the same as Injector.getInstance
  • Registration.to is the same as Registration.toType

Be aware that Injector.register and may become depreciated in one of the next releases.

Prefer the bind, to and theget` version over its equivalent.


This package is based "Dice" - Thanks Lars Tackmann!

Change Log for dryice

Lightweight dependency injection framework for Dart.



  • DryIce is now by default Dart 2.x ready and uses reflectable 98c1ed2


  • new bind overwrites the previus one (ModuleContainer) 357dcbd


  • Remove weird recursive injectableDeclarations-Function 28a92b5


v2.0 - 2018-05-30



  • new bind overwrites the previus one (ModuleContainer) 6aedbe8


  • Expected wrong return type for ClassMirror#newInstance a7e1476


This CHANGELOG.md was generated with Changelog for Dart

Use this package as a library

1. Depend on it

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

  dryice: "^2.0.15"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

      import 'package:dryice/dryice.dart';
Version Uploaded Documentation Archive
2.0.15 Jun 1, 2018 Go to the documentation of dryice 2.0.15 Download dryice 2.0.15 archive
1.8.3 Mar 26, 2018 Go to the documentation of dryice 1.8.3 Download dryice 1.8.3 archive
1.8.0 Mar 26, 2018 Go to the documentation of dryice 1.8.0 Download dryice 1.8.0 archive


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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3


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


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dryice/dryice.dart.


  • 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.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 7 hints.

    Run dartfmt to format lib/dryice.dart.

    Run dartfmt to format lib/src/Registration.dart.

    Similar analysis of the following files failed:

    • lib/src/annotations.dart (hint)
    • lib/src/assert_messages.dart (hint)
    • lib/src/injector.dart (hint)
    • lib/src/mirror_util.dart (hint)
    • lib/src/module.dart (hint)
  • 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 dryice.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.9.0 <2.0.0
logging >=0.11.3 <1.0.0 0.11.3+1
reflectable ^2.0.0 2.0.1
Transitive dependencies
analyzer 0.31.1 0.32.1
args 1.4.3
async 2.0.7
barback 0.15.2+16
build 0.12.6
build_barback 0.5.0+4
build_config 0.2.6+2 0.3.0
build_resolvers 0.2.0+2
built_collection 3.1.1
built_value 5.5.2
charcode 1.1.1
cli_util 0.1.3
code_builder 3.1.0
code_transformers 0.5.1+5
collection 1.14.10
convert 2.0.1
crypto 2.0.5
csslib 0.14.4
dart_style 1.0.9+1 1.1.0
fixnum 0.10.7
front_end 0.1.0-alpha.9 0.1.1
glob 1.1.5
graphs 0.1.2
html 0.13.3+1
http 0.11.3+16
http_multi_server 2.0.5
http_parser 3.1.2
io 0.3.2+1
isolate 1.1.0 2.0.1
kernel 0.3.0-alpha.9 0.3.1
matcher 0.12.3
meta 1.1.5
mime 0.9.6+1
package_config 1.0.3
package_resolver 1.0.3
path 1.6.1
plugin 0.2.0+2
pool 1.3.5
quiver 0.29.0+1
shelf 0.7.3+1
source_maps 0.10.5
source_span 1.4.0
stack_trace 1.9.2
stream_channel 1.6.7+1
stream_transform 0.0.14
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+8
yaml 2.1.14
Dev dependencies
build_runner any 0.8.9 0.8.10
build_test any
build_web_compilers any
console_log_handler ^1.0.0
logging_handlers ^0.8.0
test any