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

Drugs vs Money or Idiomatic DI with Kotlin

Drugs vs Money or Idiomatic DI with Kotlin

Slides from my talk at Kotlin Lviv User Group meetup, 2019-01-15

Sources from live coding session: https://github.com/DmitriyZaitsev/kontainer

Dmytro Zaitsev

January 15, 2019
Tweet

More Decks by Dmytro Zaitsev

Other Decks in Programming

Transcript

  1. Dependency Injection A technique for applying the principle of Inversion

    of Control The goal - to achieve separation of concerns in our code We want to use objects and don’t care about creating them
  2. Service Locator Design pattern used to encapsulate the processes involved

    in obtaining a dependency Acts like a simple runtime linker
  3. Dependency Injection vs Service Locator Both solve the problem of

    decoupling our code Code does not instantiate concrete implementations
  4. Dependency Injection vs Service Locator With DI, we don’t have

    direct requests for dependencies Dependencies are injected by the framework With SL, our code asks the locator for specific dependencies Every class that needs an injection has a dependency on the service locator
  5. Dagger 2 Fully static compile-time DI framework for Java Uses

    AP and codegen to build a dependency graph “If it compiles, it will work…’’’ Requires lots of boilerplate Adds up to the build time Generates lots of code. It may be an overkill for small projects
  6. “Just because big companies or your friends use a library,

    it doesn’t mean it will work for you. Evaluate it and then go for it.”
  7. Koin & Kodein Lightweight DI frameworks for Kotlin Work like

    Service Locator No proxy, codegen, introspection, annotation processing Leverage Kotlin features: delegates, reified generics, inline functions Simple declarative DSL with idiomatic Kotlin API Will crush at runtime if it can’t resolve a dependency
  8. Kodein Can be used on JVM, Android, JS and Native

    Robust Has tons of features More verbose, complicated API