| 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 |
| 5 /** | 7 /** |
| 6 * Static implementation of [Injector] that uses type factories | 8 * Static implementation of [Injector] that uses type factories |
| 7 */ | 9 */ |
| 8 class StaticInjector extends Injector { | 10 class StaticInjector extends Injector { |
| 9 Map<Type, TypeFactory> typeFactories; | 11 Map<Type, TypeFactory> typeFactories; |
| 10 | 12 |
| 11 StaticInjector({List<Module> modules, String name, | 13 StaticInjector({List<Module> modules, String name, |
| 12 bool allowImplicitInjection: false, typeFactories}) | 14 bool allowImplicitInjection: false, this.typeFactories}) |
| 13 : super(modules: modules, name: name, | 15 : super(modules: modules, name: name, |
| 14 allowImplicitInjection: allowImplicitInjection) { | 16 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 } | |
| 22 | 20 |
| 23 newFromParent(List<Module> modules, String name) { | 21 newFromParent(List<Module> modules, String name) { |
| 24 return new StaticInjector._fromParent(modules, this, name: name); | 22 return new StaticInjector._fromParent(modules, this, name: name); |
| 25 } | 23 } |
| 26 | 24 |
| 27 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, | 25 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, |
| 28 Injector requestor, error) { | 26 Injector requestor, error) { |
| 29 TypeFactory typeFactory = _getFactory(type); | 27 TypeFactory typeFactory = (root as StaticInjector).typeFactories[type]; |
| 30 if (typeFactory == null) { | 28 if (typeFactory == null) { |
| 31 throw new NoProviderError(error('No type factory provided for $type!')); | 29 throw new NoProviderError(error('No type factory provided for $type!')); |
| 32 } | 30 } |
| 33 return typeFactory((type) => getInstanceByType(type, requestor)); | 31 return typeFactory((type) => getInstanceByType(type, requestor)); |
| 34 } | 32 } |
| 35 | 33 } |
| 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 |