@@ -65,8 +65,8 @@ public class DependencyContainer {
6565 - parameter tag: tag used to register definition
6666 - parameter definition: definition to remove
6767 */
68- public func remove< T, F> ( definition: DefinitionOf < T , F > ) {
69- let key = DefinitionKey ( protocolType: T . self, factoryType: F . self, associatedTag: definition . tag)
68+ public func remove< T, F> ( definition: DefinitionOf < T , F > , forTag tag : Tag ? = nil ) {
69+ let key = DefinitionKey ( protocolType: T . self, factoryType: F . self, associatedTag: tag)
7070 definitions [ key] = nil
7171 removeInjected ( definition)
7272 removeInjectedWeak ( definition)
@@ -145,13 +145,8 @@ public class DependencyContainer {
145145
146146 */
147147 public func registerFactory< T, F> ( tag tag: Tag ? = nil , scope: ComponentScope , factory: F ) -> DefinitionOf < T , F > {
148- let key = DefinitionKey ( protocolType: T . self, factoryType: F . self, associatedTag: tag)
149- let definition = DefinitionOf < T , F > ( factory: factory, scope: scope, tag: tag)
150- definitions [ key] = definition
151-
152- registerInjected ( definition)
153- registerInjectedWeak ( definition)
154-
148+ let definition = DefinitionOf < T , F > ( scope: scope, factory: factory)
149+ register ( definition, forTag: tag)
155150 return definition
156151 }
157152
@@ -162,12 +157,14 @@ public class DependencyContainer {
162157 - parameter tag: The arbitrary tag to associate definition with
163158 - parameter definition: definition to register in container
164159 */
165- public func register< T, F> ( definition: DefinitionOf < T , F > ) {
166- let key = DefinitionKey ( protocolType: T . self, factoryType: F . self, associatedTag: definition . tag)
160+ public func register< T, F> ( definition: DefinitionOf < T , F > , forTag tag : Tag ? = nil ) {
161+ let key = DefinitionKey ( protocolType: T . self, factoryType: F . self, associatedTag: tag)
167162 definitions [ key] = definition
168163
169- registerInjected ( definition)
170- registerInjectedWeak ( definition)
164+ if tag == nil {
165+ registerInjected ( definition)
166+ registerInjectedWeak ( definition)
167+ }
171168 }
172169
173170 // MARK: Resolve dependencies
@@ -231,7 +228,7 @@ public class DependencyContainer {
231228 return resolvedInstances. resolve {
232229
233230 if let previouslyResolved: T = resolvedInstances. previouslyResolved ( key, definition: definition) {
234- resolvedInstances. storeResolvedInstance ( previouslyResolved, forKey: key)
231+ resolvedInstances. storeResolvedInstance ( previouslyResolved, forKey: key, definition : definition )
235232 return previouslyResolved
236233 }
237234 else {
@@ -241,11 +238,11 @@ public class DependencyContainer {
241238 //when it returns instance that we try to resolve here can be already resolved
242239 //so we return it, throwing away instance created by previous call to builder
243240 if let previouslyResolved: T = resolvedInstances. previouslyResolved ( key, definition: definition) {
244- resolvedInstances. storeResolvedInstance ( previouslyResolved, forKey: key)
241+ resolvedInstances. storeResolvedInstance ( previouslyResolved, forKey: key, definition : definition )
245242 return previouslyResolved
246243 }
247244
248- resolvedInstances. storeResolvedInstance ( resolvedInstance, forKey: key)
245+ resolvedInstances. storeResolvedInstance ( resolvedInstance, forKey: key, definition : definition )
249246 definition. resolvedInstance = resolvedInstance
250247 definition. resolveDependenciesBlock ? ( self , resolvedInstance)
251248 resolveDependencies ( resolvedInstance)
@@ -265,10 +262,12 @@ public class DependencyContainer {
265262 class ResolvedInstances {
266263 var resolvedInstances = [ DefinitionKey: Any] ( )
267264
268- func storeResolvedInstance< T> ( instance: T , forKey key: DefinitionKey ? ) {
265+ func storeResolvedInstance< T, F > ( instance: T , forKey key: DefinitionKey ? , definition : DefinitionOf < T , F > ) {
269266 resolvedInstances [ key] = instance
270- resolvedInstances [ DependencyContainer . injectedKey ( T . self) ] = instance
271- resolvedInstances [ DependencyContainer . injectedWeakKey ( T . self) ] = instance
267+ if key != nil {
268+ resolvedInstances [ definition. injectedKey] = instance
269+ resolvedInstances [ definition. injectedWeakKey] = instance
270+ }
272271 }
273272
274273 func previouslyResolved< T, F> ( key: DefinitionKey ? , definition: DefinitionOf < T , F > ) -> T ? {
0 commit comments