Created
February 14, 2022 20:37
-
-
Save egabor/707df1e25919db25072709d8c0efb375 to your computer and use it in GitHub Desktop.
Dependency Injection snippets. [Swinject]
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 | |
import Swinject | |
class DependencyProvider { | |
static let shared = DependencyProvider() | |
private let container = Container() | |
let assembler: Assembler | |
private init() { | |
assembler = Assembler( | |
[ | |
// XyAssembly1(), | |
// XyAssembly2() | |
], | |
container: container | |
) | |
} | |
} |
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 | |
import Swinject | |
class RepositoryAssembly: Assembly { | |
func assemble(container: Container) { | |
container.register(<#Type#>.self) { _ in | |
<#Object()#> | |
} | |
.inObjectScope(<#ObjectScope#>) | |
} | |
} |
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
.package(url: "https://github.com/Swinject/Swinject", .exact("2.8.1")) |
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 | |
import Swinject | |
@propertyWrapper | |
struct Injected<T> { | |
private let resolver = (DependencyProvider.shared.assembler.resolver as! Container).synchronize() // swiftlint:disable:this force_cast | |
init() {} | |
lazy var wrappedValue: T = { | |
return resolver.resolve(T.self)! | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment