Created
June 20, 2023 19:42
-
-
Save ivanopcode/89ebd83ef9f32790fee2125f5080ef1b to your computer and use it in GitHub Desktop.
Swinject-based DI container builder template
This file contains hidden or 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
// MIT | |
// by Alexey Grigorev, Ivan Oparin | |
import Swinject | |
class DIContainerBuilder { | |
public static func build() -> Container { | |
let container = Container() | |
return container | |
} | |
private static func utilsModule(container: Container) { | |
} | |
private static func settingsModule(container: Container) { | |
// stub | |
} | |
private static func persistenceModule(container: Container) { | |
// stub | |
} | |
} | |
typealias F = ObjectFactory | |
class ObjectFactory { | |
private static var defaultContainer: Container? | |
public static func initialize(with container: Container) { | |
defaultContainer = container | |
} | |
public static func deInitialize() { | |
defaultContainer = nil | |
} | |
public static func get<Service>(type: Service.Type, name: String? = nil) -> Service? { | |
if let name = name { | |
return defaultContainer?.synchronize().resolve(type, name: name) | |
} | |
return defaultContainer?.synchronize().resolve(type) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment