Minimalist dependency injection mechanism written in pure Swift.
Support any first party Swift platfom. [ iOS / iPadOS / watchOS / macOS / Linux ]
Well tested. Used in production across the Infomaniak iOS Apps.
Property wrapper based with @LazyInjectService
and @InjectService
.
Optimised to work well with SwiftUI Views.
- Efficient SwiftUI.
@(Lazy)InjectService
will not re-resolve, when used as a property, on aView
redraw. - Good integration test coverage
- Pure Swift Type Support
- Thread safety
- Injection by name
- Injection with Arguments
- Lazy init (with @LazyInjectService)
- Optionals
- Multiple containers
- Swift 5.6 +
- SPM
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: SomeService
Injection 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.
Checkout ITSimpleReslover.swift
for more advanced examples.
This package is available under the permissive ApacheV2 licence for you to enjoy.