@@ -46,10 +46,12 @@ class ClientImp: Client {
46
46
*/
47
47
public final class Injected < T> : _Injected {
48
48
49
+ static var tag : DependencyContainer . Tag {
50
+ return . String( " \( Injected< T> . self ) " )
51
+ }
52
+
49
53
var _value : Any ?
50
54
51
- public init ( ) { }
52
-
53
55
public var value : T ? {
54
56
get {
55
57
return _value as? T
@@ -58,7 +60,9 @@ public final class Injected<T>: _Injected {
58
60
_value = newValue
59
61
}
60
62
}
61
-
63
+
64
+ public init ( ) { }
65
+
62
66
}
63
67
64
68
/**
@@ -97,10 +101,12 @@ public final class InjectedWeak<T>: _InjectedWeak {
97
101
//so we just rely on user reading documentation and passing AnyObject in runtime
98
102
//also we will throw fatal error if type can not be casted to AnyObject during resolution
99
103
104
+ static var tag : DependencyContainer . Tag {
105
+ return . String( " \( InjectedWeak< T> . self ) " )
106
+ }
107
+
100
108
weak var _value : AnyObject ?
101
109
102
- public init ( ) { }
103
-
104
110
public var value : T ? {
105
111
get {
106
112
return _value as? T
@@ -109,7 +115,9 @@ public final class InjectedWeak<T>: _InjectedWeak {
109
115
_value = newValue as? AnyObject
110
116
}
111
117
}
112
-
118
+
119
+ public init ( ) { }
120
+
113
121
}
114
122
115
123
extension DependencyContainer {
@@ -140,16 +148,8 @@ extension DependencyContainer {
140
148
141
149
*/
142
150
public func resolveDependencies( instance: Any ) {
143
- for child in Mirror ( reflecting: instance) . children
144
- where child. value is _Injected || child. value is _InjectedWeak {
145
-
146
- let tag = Tag . String ( " \( child. value. dynamicType) " )
147
- if let value = child. value as? _Injected {
148
- value. _value = resolve ( tag: tag) as Any
149
- }
150
- else if let weakValue = child. value as? _InjectedWeak {
151
- weakValue. _value = resolve ( tag: tag) as AnyObject
152
- }
151
+ for child in Mirror ( reflecting: instance) . children {
152
+ ( child. value as? _AutoInjected ) ? . resolve ( self )
153
153
}
154
154
}
155
155
@@ -162,11 +162,11 @@ extension DependencyContainer {
162
162
typealias InjectedWeakFactory = ( ) -> AnyObject
163
163
164
164
static func injectedKey< T> ( type: T . Type ) -> DefinitionKey {
165
- return DefinitionKey ( protocolType: Any . self, factoryType: InjectedFactory . self, associatedTag: Tag . String ( injectedTag ( type ) ) )
165
+ return DefinitionKey ( protocolType: Any . self, factoryType: InjectedFactory . self, associatedTag: Injected < T > . tag )
166
166
}
167
167
168
168
static func injectedWeakKey< T> ( type: T . Type ) -> DefinitionKey {
169
- return DefinitionKey ( protocolType: AnyObject . self, factoryType: InjectedWeakFactory . self, associatedTag: Tag . String ( injectedWeakTag ( type ) ) )
169
+ return DefinitionKey ( protocolType: AnyObject . self, factoryType: InjectedWeakFactory . self, associatedTag: InjectedWeak < T > . tag )
170
170
}
171
171
172
172
func registerInjected< T, F> ( definition: DefinitionOf < T , F > ) {
@@ -191,19 +191,29 @@ extension DependencyContainer {
191
191
192
192
}
193
193
194
- func injectedTag< T> ( type: T . Type ) -> String {
195
- return " \( Injected < T > ( ) . dynamicType) "
194
+ protocol _AutoInjected {
195
+ func resolve( container: DependencyContainer )
196
+ static var tag : DependencyContainer . Tag { get }
196
197
}
197
198
198
- func injectedWeakTag < T > ( type : T . Type ) -> String {
199
- return " \( InjectedWeak < T > ( ) . dynamicType ) "
199
+ protocol _Injected : class , _AutoInjected {
200
+ var _value : Any ? { get set }
200
201
}
201
202
202
- protocol _Injected : class {
203
- var _value : Any ? { get set }
203
+ extension _Injected {
204
+ func resolve( container: DependencyContainer ) {
205
+ self . _value = container. resolve ( tag: self . dynamicType. tag) as Any
206
+ }
204
207
}
205
208
206
- protocol _InjectedWeak : class {
209
+ protocol _InjectedWeak : class , _AutoInjected {
207
210
weak var _value : AnyObject ? { get set }
208
211
}
209
212
213
+ extension _InjectedWeak {
214
+ func resolve( container: DependencyContainer ) {
215
+ self . _value = container. resolve ( tag: self . dynamicType. tag) as AnyObject
216
+ }
217
+ }
218
+
219
+
0 commit comments