Last active
April 13, 2020 14:52
-
-
Save robertofrontado/632e68af049659a7b9a2baf33cfeb3cf to your computer and use it in GitHub Desktop.
SwiftDI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public class SwiftDI { | |
public static let `default` = SwiftDI() | |
private var dependencies = [String: Resolver]() | |
public static func configure(container: SwiftDI = .default, _ config: (SwiftDI) -> Void) { | |
config(container) | |
} | |
public func single<T>(_ single: () -> T) { | |
let key = String(describing: T.self) | |
dependencies[key] = Resolver.single(object: single()) | |
} | |
public func factory<T>(_ factory: @escaping () -> T) { | |
let key = String(describing: T.self) | |
dependencies[key] = Resolver.factory(block: factory) | |
} | |
public func resolve<T>() -> T { | |
let key = String(describing: T.self) | |
guard let object = dependencies[key]?.resolve() as? T else { | |
fatalError("Dependency '\(T.self)' not resolved!") | |
} | |
return object | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment