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

Dagger and Koin

Dagger and Koin

Dagger, Koin, Dependency Injection, Service Locator

Eoin Fogarty

June 30, 2019
Tweet

More Decks by Eoin Fogarty

Other Decks in Programming

Transcript

  1. # Introduction - Eoin Fogarty - Ex-Engineer at Cyberagent. -

    Android and Flutter developer @_modge_ https://github.com/eoinfogarty
  2. # But First... Whats is the aim of us doing

    this? - Dependency Inversion - Inversion of Control
  3. # Dependency Inversion The D in SOLID. High-level modules should

    not depend on low-level modules. Both should depend on abstractions (e.g. interfaces).
  4. # Inversion of Control Remove dependencies from your code. https://stackoverflow.com/a/3140

    class TextEditor { private val checker: SpellChecker init { // this is not testable! this.checker = SpellChecker() } }
  5. # Inversion of Control Remove dependencies from your code. https://stackoverflow.com/a/3140

    class TextEditor( // control over implementation has be inverted and is not outside the class val checker: IocSpellChecker )
  6. # Inversion of Control Remove dependencies from your code. https://stackoverflow.com/a/3140

    class SpellChecker : IocSpellChecker { ... } val spellChecker = SpellChecker() // dependency val textEditor = TextEditor(spellChecker)
  7. # Providing Field Dependencies • Service Locator val spellChecker =

    Locator.get(SpellChecker::class) • Dependency Injection @Inject lateinit var spellChecker: SpellChecker
  8. # Service Locator have an object that knows how to

    get hold of all of the services that an application might need
  9. # Dependency Injection an assembler, that populates a field in

    the lister class with an appropriate implementation for the finder interface
  10. # Both.. - Provide fundamental decoupling - Application code is

    independent of the concrete implementation. The differences is how the implementation is provided. - Service Locator: the class asks for it. - Every class has a dependency to the locator. - Injection: there is no explicit request.
  11. # So .. .. the decision between locator and injector

    depends on whether that dependency is a problem. https://martinfowler.com/articles/injection.html
  12. # Implementation Koin vs Dagger Fields // with Koin private

    val viewModel: MainViewModel by inject() // with Dagger @Inject lateinit var viewModel: MainViewModel With dagger there explicit request, so we cannot declare it a val Since it is provided from outside, it cannot be private.
  13. # Implementation Koin vs Dagger Constructors // with Koin class

    DiskCacheImpl(gson: Gson) : DiskCache // with Dagger class DiskCacheImpl @Inject constructor(gson: Gson) : DiskCache @Inject violates separation of concerns. Impl should not know its being injected.
  14. # Other Downsides of Dagger - Steep learning curve for

    beginners. - Complicated, especially wanting to read generated code. - Annotation Processing increasing build times. - Lots of boiler plate code setting up components and modules and binder factories. - Setting up graphs for testing is difficult.
  15. # So Koin! Probably not… Dagger2: - Very fast at

    runtime. - Compile time validation. - Benefits increase with scale. - A lot of daggers problems (complexity, learning curve) are daggers and not the pattern it implements.
  16. # In Conclusion Probably … Koin is good if you

    are new or are learning. Koin is good to get started quickly. As you scale, Dagger is technically the better choice..