infomaniak/swift-dependency-injection
Minimalist dependency injection mechanism written in pure Swift.
Features
- [x] Efficient SwiftUI.
@(Lazy)InjectServicewill not re-resolve, when used as a property, on aViewredraw. - [x] Good integration test coverage
- [x] Pure Swift Type Support
- [x] Thread safety
- [x] Injection by name
- [x] Injection with Arguments
- [x] Lazy init (with @LazyInjectService)
Roadmap
- [ ] Optionals
- [ ] Multiple containers
Requirements
- Swift 5.6 +
- SPM
Setup
Early on in the lifecycle of your app, you want to write something like this :
import InfomaniakDI
[…]
let factory = Factory(type: SomeService.self) { _, _ in
SomeService()
}
do {
try SimpleResolver.sharedResolver.store(factory: factory)
}
catch {
FatalError("unexpected DI error \(error)")
}later on, when you want to resolve a service use the property wrapper like so:
@InjectService var injected: SomeServiceInjection will be performed at the init time of the owner of the property.
Use @LazyInjectService for resolution at first use of the wrapped property. Prefer @LazyInjectService when used as a property.
Documentation
Checkout ITSimpleReslover.swift for more advanced examples.
Licence
This package is available under the permissive ApacheV2 licence for you to enjoy.
Package Metadata
Repository: infomaniak/swift-dependency-injection
Default branch: main
README: README.md