Skip to content

Instantly share code, notes, and snippets.

@arnaudgiuliani
Created December 13, 2017 10:51
Show Gist options
  • Select an option

  • Save arnaudgiuliani/715b2db73f592403b424dd038457c00f to your computer and use it in GitHub Desktop.

Select an option

Save arnaudgiuliani/715b2db73f592403b424dd038457c00f to your computer and use it in GitHub Desktop.
Koin sample Kotlin application
// A model class
class HelloModel(val who: String = "Koin")
// Service interface
interface HelloService {
fun hello(): String
}
// Service implementation with injected helloModel instance
class HelloServiceImpl(val helloModel: HelloModel) : HelloService {
override fun hello() = "Hello ${helloModel.who}"
}
// HelloApplication
class HelloApplication : KoinComponent {
// Inject service
val helloService by inject<HelloService>()
init {
println(helloService.hello())
}
}
// Koin module
val HelloModule = applicationContext {
provide { HelloModel() }
provide { HelloServiceImpl(get()) as HelloService }
}
fun main(vararg args: String) {
startKoin(listOf(HelloModule))
HelloApplication()
// will print "Hello Koin"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment