This repository was archived by the owner on Dec 6, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE - 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. There is also a faster NodeInjector class for specific use cases where many injector features aren’t needed. (Miško has more info on NodeInjectors) - ModuleInjectors have no visibility - All bindings and instances of parent injectors are now visible in child injectors. NodeInjectors do, however, have some sense of visibility. - 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 one of: - 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 array “p” 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(Engine, toFactory: (i) => i.get(Engine)); - New code. module.bind(Engine, toFactory: (e) => e, inject: [Engine]); - With sugar. module.bind(Engine, 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. Typical use will not need to worry about this at all, and the default reflector will suffice. If needed, implement TypeReflector and 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 - And it’s lots faster!
- Loading branch information
Anting Shen
committed
Jul 23, 2014
1 parent
57523ea
commit 10e471b
Showing
49 changed files
with
1,833 additions
and
1,711 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
import 'package:di/dynamic_injector.dart'; | ||
import 'package:di/src/reflector_dynamic.dart'; | ||
|
||
import 'injector_benchmark_common.dart'; | ||
|
||
|
||
main() { | ||
new InjectorBenchmark('DynamicInjectorBenchmark', | ||
(m) => new DynamicInjector(modules: m)).report(); | ||
} | ||
new DynamicTypeFactories()).report(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,22 @@ | ||
import 'package:di/static_injector.dart'; | ||
import 'package:di/di.dart'; | ||
import 'package:di/src/reflector_static.dart'; | ||
|
||
import 'injector_benchmark_common.dart'; | ||
|
||
// tests the speed of cached getInstanceByKey requests | ||
class InstanceBenchmark extends InjectorBenchmark{ | ||
InstanceBenchmark(name, injectorFactory) : super(name, injectorFactory); | ||
InstanceBenchmark(name, typeReflector) : super(name, typeReflector); | ||
|
||
void run(){ | ||
Injector injector = injectorFactory([module]); | ||
Injector injector = new ModuleInjector([module]); | ||
for (var i = 0; i < 30; i++) { | ||
injector.get(A); | ||
} | ||
} | ||
} | ||
|
||
main() { | ||
var typeFactories = { | ||
A: (f) => new A(f(B), f(C)), | ||
B: (f) => new B(f(D), f(E)), | ||
C: (f) => new C(), | ||
D: (f) => new D(), | ||
E: (f) => new E(), | ||
}; | ||
|
||
new InstanceBenchmark('InstanceBenchmark', | ||
(m) => new StaticInjector(modules: m, typeFactories: typeFactories) | ||
new GeneratedTypeFactories(typeFactories, paramKeys) | ||
).report(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
import 'package:di/di.dart'; | ||
import 'package:di/src/reflector_static.dart'; | ||
import 'generated_files/factories.dart'; | ||
|
||
import 'dart:math'; | ||
|
||
class LargeBenchmark extends BenchmarkBase { | ||
var injectorFactory; | ||
Injector rootInjector; | ||
Injector leafInjector; | ||
var leafKey; | ||
var rng = new Random(); | ||
var numInjectors = 1; | ||
var allLeaves = []; | ||
|
||
LargeBenchmark(name, this.injectorFactory) : super("Large" + name); | ||
|
||
setup() { | ||
var rootModule = new Module() | ||
..bindByKey(key999) | ||
..bindByKey(key998) | ||
..bindByKey(key997) | ||
..bindByKey(key996) | ||
..bindByKey(key995); | ||
rootInjector = injectorFactory([rootModule]); | ||
|
||
createChildren (injector, depth, width) { | ||
if (depth <= 0){ | ||
allLeaves.add(injector); | ||
return; | ||
} | ||
for (var i=0; i<width; i++){ | ||
var module = new Module(); | ||
for (var j=0; j<5; j++){ | ||
leafKey = allKeys[rng.nextInt(995)]; | ||
module.bindByKey(leafKey); | ||
} | ||
leafInjector = injector.createChild([module]); | ||
numInjectors++; | ||
createChildren(leafInjector, depth-1, width); | ||
} | ||
} | ||
|
||
createChildren(rootInjector, 5, 5); | ||
print("$numInjectors injectors created."); | ||
} | ||
} | ||
|
||
class GetFromRoot extends LargeBenchmark { | ||
GetFromRoot() : super('FromRoot', (m) => new ModuleInjector(m)); | ||
|
||
run() { | ||
leafInjector.getByKey(key999); | ||
} | ||
} | ||
|
||
class GetFromLeaf extends LargeBenchmark { | ||
GetFromLeaf() : super('FromLeaf', (m) => new ModuleInjector(m)); | ||
|
||
run() { | ||
leafInjector.getByKey(leafKey); | ||
} | ||
} | ||
|
||
main() { | ||
Module.DEFAULT_REFLECTOR = new GeneratedTypeFactories(typeFactories, parameterKeys); | ||
new GetFromRoot().report(); | ||
new GetFromLeaf().report(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.