Slide 1

Slide 1 text

Drugs vs Money or Idiomatic DI with Kotlin

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Dependency Injection

Slide 4

Slide 4 text

Service Locator Design pattern used to encapsulate the processes involved in obtaining a dependency Acts like a simple runtime linker

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Dependency Injection vs Service Locator Both solve the problem of decoupling our code Code does not instantiate concrete implementations

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

“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.”

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Koin Small, pragmatic, concise Can only be used with Kotlin Simple natural API

Slide 12

Slide 12 text

Kodein Can be used on JVM, Android, JS and Native Robust Has tons of features More verbose, complicated API

Slide 13

Slide 13 text

So, choose wisely or… Create your own solution!

Slide 14

Slide 14 text

Demo https://github.com/DmitriyZaitsev/kontainer

Slide 15

Slide 15 text

Thank you! @DmitriyZaitsev