1. Overview
First you need to decide which kind of ProvidableKey
you want. A plain String
might be working, but using the GenericProvidableKey
with a typealias will be just fine.
enum ServicesKeyType { } // will never be instantiated
typealias Services = GenericProvidableKey<Services>
let serviceInjector = LazyInjector<Services>().globalize().erase()
serviceInjector // only Services will fit in here
You can use a globalized LazyInjector
to provide your global services. This injector will be passed to every ViewController
. Thereafter we erase the Injector
in order to get rid of generics (Injector where Key = MyKey
vs. AnyInjector<MyKey>
).
// this will be passed to your RootViewController
let appInjector = LazyInjector<Services>().globalize().erase()
On segues, you can compose the Injector
the ViewController
received (for your RootViewController: appInjector
).
protocol SessionService { }
extension Provider {
static var sessionService: Provider<Services, SessionService> {
return .derive()
}
}
// then
var segueInjector = StrictInjector<Services>()
segueInjector.inject(mySession, for: .sessionService)
let navInjector = segueInjector.compose(appInjector)
// All changes will now be performed on segueInjector by default,
// but contents of appInjector are available, too