OLD | NEW |
1 library di.static_injector; | 1 library di.static_injector; |
2 | 2 |
3 import 'di.dart'; | 3 import 'di.dart'; |
| 4 import 'src/error_helper.dart'; |
| 5 import 'src/base_injector.dart'; |
| 6 import 'src/provider.dart'; |
| 7 |
| 8 export 'annotations.dart'; |
| 9 export 'di.dart'; |
4 | 10 |
5 /** | 11 /** |
6 * Static implementation of [Injector] that uses type factories | 12 * Static implementation of [Injector] that uses type factories |
7 */ | 13 */ |
8 class StaticInjector extends Injector { | 14 class StaticInjector extends BaseInjector { |
9 Map<Type, TypeFactory> typeFactories; | 15 Map<Type, TypeFactory> typeFactories; |
10 | 16 |
11 StaticInjector({List<Module> modules, String name, | 17 StaticInjector({List<Module> modules, String name, |
12 bool allowImplicitInjection: false, typeFactories}) | 18 bool allowImplicitInjection: false, typeFactories}) |
13 : super(modules: modules, name: name, | 19 : super(modules: modules, name: name, |
14 allowImplicitInjection: allowImplicitInjection) { | 20 allowImplicitInjection: allowImplicitInjection) { |
15 this.typeFactories = _extractTypeFactories(modules, typeFactories); | 21 this.typeFactories = _extractTypeFactories(modules, typeFactories); |
16 } | 22 } |
17 | 23 |
18 StaticInjector._fromParent(List<Module> modules, Injector parent, {name}) | 24 StaticInjector._fromParent(List<Module> modules, Injector parent, {name}) |
19 : super.fromParent(modules, parent, name: name) { | 25 : super.fromParent(modules, parent, name: name) { |
20 this.typeFactories = _extractTypeFactories(modules); | 26 this.typeFactories = _extractTypeFactories(modules); |
21 } | 27 } |
22 | 28 |
23 newFromParent(List<Module> modules, String name) { | 29 newFromParent(List<Module> modules, String name) => |
24 return new StaticInjector._fromParent(modules, this, name: name); | 30 new StaticInjector._fromParent(modules, this, name: name); |
| 31 |
| 32 Object newInstanceOf(Type type, ObjectFactory objFactory, |
| 33 Injector requestor, resolving) { |
| 34 TypeFactory typeFactory = _getFactory(type); |
| 35 if (typeFactory == null) { |
| 36 throw new NoProviderError( |
| 37 error(resolving, 'No type factory provided for $type!')); |
| 38 } |
| 39 return typeFactory((type, [annotation]) => |
| 40 objFactory.getInstanceByKey( |
| 41 new Key(type, annotation), requestor, resolving)); |
25 } | 42 } |
26 | 43 |
27 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, | 44 TypeFactory _getFactory(Type key) { |
28 Injector requestor, error) { | |
29 TypeFactory typeFactory = _getFactory(type); | |
30 if (typeFactory == null) { | |
31 throw new NoProviderError(error('No type factory provided for $type!')); | |
32 } | |
33 return typeFactory((type) => getInstanceByType(type, requestor)); | |
34 } | |
35 | |
36 TypeFactory _getFactory(Type type) { | |
37 var cursor = this; | 45 var cursor = this; |
38 while (cursor != null) { | 46 while (cursor != null) { |
39 if (cursor.typeFactories.containsKey(type)) { | 47 if (cursor.typeFactories.containsKey(key)) { |
40 return cursor.typeFactories[type]; | 48 return cursor.typeFactories[key]; |
41 } | 49 } |
42 cursor = cursor.parent; | 50 cursor = cursor.parent; |
43 } | 51 } |
44 return null; | 52 return null; |
45 } | 53 } |
46 } | 54 } |
47 | 55 |
48 Map<Type, TypeFactory> _extractTypeFactories(List<Module> modules, | 56 Map<Type, TypeFactory> _extractTypeFactories(List<Module> modules, |
49 [Map<Type, TypeFactory> initial = const {}]) { | 57 [Map<Type, TypeFactory> initial = const {}]) { |
50 if (modules == null || modules.isEmpty) return initial; | 58 if (modules == null || modules.isEmpty) return initial; |
51 var tmp = new Map.from(initial == null ? {} : initial); | 59 var factories = new Map.from(initial == null ? {} : initial); |
52 modules.forEach((module) { | 60 modules.forEach((m) { |
53 module.typeFactories.forEach((type, factory) { | 61 factories.addAll(m.typeFactories); |
54 tmp[type] = factory; | |
55 }); | |
56 }); | 62 }); |
57 return tmp; | 63 return factories; |
58 } | 64 } |
OLD | NEW |