dryice 2.0.22

  • README.md
  • Installing
  • Versions
  • 77

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


  • Removed annotations.dart and moved InjectAnnotation to dryice.dart 633fc45


  • Removed all references to isInstanceOf, works with latest 'reflectable' version 1ee88c9
  • Multi-Bind-Test added 03ec3fc

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

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.22 Aug 29, 2018 Go to the documentation of dryice 2.0.22 Download dryice 2.0.22 archive
2.0.19 Aug 23, 2018 Go to the documentation of dryice 2.0.19 Download dryice 2.0.19 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
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.4


Detected platforms: Flutter, web, other

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

Health suggestions

Format lib/dryice.dart.

Run dartfmt to format lib/dryice.dart.

Format lib/src/Registration.dart.

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

Format lib/src/assert_messages.dart.

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

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/injector.dart (Run dartfmt to format lib/src/injector.dart.)
  • lib/src/mirror_util.dart (Run dartfmt to format lib/src/mirror_util.dart.)
  • lib/src/module.dart (Run dartfmt to format lib/src/module.dart.)

Maintenance suggestions

The description is too short. (-20 points)

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. (-10 points)

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 >=2.0.0 <3.0.0
logging >=0.11.3 <1.0.0 0.11.3+2
reflectable >=2.0.4 <3.0.0 2.0.7
Transitive dependencies
analyzer 0.32.6 0.33.0
args 1.5.0
async 2.0.8
build 0.12.8 1.0.1
build_config 0.3.1+3
build_resolvers 0.2.2+6
build_runner_core 1.0.0 1.0.1
built_collection 4.0.0
built_value 6.1.4
charcode 1.1.2
code_builder 3.1.3
collection 1.14.11
convert 2.0.2
crypto 2.0.6
csslib 0.14.6
dart_style 1.1.3 1.2.0
fixnum 0.10.8
front_end 0.1.4+2 0.1.6
glob 1.1.7
graphs 0.1.2+1
html 0.13.3+3
http 0.12.0
http_multi_server 2.0.5
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
json_annotation 1.2.0
kernel 0.3.4+2 0.3.6
matcher 0.12.4
meta 1.1.6
mime 0.9.6+2
package_config 1.0.5
package_resolver 1.0.6
path 1.6.2
pedantic 1.2.0
plugin 0.2.0+3
pool 1.3.6
pub_semver 1.4.2
pubspec_parse 0.1.2+2
quiver 2.0.0+1
shelf 0.7.3+3
shelf_web_socket 0.2.2+4
source_span 1.4.1
stack_trace 1.9.3
stream_channel 1.6.8
stream_transform 0.0.14+1
string_scanner 1.0.4
timing 0.1.1
typed_data 1.1.6
utf 0.9.0+5
watcher 0.9.7+10
web_socket_channel 1.0.9
yaml 2.1.15
Dev dependencies
build_runner any 0.10.3 1.0.0
build_test any
build_web_compilers any
console_log_handler ^1.0.0
test any