A Swift library for easy dependecy injection.
Using Swift Macros, SwiftInjectable allows you to easily make inject. Add the @Injectable(default: DefaultDependency.self)
to create an Injection key.
This is based on SwiftLee blog https://www.avanderlee.com/swift/dependency-injection/
dependencies: [
.package(url: "https://github.com/estampworld/swift-injectable.git", from: "0.0.1")
]
@Injectable(default: PokemonServiceAPI.self)
protocol PokeService {
func fetchPoke() async throws -> [String]
}
struct PokemonServiceAPI: PokeService {
func fetchPoke() async throws -> [String] {
[]
}
}
// Line use for limitation of using Extensions in Macros
@InjectableValues(name: PokeService.self)
extension InjectedValues { }
With a SwiftModifier
SwiftUI.DisplayView()
.dependecy(\.service, ServiceAPI() )
@Injected(\.Service) var service
- Eduardo Irias
- Twitter: @eduardo22i
- GitHub: eduardo22i