> ## Content Index
> Fetch the complete content index at: https://lucas-schiavini.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Daily Dispatch #5 - Dependency Injection
- URL: https://lucas-schiavini.com/daily-dispatch-5-dependency-injection/
- Published: 2022-02-10T15:02:41.000Z
- Updated: 2022-08-16T14:40:02.000Z
- Author: Lucas Schiavini
- Tags: dailydispatch, #Import 2026-08-11 14:49

Classes shouldn’t have to fetch their dependencies, you should get a third party module to link implementations with it’s respective interfaces. That way you can change easily the implementations if need be.

Two libraries used for dependency injection are [Koin](https://github.com/InsertKoinIO/koin?ref=lucas-schiavini.com)(7.3k stars on GitHub as the time of this post) and [Kodein-DI](https://github.com/Kodein-Framework/Kodein-DI?ref=lucas-schiavini.com)(2.7k stars on GitHub as the time of this post). The main difference that I see is that [Kodein-DI](https://github.com/Kodein-Framework/Kodein-DI?ref=lucas-schiavini.com) works for multi platform apps on Kotlin, however both share many similarities and if your code is neat, you can replace one for the other.

With [Koin](https://github.com/InsertKoinIO/koin?ref=lucas-schiavini.com) here’s how it works. 

**Declaring component’s definitions**

// Use the Koin DSL to describe your components definitions
// Given some classes 
class Controller(val service: BusinessService) 
class BusinessService() 
// just declare it 
val myModule = module { 
  single { Controller(get()) }
  single { BusinessService() }
}

**Injecting Constructors**

// Instances are resolved from your modules

// Controller & BusinessService are declared in a module
class Controller(val service: BusinessService) { 
  
  fun hello() {
    // service is ready to use
    service.sayHello()
  }
} 

**Start koin**

//Just run startKoin function in your application
fun main(vararg args: String) { 
  // start Koin!
  startKoin {
    // declare modules
    modules(myModule)
  }
} 

**Injecting**

// Just inject in a simple Activity 
class MyActivity() : AppCompatActivity() {
  // inject BusinessService into property
  val service: BusinessService by inject()
}

---

Now for [Kodein-DI](https://github.com/Kodein-Framework/Kodein-DI?ref=lucas-schiavini.com) , I’m still trying to make it work.