Making dependency injection

How Koin is making dependency injection

Injection by constructor

Dependency injection is firstly made in your components, by declaring how to build them with the Koin DSL.

In this module:

val myModule = applicationContext {
    bean { MyComponentA() }
    bean { MyComponentB(get()) }
}

The MyComponentB class can use directly its dependency:

class MyComponentB(val componentA : MyComponentA) {
    // componentA has been injected

    fun callA() = componentA...
}

Injecting into runtime components

Once your Koin module is loaded, you have to inject it into a runtime or particular app platform (Android, SparkKotlin …). You have two ways of injecting it into your runtime:

  • Use an adapted Koin module, to help you to inject your module(s) into your runtime components (Activity, Fragments, Controller …)
  • Tag a component as KoinComponent

Upgraded runtimes and KoinComponents main target are to bring by inject() delegate instance getter, and other properties operators.

What’s next?

Check dedicated developer guides, to how to use dedicated koin modules.