open class WinterApplication
Holds plugins, the application Component, the application Graph and offers an abstraction to inject dependencies into classes that cannot make use of constructor injection using an InjectionAdapter system.
The InjectionAdapter backed abstraction takes the burden off of the using class to know how exactly a graph or parent graph is constructed and stored.
An application specific graph creation and retrieval strategy can be provided by setting a custom InjectionAdapter.
Winter.adapter = MyCustomAdapter()
To use Winter in a library it is recommended to create a library specific object from WinterApplication for use in applications it is recommended to use the Winter object.
Example, using the SimpleAndroidInjectionAdapter which is part of the winter-androidx 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
Winter.useSimpleAndroidAdapter()
// open application graph
Winter.inject(this)
}
}
class MyActivity : Activity(), WinterAware {
private val viewModel: RepoListViewModel by inject()
override fun onCreate(savedInstanceState: Bundle?) {
Winter.inject(this)
super.onCreate(savedInstanceState)
}
}
interface InjectionAdapter
Adapter interface to provide application specific graph creation and retrieval strategy. |
WinterApplication(qualifier: Any = ApplicationScope::class, block: ComponentBuilderBlock)
Convenient constructor to configure the application Component during initialization. WinterApplication()
Holds plugins, the application Component, the application Graph and offers an abstraction to inject dependencies into classes that cannot make use of constructor injection using an InjectionAdapter system. |
var checkForCyclicDependencies: Boolean
If this is set to true, Winter will check for cyclic dependencies and throws an error if it encounters one. Without this check you will run in a StackOverflowError when you accidentally declared a cyclic dependency which may be hard to track down. |
|
var component: Component
The application component. |
|
val graph: Graph
Get the application graph. |
|
var graphOrNull: Graph?
Get the application graph if open otherwise null. |
|
var injectionAdapter: WinterApplication.InjectionAdapter?
The application injection adapter. |
|
var plugins: Plugins
The plugins registered on the application. |
fun closeGraph(): Unit
Close the application graph. |
|
fun closeGraphIfOpen(): Unit
Close the application graph if open otherwise do nothing. |
|
fun component(qualifier: Any = ApplicationScope::class, block: ComponentBuilderBlock): Unit
Sets the application component by supplying an optional qualifier and a component builder block. |
|
fun createGraph(block: ComponentBuilderBlock? = null): Graph
Create graph from component without registering it as application graph. |
|
fun getOrOpenGraph(block: ComponentBuilderBlock? = null): Graph
Get application graph if already open otherwise open and return it. |
|
fun inject(instance: Any): Unit
Inject dependencies into instance by using the dependency graph returned from InjectionAdapter.get called with instance. fun inject(instance: Any, target: Any): Unit
Inject dependencies with dependency graph from instance into target. This uses the dependency graph returned from InjectionAdapter.get called with instance. |
|
fun openGraph(block: ComponentBuilderBlock? = null): Graph
Open the application component. |
fun WinterApplication.useApplicationGraphOnlyAdapter(): Unit
Register an ApplicationGraphOnlyInjectionAdapter on this WinterApplication instance. |
object Winter : WinterApplication
The default WinterApplication object. |