forked from chakrit/swift-ioc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
section-1.swift
114 lines (90 loc) · 2.82 KB
/
section-1.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import Foundation
typealias Registry = [String: () -> AnyObject]
protocol Resolver {
var registrations: Registry { get }
func resolve<T: AnyObject>() -> T
}
private class EmptyContainer: Resolver {
let registrations: Registry = [:]
func resolve<T : AnyObject>() -> T {
fatalError("no registration for requested type.")
}
}
private class Container: Resolver {
let registrations: Registry
init(name: String, resolver: () -> AnyObject) {
registrations = [name: resolver]
}
init(previousRegistrations: Registry, name: String, resolver: () -> AnyObject) {
var merged = previousRegistrations
merged[name] = resolver
registrations = merged
}
init(previousRegistrations: Registry, additionalRegistrations: Registry) {
var merged = previousRegistrations
for (key, value) in additionalRegistrations {
merged[key] = value
}
registrations = merged
}
func resolve<T : AnyObject>() -> T {
let name = toString(T)
if let resolver = registrations[name] {
return resolver() as! T
} else {
fatalError("no registration for requested type.")
}
}
}
func emptyContainer() -> Resolver {
return EmptyContainer()
}
func singleton<T: AnyObject>(instance: T) -> Resolver {
return Container(name: toString(T),
resolver: { () -> AnyObject in instance })
}
func factory<T: AnyObject>(factory: () -> T) -> Resolver {
return Container(name: toString(T), resolver: factory)
}
func +(lhs: Resolver, rhs: Resolver) -> Resolver {
return Container(previousRegistrations: lhs.registrations,
additionalRegistrations: rhs.registrations)
}
func +=(inout lhs: Resolver, rhs: Resolver) {
lhs = lhs + rhs
}
prefix operator <- {}
prefix func <-<T: AnyObject>(resolver: Resolver) -> T {
return resolver.resolve()
}
// ______________________________________________________________________
// T E S T
class TestObject {
let id: String = NSUUID().UUIDString
init() { dump("ctor: \(id)") }
}
class Singleton: TestObject { }
class Factory: TestObject { }
var container = emptyContainer()
container += singleton(Singleton())
container += factory({ Factory() })
var singleton: Singleton = <-container
dump(singleton.id)
singleton = <-container
dump(singleton.id)
singleton = <-container
dump(singleton.id)
// In Swift1.2 @autoclosure now implies @noescape so we can no longer use it like what we did
// previously.
//var auto: Auto = <-container
//dump(auto.id)
//auto = <-container
//dump(auto.id)
//auto = <-container
//dump(auto.id)
var factory: Factory = <-container
dump(factory.id)
factory = <-container
dump(factory.id)
factory = <-container
dump(factory.id)