Skip to content

Commit 1377a9b

Browse files
committed
minor wrappers refactoring
1 parent 706e16a commit 1377a9b

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

Diff for: Dip/Dip/AutoInjection.swift

+35-25
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ class ClientImp: Client {
4646
*/
4747
public final class Injected<T>: _Injected {
4848

49+
static var tag: DependencyContainer.Tag {
50+
return .String("\(Injected<T>.self)")
51+
}
52+
4953
var _value: Any?
5054

51-
public init() {}
52-
5355
public var value: T? {
5456
get {
5557
return _value as? T
@@ -58,7 +60,9 @@ public final class Injected<T>: _Injected {
5860
_value = newValue
5961
}
6062
}
61-
63+
64+
public init() {}
65+
6266
}
6367

6468
/**
@@ -97,10 +101,12 @@ public final class InjectedWeak<T>: _InjectedWeak {
97101
//so we just rely on user reading documentation and passing AnyObject in runtime
98102
//also we will throw fatal error if type can not be casted to AnyObject during resolution
99103

104+
static var tag: DependencyContainer.Tag {
105+
return .String("\(InjectedWeak<T>.self)")
106+
}
107+
100108
weak var _value: AnyObject?
101109

102-
public init() {}
103-
104110
public var value: T? {
105111
get {
106112
return _value as? T
@@ -109,7 +115,9 @@ public final class InjectedWeak<T>: _InjectedWeak {
109115
_value = newValue as? AnyObject
110116
}
111117
}
112-
118+
119+
public init() {}
120+
113121
}
114122

115123
extension DependencyContainer {
@@ -140,16 +148,8 @@ extension DependencyContainer {
140148

141149
*/
142150
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)
153153
}
154154
}
155155

@@ -162,11 +162,11 @@ extension DependencyContainer {
162162
typealias InjectedWeakFactory = ()->AnyObject
163163

164164
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)
166166
}
167167

168168
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)
170170
}
171171

172172
func registerInjected<T, F>(definition: DefinitionOf<T, F>) {
@@ -191,19 +191,29 @@ extension DependencyContainer {
191191

192192
}
193193

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 }
196197
}
197198

198-
func injectedWeakTag<T>(type: T.Type) -> String {
199-
return "\(InjectedWeak<T>().dynamicType)"
199+
protocol _Injected: class, _AutoInjected {
200+
var _value: Any? { get set }
200201
}
201202

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+
}
204207
}
205208

206-
protocol _InjectedWeak: class {
209+
protocol _InjectedWeak: class, _AutoInjected {
207210
weak var _value: AnyObject? { get set }
208211
}
209212

213+
extension _InjectedWeak {
214+
func resolve(container: DependencyContainer) {
215+
self._value = container.resolve(tag: self.dynamicType.tag) as AnyObject
216+
}
217+
}
218+
219+

Diff for: Dip/Dip/Definition.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ public final class DefinitionOf<T, F>: Definition {
105105
self.tag = tag
106106

107107
if let factory = factory as? ()->T where tag == nil {
108-
injectedDefinition = DefinitionOf<Any, ()->Any>(factory: { factory() }, scope: scope, tag: DependencyContainer.Tag.String(injectedTag(T.self)))
108+
injectedDefinition = DefinitionOf<Any, ()->Any>(factory: { factory() }, scope: scope, tag: Injected<T>.tag)
109109

110110
injectedWeakDefinition = DefinitionOf<AnyObject, ()->AnyObject>(factory: {
111111
guard let result = factory() as? AnyObject else {
112112
fatalError("\(T.self) can not be casted to AnyObject. InjectedWeak wrapper should be used to wrap only classes.")
113113
}
114114
return result
115-
}, scope: scope, tag: DependencyContainer.Tag.String(injectedWeakTag(T.self)))
115+
}, scope: scope, tag: InjectedWeak<T>.tag)
116116
}
117117

118118
}

Diff for: Dip/DipTests/AutoInjectionTests.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,13 @@ class AutoInjectionTests: XCTestCase {
6868

6969
override func setUp() {
7070
super.setUp()
71-
// Put setup code here. This method is called before the invocation of each test method in the class.
71+
7272
container.reset()
7373
AutoInjectionTests.serverDeallocated = false
7474
AutoInjectionTests.clientDeallocated = false
7575

7676
container.register(.ObjectGraph) { ServerImp() as Server }
7777
container.register(.ObjectGraph) { ClientImp() as Client }
78-
7978
}
8079

8180
func testThatItResolvesInjectedDependencies() {
@@ -100,15 +99,18 @@ class AutoInjectionTests: XCTestCase {
10099
func testThatItResolvesAutoInjectedSingletons() {
101100
container.reset()
102101

102+
//given
103103
container.register(.Singleton) { ServerImp() as Server }
104104
container.register(.Singleton) { ClientImp() as Client }
105105

106+
//when
106107
let sharedClient = container.resolve() as Client
107108
let sharedServer = container.resolve() as Server
108109

109110
let client = container.resolve() as Client
110111
let server = client.server
111112

113+
//then
112114
XCTAssertTrue(client as! ClientImp === sharedClient as! ClientImp)
113115
XCTAssertTrue(client as! ClientImp === server?.client as! ClientImp)
114116
XCTAssertTrue(server as! ServerImp === sharedServer as! ServerImp)

Diff for: Dip/DipTests/ComponentScopeTests.swift

-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,9 @@ class ComponentScopeTests: XCTestCase {
3131

3232
override func setUp() {
3333
super.setUp()
34-
// Put setup code here. This method is called before the invocation of each test method in the class.
3534
container.reset()
3635
}
3736

38-
override func tearDown() {
39-
// Put teardown code here. This method is called after the invocation of each test method in the class.
40-
super.tearDown()
41-
}
42-
4337
func testThatPrototypeIsDefaultScope() {
4438
let def = container.register { ServiceImp1() as Service }
4539
XCTAssertEqual(def.scope, ComponentScope.Prototype)

0 commit comments

Comments
 (0)