Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flipping the Koin

Flipping the Koin

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

Roberto Orgiu

November 27, 2019
Tweet

More Decks by Roberto Orgiu

Other Decks in Programming

Transcript

  1. R O B E R T O O R G I U - @ _ t i w i z
    Flippin’ the Koin

    View Slide

  2. Is there a difference?
    IMPLEMENTATION
    DI vs SL

    View Slide

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

    View Slide

  4. Service Locator
    Container Dependency

    View Slide

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

    View Slide

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

    View Slide

  7. insert-koin.io

    View Slide

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

    View Slide

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

    View Slide

  10. private val target: Target
    by inject()
    inline fun
    ComponentCallbacks.inject()

    View Slide

  11. modules
    declaration
    val myModule = module {
    single { TargetImpl() }
    factory { (view: View) ->
    Presenter(view, get() }
    viewModel { MyViewModel(get()) }
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. Injection
    val viewModel: MyViewModel by viewModel()

    View Slide

  16. Injection
    class MyWorker : KoinComponent {
    override suspend fun doWork(): Result {
    val target: Target by inject()
    ...
    }
    }

    View Slide

  17. Application
    startKoin {
    androidContext(context)
    androidLogger()
    modules(
    listOf(module1,..., moduleN)
    )
    }

    View Slide

  18. Let’s talk
    about testing

    View Slide

  19. JUnit + KoinTest

    View Slide

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

    View Slide

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

    View Slide

  22. Building a test

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. startKoin stopKoin Test

    View Slide

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

    View Slide

  29. class MyTest : KoinTest {
    @Before
    fun setup() {
    startKoin {...}
    }
    @Test
    fun my_test() {
    }
    }
    private val target: Target by inject()
    declareMock()
    given(target).will { throw Exception() }

    View Slide

  30. Dagger
    VS
    Koin

    View Slide

  31. WARNING:
    Opinionated
    Ideas ahead

    View Slide

  32. We should use
    Dagger

    View Slide

  33. Quick setup

    View Slide

  34. Compile time
    safety
    Because we all love it.

    View Slide

  35. Scalability

    View Slide

  36. Maintainability

    View Slide

  37. Any
    questions?

    View Slide

  38. Use the tools
    You
    Like the most

    View Slide

  39. Thanks!

    View Slide