-
Notifications
You must be signed in to change notification settings - Fork 3
Inject Only
Andrey Pleshkov edited this page Mar 11, 2019
·
4 revisions
There're some cases when you want to create a service manually or barely don't control its creation (e.g. you use Storyboards).
All you need is to annotate your service with @saber.injectOnly
and invoke the generated injector manually.
Declare a container:
// @saber.container(AppContainer)
// @saber.scope(App)
protocol AppContaining {}
Create a very simple service:
// @saber.scope(App)
// @saber.cached
class UserManager {
var currentUserName: String {
return "Steve"
}
}
Then annotate your ViewController
, so Saber will generate an exposed injector to invoke manually:
// @saber.scope(App)
// @saber.injectOnly
class ViewController: UIViewController {
// @saber.inject
var userManager: UserManager!
}
Run Saber to generate the AppContainer
and expose it somewhere in your code, e.g. in AppDelegate.swift
:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let appContainer = AppContainer() // HERE!
// ...
}
Now we can use the generated injector:
// @saber.scope(App)
// @saber.injectOnly
class ViewController: UIViewController {
// @saber.inject
var userManager: UserManager!
override func awakeFromNib() {
super.awakeFromNib()
let appContainer = (UIApplication.shared.delegate as! AppDelegate).appContainer
appContainer.injectTo(viewController: self) // HERE!
print(userManager.currentUserName) // Steve
}
}
- User's Guide
- Advanced