di 3.3.10

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 76

Build Status

Dependency Injection (DI) framework

Installation

Add dependency to your pubspec.yaml.

dependencies:
  di: ">=3.3.4 <4.0.0"

Then, run pub install.

Import di.

import 'package:di/di.dart';

Example

import 'package:di/di.dart';

abstract class Engine {
  go();
}

class Fuel {}

class V8Engine implements Engine {
  Fuel fuel;
  V8Engine(this.fuel);

  go() {
    print('Vroom...');
  }
}

class ElectricEngine implements Engine {
  go() {
    print('Hum...');
  }
}

// Annotation
class Electric {
  const Electric();
}

class GenericCar {
  Engine engine;

  GenericCar(this.engine);

  drive() {
    engine.go();
  }
}

class ElectricCar {
  Engine engine;

  ElectricCar(@Electric() this.engine);

  drive() {
    engine.go();
  }
}

void main() {
  var injector = new ModuleInjector([new Module()
      ..bind(Fuel)
      ..bind(GenericCar)
      ..bind(ElectricCar)
      ..bind(Engine, toFactory: (fuel) => new V8Engine(fuel), inject: [Fuel])
      ..bind(Engine, toImplementation: ElectricEngine, withAnnotation: const Electric())
  ]);
  injector.get(GenericCar).drive(); // Vroom...
  injector.get(ElectricCar).drive(); // Hum...
}

Contributing

Refer to the guidelines for contributing to AngularDart.

3.3.7

  • Update dependencies. Support dart 1.14 sdk.

3.3.6

  • Move to analyzer 0.27.1
  • Move to guinness2

3.3.5+1

Bug fix

  • Properly transform an async main method in an entry point.

3.3.5

  • Widen dependency on package:analyzer.

3.3.4

  • Widen dependency on package:analyzer.

3.3.3

Dependencies

  • Upgraded pubspec dependencies to use newer versions.

Clean-up

  • Eliminate dead code and analyzer warnings.

3.3.2

Bug fix

  • A class would only be marked as injectable if the first annotation was one of the injectable annotations (GH-191). The DI now checks all the annotations and no more the first one only.

3.3.1

Bug fix

  • Fix a bug in FF caused by using a stack trace in a warning message.

3.3.0

This release makes DI 3.0 backward compatible with DI 2.0.

  • Annotation assertions are now disabled by default. To enable them set Module.assertAnnotations to true.
  • The di.dart library now exports Injectable and Injectables.
  • Core types can be listed in a library level @Injectables annotation.

3.2.0

New feature

  • It is now possible to disable annotation assertion by setting Module.assertAnnotations to false (the feature is still enabled by default)

3.1.1

Bug fix

  • Revert "perf(Binding): saves a call to reflector.parameterKeysFor()" which is buggy

3.1.0

New features

  • Classes could be annotated with a child of Injectable to mark them as injectable. Before only Injectable was allowed.

  • An annotation instance must be passed when binding with an annotation. Before: module.bind(MyType, withAnnotation: MyAnnotation); After: module.bind(MyType, withAnnotation: const MyAnnotation());

    The former syntax is deprecated and the support will be dropped in the next major release.

3.0.0

Breaking Change

  • When assert mode is enabled, the dynamic version of the DI will make sure that the classes are set up for injection when they are bound. By default classes should either be annotated with @Injectable or listed in a library level @Injectables annotation. The class level and library levels annotations could be changed in Module.classAnnotations and Module.libAnnotations.

2.0.2

Features

  • It is now possible to inject parameterized types by using TypeLiteral:
import 'package:di/type_literal.dart';

class DependencyWithParameterizedMap {
  Map<int, String> map;
  DependencyWithParameterizedMap(this.map);
}

var injector = new ModuleInjector([moduleFactory()
    ..bind(new TypeLiteral<Map<int, String>>().type, toValue: {1 : 'first', 2: 'second'})
    ..bind(DependencyWithParameterizedMap)
]);

Breaking Change

  • annotations: Users must now explicitly import di/annotations.dart to use @Injectable

Fixes

  • Supports newer versions of barback, code transformers, and analyzer

2.0.1

Bug Fixes

  • bind: It must accept a Key as well as a Type (e01bcda6, #154)

Features

  • Binding: Display call stack when usign deprecated bind() form (f20b3ba7)

2.0.0

Breaking Changes

Calls to StaticInjector and DynamicInjector should be replaced with ModuleInjector

  • There are no longer StaticInjectors and DynamicInjectors. They have been replaced by a new ModuleInjector class that acts as both types of injectors.

ModuleInjectors have no visibility

  • All bindings and instances of parent injectors are now visible in child injectors.
  • The optional argument forceNewInstances of Injector.createChild has been removed Instead, create a new module with bindings of the types that require new instances and pass that to the child injector, and the child injector will create new instances instead of returning the instance of the parent injector.

Use new ModuleInjector(modules, parent) instead of Injector.createChild(modules)

  • The latter is still available but deprecated.
  • Injectors with no parent now have a dummy RootInjector instance as the parent Instead of checking “parent == null”, check for “parent == rootInjector”.

Injectors no longer have a name field

typeFactories have changed

  • Old type factories had the form (injector) => new Instance(injector.get(dep1), … )

  • New factories have the form:

    • toFactory(a0, a1, …) => new Instance(a0, a1, …)
  • When calling Module.bind(toFactory: factory), there is an additional argument inject of a list of types or keys (preferred for performance) whose instances should be passed to the factory. The arguments passed to the factory function will be instances of the types in inject.

    Example:

    • Old code module.bind(Car, toFactory: (i) => new Car(i.get(Engine)));
    • New code
      • module.bind(Car, toFactory: (engine) => new Car(engine), inject: [Engine]);

    There is also some syntactic sugar for this special case.

    • Old code module.bind(V8Engine, toFactory: (i) => i.get(Engine));
    • New code module.bind(V8Engine, toFactory: (e) => e, inject: [Engine]);
    • With sugar module.bind(V8Engine, toInstanceOf: Engine);

Modules have a TypeReflector instance attached

  • The TypeReflector is how the module will find the toFactory and inject arguments when not explicitly specified. This is either done with mirroring or code generation via transformers. Transformers will set the default to use code gen. For testing and other special purposes where a specific reflector is needed, use new Module.withReflector(reflector).

The transformer has been updated

  • Running the transformer will do the necessary code generation and edits to switch the default TypeReflector from mirroring to static factories. Enable transformer to use static factories, disable to use mirrors. More docs on the transformer can be found in transformer.dart

Deprecated module methods removed

  • .value, .type, .factory, .factoryByKey are gone. Use ..bind.

Deprecations

  • module.bind() calls specifying the inject parameter but no toFactory have been deprecated and will be removed in v3. Use the toInstanceOf parameter instead.
  • The dynamic injector shim (dynamic_injector.dart) has been added to ensure backward compatibility with v1 and will be removed in v3.

1.2.3

Features

  • module: Expose DEFAULT_VALUE temporarily (6f5d88a1)

1.2.2

Reverted changes that tickled a Dart bug (to be fixed in 1.6)

1.2.1

Added missing library declaration to injector.

1.2.0

Features

  • module: allow v2 style toFactory binding with inject (1ef6ba71)

Performance Improvements

  • injector: inlined getProviderWithInjector (d2a38b54)

1.1.0

Performance Improvements

  • injector: optimized module to injector instantiation (62f22f15)

1.0.0

Starting with this release DI is following semver.

Bug Fixes

  • Key: fixed bugs caused by hashCode collisions, and docs cleanup (f673267d, #94)
  • circular deps: Improve error messages (4ccdb1f0)

Performance Improvements

  • Key: don't use Map.putIfAbsent -- too slow (0930b377)
  • injector: use separate structures to allow compiler optimizations (f7b8af92)

0.0.40

Bug Fixes

  • module: correctly handle null value binding (ada47b36, #93)

0.0.39

Bug Fixes

  • transformer: Exception on parameterized types with implicit constructors (ed0a2b02)

Features

Breaking Changes

Module has a new API:

new Module()
    ..bind(Foo, toValue: new Foo())
    ..bind(Foo, toFactory: (i) => new Foo())
    ..bind(Foo, toImplementation: FooImpl);

Old methods type, value and factory were deprecated and will be removed in the next release.

0.0.38

Fixes

  • key: made Key part of di.dart again (fe390ddf)

0.0.37

Combined with previous release (0.0.36) injector is on average 2x faster.

Before:

VM:
DynamicInjectorBenchmark(RunTime): 231.93784065870346 us.
StaticInjectorBenchmark(RunTime): 107.05491917353602 us.

dart2js:
DynamicInjectorBenchmark(RunTime): 2175 us.
StaticInjectorBenchmark(RunTime): 765.1109410864575 us.

After:

VM:
DynamicInjectorBenchmark(RunTime): 156.3721657544957 us.
StaticInjectorBenchmark(RunTime): 54.246114622040196 us.

dart2js:
DynamicInjectorBenchmark(RunTime): 1454.5454545454545 us.
StaticInjectorBenchmark(RunTime): 291.9281856663261 us.

Bug Fixes

  • warnings: refactored injector to fix analyzer warnings (7d374b19)

Performance Improvements

  • injector:
    • Make resolving a linked-list stored with the frame (c588e662)
    • Do not closurize methods. (5f47cbd0)
    • Do not check the circular dependency until we are 30 deep. (1dedf6e3)
    • Track resolving keys with the frame. (17aeb4df)
  • resolvedTypes: minor performance inmprovement in resolvedTypes (ba16bde5)

0.0.36

Performance Improvements

  • injector:
    • skip _checkKeyConditions in dart2js (6763552a)
    • +29%. Use an array for type lookup instead of a map.

1. Depend on it

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


dependencies:
  di: "^3.3.10"

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:di/di.dart';
        
Version Uploaded Documentation Archive
3.3.10 Apr 28, 2016 Go to the documentation of di 3.3.10 Download di 3.3.10 archive
3.3.9 Apr 27, 2016 Go to the documentation of di 3.3.9 Download di 3.3.9 archive
3.3.8 Mar 31, 2016 Go to the documentation of di 3.3.8 Download di 3.3.8 archive
3.3.7 Feb 1, 2016 Go to the documentation of di 3.3.7 Download di 3.3.7 archive
3.3.6 Jan 20, 2016 Go to the documentation of di 3.3.6 Download di 3.3.6 archive
3.3.5+1 Oct 21, 2015 Go to the documentation of di 3.3.5+1 Download di 3.3.5+1 archive
3.3.5 Oct 14, 2015 Go to the documentation of di 3.3.5 Download di 3.3.5 archive
3.3.4 Mar 19, 2015 Go to the documentation of di 3.3.4 Download di 3.3.4 archive
3.3.3 Dec 15, 2014 Go to the documentation of di 3.3.3 Download di 3.3.3 archive
3.3.2 Oct 15, 2014 Go to the documentation of di 3.3.2 Download di 3.3.2 archive

All 72 versions...

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
83 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
23 / 100
Overall score:
Weighted score of the above. [more]
76

Platforms

Detected platforms: web, other

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

Suggestions

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

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use di.dart.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.13.0 <2.0.0
analyzer 0.27.2 0.27.2 0.31.0+1
barback >=0.15.0 <0.16.0 0.15.2+14
code_transformers >=0.3.1 <0.4.0 0.3.1 0.5.1+4
path >=1.3.0 <2.0.0 1.5.1
Transitive dependencies
args 0.13.7 1.3.0
async 2.0.3
boolean_selector 1.0.2
charcode 1.1.1
cli_util 0.0.1+2 0.1.2+1
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
csslib 0.13.7+1 0.14.1
glob 1.1.5
html 0.12.2+2 0.13.2+2
http 0.11.3+16
http_multi_server 2.0.4
http_parser 3.1.1
io 0.3.2+1
js 0.6.1
logging 0.11.3+1
meta 1.1.2
mime 0.9.6
multi_server_socket 1.0.1
node_preamble 1.4.0
package_config 0.1.5 1.0.3
package_resolver 1.0.2
plugin 0.1.0 0.2.0+2
pool 1.3.4
pub_semver 1.3.2
shelf 0.7.2
shelf_packages_handler 1.0.3
shelf_static 0.2.7
shelf_web_socket 0.2.2
source_map_stack_trace 1.1.4
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.1
stream_channel 1.6.3
string_scanner 1.0.2
term_glyph 1.0.0
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
web_socket_channel 1.0.6
when 0.2.0
which 0.1.3
yaml 2.1.13
Dev dependencies
benchmark_harness >=1.0.4 <2.0.0
guinness2 >=0.0.3 <1.0.0
matcher >=0.11.0 <0.13.0 0.12.1+4
test >=0.12.7 <0.13.0 0.12.30+1