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