koin

Project Url: Ekito/koin
Introduction: KOIN - a smart Kotlin dependency injection framework
More: Author   ReportBugs   
Tags:

logo

Koin is a small library to lets you write dependency injection in a concise and pragmatic way. No proxy, no code generation, no introspection. Just DSL and functional Kotlin magic!

Declare, Start & Inject

insert-koin.io

All documentation, sample and references has been move to our website. Check the official website to get started: insert-koin.io

Koin samples have been moved here: koin-samples @ github

Getting Started

Actual Version

koin_version = '0.8.0'

Gradle

Check that you have the jcenter repository.

// Add Jcenter to your repositories if needed
repositories {
    jcenter()    
}

Choose your the Koin module for your runtime:

// Koin for Kotlin
compile "org.koin:koin-core:$koin_version"

// Koin for Android
compile "org.koin:koin-android:$koin_version"

// Koin for Android Architecture Components
compile "org.koin:koin-android-architecture:$koin_version"

// Koin for Spark Kotlin
compile "org.koin:koin-spark:$koin_version"

Check others modules (Ktor, JUnit ...) on getting started web page

Declare

Write a module with what you want to declare and assemble:

// Given some classes 
class Controller(val service : BusinessService) 
class BusinessService() 

// just declare it 
val myModule = applicationContext { 
  provide { Controller(get()) } 
  provide { BusinessService() } 
}

Start

Use the startKoin() function to start Koin with your modules in your application. Below some start examples.

Kotlin:

fun main(vararg args : String) { 
  // start Koin!
  startKoin(listOf(myModule))
}

Android:

class MyApplication : Application() {
  override fun onCreate(){
    super.onCreate()
    // start Koin!
    startKoin(this, listOf(myModule))
  } 
}

Inject

You're ready to go! Components declared in modules are injected by constructors.

class Controller(val service : BusinessService){ 
  // service has been injected 
}

Inject into Android Activity:

// Just a simple Activity - No need of interface nor annotation 
class MyActivity() : AppCompatActivity() {

    // lazy inject BusinessService
    val service : BusinessService by inject()
}

Get your Android Architecture ViewModel:

// MyViewModel must be previously declared with 'viewModel'
val module = applicationContext{
  viewModel { MyViewModel(get())}
  //...
}

// Your ViewModel
class MyViewModel(val service : BusinessService) : ViewModel() {
  // do antyhing with service
}

// Bind it to your Activity
class MyActivity() : AppCompatActivity() {
  override fun onCreate(){
    super.onCreate()
    val viewModel = getViewModel<MyViewModel>()
  }
}

A Spark HTTP Controller:

// Declare your controller
val module = applicationContext {
  controller { HelloController(get())}
  //...
}

// Your Spark HTTP Controller
class HelloController(val service: HelloService) {
  init {
      get("/hello") {
          service.sayHello()
      }
  }
}

fun main(vararg args: String) {
  // Spark
  startSpark {
      // Koin
      startKoin(listOf(helloAppModule))
      // Run all Controllers
      runControllers()
  }
}

Check the getting started sections for more details.

Follow us & Contact

Twitter - @insertkoin_io

Medium - Koin Developers Hub

Slack - Kotlin Slack on #koin channel

Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea