Created
June 19, 2023 13:30
-
-
Save satishVekariya/cb32bd39b0cceda3f177088141c47cb1 to your computer and use it in GitHub Desktop.
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 Swinject /// https://github.com/Swinject/Swinject.git | |
import Foundation | |
@propertyWrapper | |
public struct Inject<Dependency> { | |
public let wrappedValue: Dependency | |
public init(resolver: Resolver = AppAssembler.shared.resolver) { | |
wrappedValue = resolver.resolve(Dependency.self)! | |
} | |
} | |
// Source: | |
// - https://ali-akhtar.medium.com/ios-dependency-injection-using-swinject-9c4ceff99e41 | |
// - https://github.com/Swinject/Swinject/blob/master/Documentation/Assembler.md | |
public final class AppAssembler { | |
/// Shared app assembler | |
public static let shared = AppAssembler() | |
private init() {} | |
private var assembler: Assembler! | |
var container = Container() | |
/// Clear dependency graph and remove assembler | |
public func clearAll() { | |
container = .init() | |
assembler = nil | |
} | |
/// Configure all registered assembly using `Assembler`. You must call this method on app launch or before resolving any dependencies | |
/// | |
/// After this call you can resolve your dependencies | |
/// | |
/// Example: | |
/// ``` | |
/// @Inject var myService: MyService | |
/// | |
/// OR | |
/// | |
/// let myService = AppAssembler.shared.resolver.resolve(MyService.self) | |
/// ``` | |
/// - Parameter assemblies: List of your Assemblies | |
public func configure(_ assemblies: Assembly...) { | |
assembler = Assembler(assemblies, container: container) | |
} | |
/// Synchronized view of the container for thread safety. | |
public var resolver: Resolver { | |
container.synchronize() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment