@@ -9,44 +9,51 @@ import Foundation
99
1010// ✅ Registration
1111// ✅ Resolution
12- // TODO: InstanceStore
12+ // ✅ InstanceStore
13+ // TODO: Error Handling
14+ // TODO: Write Unit Tests
15+ // TODO: Is this robust and sufficient enough to support my needs?
1316
1417public class Container {
15- var factories : ThreadSafeDictionary < FactoryKey , any FactoryRegistrable > = . init( )
18+ var factories : ThreadSafeDictionary < ProductKey , any Registrable > = . init( )
1619
1720 public init ( ) { }
1821
19- public func registerAsync< Product> ( _ productType: Product . Type , name: String ? = nil , factory block: @escaping ( Resolver ) async -> Product ) {
20- let factoryKey = FactoryKey ( productType: productType, name: name)
22+ @discardableResult
23+ public func registerAsync< Product> ( _ productType: Product . Type , name: String ? = nil , factory block: @escaping ( Resolver ) async -> Product ) -> Registration < Product > {
24+ let factoryKey = ProductKey ( productType: productType, name: name)
2125 let factory = Factory . async ( block)
22- let registration = FactoryRegistration ( factory: factory)
26+ let registration = Registration ( factory: factory, container : self )
2327 factories. insert ( registration, for: factoryKey)
28+ return registration
2429 }
2530
26- public func register< Product> ( _ productType: Product . Type , name: String ? = nil , factory block: @escaping ( Resolver ) -> Product ) {
27- let factoryKey = FactoryKey ( productType: productType, name: name)
31+ @discardableResult
32+ public func register< Product> ( _ productType: Product . Type , name: String ? = nil , factory block: @escaping ( Resolver ) -> Product ) -> Registration < Product > {
33+ let factoryKey = ProductKey ( productType: productType, name: name)
2834 let factory = Factory . sync ( block)
29- let registration = FactoryRegistration ( factory: factory)
35+ let registration = Registration ( factory: factory, container : self )
3036 factories. insert ( registration, for: factoryKey)
37+ return registration
3138 }
3239}
3340
3441extension Container : Resolver {
3542 public func resolve< Product> ( _ productType: Product . Type , name: String ? ) -> Product ? {
3643 let registration = registration ( for: productType, with: name)
37- let product = registration? . factory . make ( self )
44+ let product = registration? . resolve ( )
3845 return product
3946 }
4047
4148 public func resolveAsync< Product> ( _ productType: Product . Type , name: String ? ) async -> Product ? {
4249 let registration = registration ( for: productType, with: name)
43- let product = await registration? . factory . makeAsync ( self )
50+ let product = await registration? . resolveAsync ( )
4451 return product
4552 }
4653
47- func registration< Product> ( for productType: Product . Type , with name: String ? ) -> FactoryRegistration < Product > ? {
48- let factoryKey = FactoryKey ( productType: productType, name: name)
49- let registration = factories. getValue ( for: factoryKey) as? FactoryRegistration < Product >
54+ func registration< Product> ( for productType: Product . Type , with name: String ? ) -> Registration < Product > ? {
55+ let factoryKey = ProductKey ( productType: productType, name: name)
56+ let registration = factories. getValue ( for: factoryKey) as? Registration < Product >
5057 return registration
5158 }
5259}
0 commit comments