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 will have to inject it into a runtime or particular app platform (Android, SparkKotlin …). You have two ways of injecting into your runtime:

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

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

What’s next?

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