Thanks to visit codestin.com
Credit goes to github.com

Skip to content

alvince/koin

 
 

Repository files navigation

logo

What's KOIN?

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

You can check the getting started section from our website, to discover Koin with the favorite platofrm. Or follow the snippets below.

Actual Version

koin_version = '0.8.1'

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))
  } 
} 

SparkKotlin:

fun main(vararg args : String) { 
  // start Spark & Koin
  start( modules = listOf(myModule)){
  	runControllers()
  }
} 

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()
}

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.

Follow us & Contact

Twitter - @insertkoin_io

Slack - Kotlin Slack on #koin channel

About

KOIN - a pragmatic lightweight dependency injection framework for Kotlin

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 91.8%
  • CSS 7.2%
  • Other 1.0%