Created
December 13, 2017 10:51
-
-
Save arnaudgiuliani/715b2db73f592403b424dd038457c00f to your computer and use it in GitHub Desktop.
Koin sample Kotlin application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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