di 2.0.0-alpha.4 di: ^2.0.0-alpha.4 copied to clipboard
Dependency Injection framework
2.0.0-alpha.1 #
Breaking Changes #
Calls to StaticInjector and DynamicInjector should be replaced with ModuleInjector #
- There are no longer
StaticInjectors
andDynamicInjectors
. They have been replaced by a newModuleInjector
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
ofInjector.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 one of these two forms:
toFactory(a0, a1, …) => new Instance(a0, a1, …)
toFactoryPos(List<dependency instances> p) => new Instance(p[0], p[1] …)
-
When calling
Module.bind(toFactory: factory)
, there is an additional argumentinject
of a list of types or keys (preferred for performance) whose instances should be passed to the factory. The arrayp
passed to the factory function will be instances of the types ininject
.Example:
- Old code
module.bind(Car, toFactory: (i) => new Car(i.get(Engine)));
- New code
module.bind(Car, toFactoryPos: (p) => new Car(p[0]), inject: [Engine]);
module.bind(Car, toFactory: (engine) => new Car(engine), inject: [Engine]);
There is also some syntactic sugar for this special case.
- Old code
module.bind(Engine, toFactory: (i) => i.get(Engine));
- New code
module.bind(Engine, toFactory: (e) => e, inject: [Engine]);
- With sugar
module.bind(Engine, inject: [Engine]);
- Old code
Modules have a TypeReflector instance attached #
-
The
TypeReflector
is how the module will find thetoFactoryPos
andinject
arguments when not explicitly specified. This is either done with mirroring or code generation via transformers. Typical use will not need to worry about this at all, and should initialize it as shown below. If needed, implementTypeReflector
and usenew Module.withReflector(reflector)
. -
Modules need a
TypeReflector
instance initialized:import 'package:di/di_dynamic.dart'; main() { setupModuleTypeReflector(); }
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 intransformer.dart
- Annotating types for injection has not changed.
1.0.0 #
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.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:
- resolvedTypes: minor performance inmprovement in resolvedTypes (ba16bde5)