SwinjectLight is framework for lightweight dependency injection which works on any platform supporting Swift. Framework took inspiration from original Swinject implementation but removes some more complex functionality which is usually not necessary.
To add a package dependency to your Xcode project, select File > Add Package Dependency and https://github.com/marekpridal/SwinjectLight
.
Alternatively in Package.swift
add the following
// swift-tools-version:5.10
import PackageDescription
let package = Package(
name: "SwinjectLightExample",
dependencies: [
.package(url: "https://github.com/marekpridal/SwinjectLight", from: "1.0.0")
],
targets: [
.target(name: "SwinjectLightExample", dependencies: ["SwinjectLight"])
]
)
import SwinjectLight
// Create container
let container = Container()
// Register singleton dependency
container.register(Session.self) { r in
DefaultSession.shared
}
// Register instance based dependency
container.register(Api.self) { r in
Networking(session: r.resolve(Session.self))
}
// Resolve dependency
let api = container.resolve(Api.self)
You can also check out demo project in repo for further details about usage.