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!
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
You can check the getting started section from our website, to discover Koin with the favorite platofrm. Or follow the snippets below.
koin_version = '0.8.1'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
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() }
} 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))
}
} SparkKotlin:
fun main(vararg args : String) {
// start Spark & Koin
start( modules = listOf(myModule)){
runControllers()
}
} 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()
}Inject your Android 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() {
val viewModel : MyViewModel by getViewModel()
override fun onCreate(){
super.onCreate()
}
}Start 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()
}
}Go to the getting started sections for more details.