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