object Injection : WinterInjection
Abstraction to create, get and dispose a dependency graph from a class that can't make use of constructor injection. This takes the burden off of the class to know how exactly a graph or parent graph is stored and how to create and store a new graph.
An application specific graph creation and retrieval strategy can be provided by setting a custom Adapter.
Injection.adapter = MyCustomAdapter()
To use this abstraction in a library it is recommended to create a library specific object from WinterInjection.
Example using the SimpleAndroidInjectionAdapter which is part of the winter-android module:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// register component
Winter.component {
singleton<GitHubApi> { GitHubApiImpl() }
singleton { RepoListViewModel(instance()) }
subcomponent("activity") {
singleton { Glide.with(instance<Activity>()) }
}
}
// register adapter
Injection.useSimpleAndroidAdapter()
// create root graph
Injection.createGraph(this)
}
}
class MyActivity : Activity() {
private val injector = Injector()
private val viewModel: RepoListViewModel by injector.instance()
override fun onCreate(savedInstanceState: Bundle?) {
Injection.createGraphAndInject(this, injector)
super.onCreate(savedInstanceState)
}
override fun onDestroy() {
Injection.disposeGraph(this)
super.onDestroy()
}
}
See Also
var adapter: WinterInjection.Adapter
Set the application specific adapter. |
fun createGraph(instance: Any, block: ComponentBuilderBlock? = null): Graph
Create and return dependency graph for instance. |
|
fun createGraphAndInject(instance: Any, injector: Injector, block: ComponentBuilderBlock? = null): Graph
Create and return dependency graph for instance and also pass the graph to the given injector. fun <T : Any> createGraphAndInject(instance: T, injectSuperClasses: Boolean = false, block: ComponentBuilderBlock? = null): Graph
Create and return dependency graph for instance and inject all members into instance. |
|
fun disposeGraph(instance: Any): Unit
Dispose the dependency graph of the given instance. |
|
fun getGraph(instance: Any): Graph
Get dependency graph for instance. |
|
fun inject(instance: Any, injector: Injector): Unit
Get dependency graph for given instance and inject dependencies into injector. fun <T : Any> inject(instance: T, injectSuperClasses: Boolean = false): Unit
Inject into instance by using the dependency graph of the instance. This uses MembersInjector and is useful in conjunction with Winters JSR330 annotation processor. |
fun WinterInjection.useApplicationGraphOnlyAdapter(tree: WinterTree = GraphRegistry): Unit fun WinterInjection.useApplicationGraphOnlyAdapter(application: WinterApplication): Unit
Register an ApplicationGraphOnlyAdapter on this WinterInjection instance. |