Flipping the Koin

Flipping the Koin

Slides of the talk I gave at Kotlin/Everywhere in Stockholm, St. Petersburg and Milan

6923bdeb363961b064d2cdb6329982d6?s=128

Roberto Orgiu

November 27, 2019
Tweet

Transcript

  1. R O B E R T O O R G

    I U - @ _ t i w i z Flippin’ the Koin
  2. Is there a difference? IMPLEMENTATION DI vs SL

  3. DI Container Should only call itself Never called by others

    Constructs objects
  4. Service Locator Container Dependency

  5. I’m not really answering this question WHAT IS DAGGER?

  6. @Inject lateinit var myTarget: Target myComponent.inject(this) val myTarget = myComponent.getTarget()

  7. insert-koin.io

  8. Why I like it Kotlin DSL Quick setup Easy to

    follow
  9. I’m not really answering this question either WHAT IS KOIN?

  10. private val target: Target by inject() inline fun <reified T

    : Any> ComponentCallbacks.inject()
  11. modules declaration val myModule = module { single<Target> { TargetImpl()

    } factory { (view: View) -> Presenter(view, get() } viewModel { MyViewModel(get()) } }
  12. Injection private val target: Target by inject { parametersOf(this) }

  13. Injection private val target: Target by inject { parametersOf(this) }

  14. Injection private val target: Target by inject { parametersOf(this) }

  15. Injection val viewModel: MyViewModel by viewModel()

  16. Injection class MyWorker : KoinComponent { override suspend fun doWork():

    Result { val target: Target by inject() ... } }
  17. Application startKoin { androidContext(context) androidLogger() modules( listOf(module1,..., moduleN) ) }

  18. Let’s talk about testing “

  19. JUnit + KoinTest

  20. Dependencies koinApplication { androidContext(context) modules(fetchModules()) }.checkModules()

  21. Dependencies koinApplication { androidContext(context) modules(fetchModules()) }.checkModules { create<MyClass> { parametersOf(mock1,...,paramN)

    } }
  22. Building a test

  23. startKoin class MyTest : KoinTest { @Before fun setup() {

    startKoin { androidContext(context) modules(fetchModules()) } } }
  24. startKoin class MyTest : KoinTest { @Before fun setup() {

    startKoin { androidContext(context) modules(fetchModules()) } } }
  25. startKoin stopKoin class MyTest : KoinTest { @After fun tearDown()

    { stopKoin() } }
  26. startKoin stopKoin class MyTest : KoinTest { @After fun tearDown()

    { stopKoin() } }
  27. startKoin stopKoin Test

  28. class MyTest : KoinTest { @Before fun setup() { startKoin

    {...} } @Test fun my_test() { } }
  29. class MyTest : KoinTest { @Before fun setup() { startKoin

    {...} } @Test fun my_test() { } } private val target: Target by inject() declareMock<Target>() given(target).will { throw Exception() }
  30. Dagger VS Koin

  31. WARNING: Opinionated Ideas ahead

  32. We should use Dagger

  33. Quick setup

  34. Compile time safety Because we all love it.

  35. Scalability

  36. Maintainability

  37. Any questions? “

  38. Use the tools You Like the most

  39. Thanks!