Skip to content

Instantly share code, notes, and snippets.

@ivanopcode
Created June 20, 2023 19:42
Show Gist options
  • Save ivanopcode/89ebd83ef9f32790fee2125f5080ef1b to your computer and use it in GitHub Desktop.
Save ivanopcode/89ebd83ef9f32790fee2125f5080ef1b to your computer and use it in GitHub Desktop.
Swinject-based DI container builder template
// 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