winter / io.jentz.winter / WinterApplication

WinterApplication

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)
  }

}

Types

InjectionAdapter

interface InjectionAdapter

Adapter interface to provide application specific graph creation and retrieval strategy.

Constructors

<init>

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.

Properties

checkForCyclicDependencies

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.

component

var component: Component

The application component.

graph

val graph: Graph

Get the application graph.

graphOrNull

var graphOrNull: Graph?

Get the application graph if open otherwise null.

injectionAdapter

var injectionAdapter: WinterApplication.InjectionAdapter?

The application injection adapter.

plugins

var plugins: Plugins

The plugins registered on the application.

Functions

closeGraph

fun closeGraph(): Unit

Close the application graph.

closeGraphIfOpen

fun closeGraphIfOpen(): Unit

Close the application graph if open otherwise do nothing.

component

fun component(qualifier: Any = ApplicationScope::class, block: ComponentBuilderBlock): Unit

Sets the application component by supplying an optional qualifier and a component builder block.

createGraph

fun createGraph(block: ComponentBuilderBlock? = null): Graph

Create graph from component without registering it as application graph.

getOrOpenGraph

fun getOrOpenGraph(block: ComponentBuilderBlock? = null): Graph

Get application graph if already open otherwise open and return it.

inject

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.

openGraph

fun openGraph(block: ComponentBuilderBlock? = null): Graph

Open the application component.

Extension Functions

useApplicationGraphOnlyAdapter

fun WinterApplication.useApplicationGraphOnlyAdapter(): Unit

Register an ApplicationGraphOnlyInjectionAdapter on this WinterApplication instance.

Inheritors

Winter

object Winter : WinterApplication

The default WinterApplication object.