Slides of the talk I gave at Kotlin/Everywhere in Stockholm, St. Petersburg and Milan
R O B E R T O O R G I U - @ _ t i w i zFlippin’ the Koin
View Slide
Is there a difference?IMPLEMENTATIONDI vs SL
DI ContainerShould only call itself Never called by othersConstructs objects
Service LocatorContainer Dependency
I’m not really answering this questionWHAT IS DAGGER?
@Injectlateinit var myTarget: TargetmyComponent.inject(this)val myTarget =myComponent.getTarget()
insert-koin.io
Why I like itKotlin DSL Quick setup Easy to follow
I’m not really answering this question eitherWHAT IS KOIN?
private val target: Targetby inject()inline fun ComponentCallbacks.inject()
modulesdeclarationval myModule = module {single { TargetImpl() }factory { (view: View) ->Presenter(view, get() }viewModel { MyViewModel(get()) }}
Injectionprivate val target: Targetby inject { parametersOf(this) }
Injectionval viewModel: MyViewModel by viewModel()
Injectionclass MyWorker : KoinComponent {override suspend fun doWork(): Result {val target: Target by inject()...}}
ApplicationstartKoin {androidContext(context)androidLogger()modules(listOf(module1,..., moduleN))}
Let’s talkabout testing“
JUnit + KoinTest
DependencieskoinApplication {androidContext(context)modules(fetchModules())}.checkModules()
DependencieskoinApplication {androidContext(context)modules(fetchModules())}.checkModules{create {parametersOf(mock1,...,paramN)}}
Building a test
startKoinclass MyTest : KoinTest {@Beforefun setup() {startKoin {androidContext(context)modules(fetchModules())}}}
startKoin stopKoinclass MyTest : KoinTest {@Afterfun tearDown() {stopKoin()}}
startKoin stopKoin Test
class MyTest : KoinTest {@Beforefun setup() {startKoin {...}}@Testfun my_test() {}}
class MyTest : KoinTest {@Beforefun setup() {startKoin {...}}@Testfun my_test() {}}private val target: Target by inject()declareMock()given(target).will { throw Exception() }
DaggerVSKoin
WARNING:OpinionatedIdeas ahead
We should useDagger
Quick setup
Compile timesafetyBecause we all love it.
Scalability
Maintainability
Anyquestions?“
Use the toolsYouLike the most
Thanks!