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

Ciklum & GDG Dnipro - Dagger 2

Royce Mars
November 30, 2017

Ciklum & GDG Dnipro - Dagger 2

Talk about Dagger 2 basics, dependency injection basics and alternative tools, and best practices of using Dagger 2 for Android Java projects, Dagger and Koin for Kotlin

Royce Mars

November 30, 2017

Transcript

58 slides
  1. See dependencies in code • Car depends on Engine. •

    Engines may vary. • Need different engines for testing and production
  2. Evolution of Dagger • Dagger 2 - Google, Greg Kick

    • Dagger - Square, Jake Wharton • Guice - Google, Jesse Wilson
  3. And again - Components & Modules Pic. by Miroslaw Stanek

    from Azimo http://frogermcs.github.io/dagger-graph-creation-performance/
  4. So… what should we inject? • Demo data sources /

    modules • Presenters • Singletons • Test implementations • All other dependencies :)
  5. Favor constructor injection over field injection • Field injection requires

    non final and non private fields • Forget an @Inject - get NullPointerException on a field • Constructor injection = immutable and thread safe objects. Protect from partially constructed state • For objects, constructed by the system (Activities, Fragments) - still use field injection
  6. Favor @Inject over @Provides • @Provides methods should not duplicate

    the constructor boilerplate • Code is easier to understand when coupled concerns are in one place
  7. Singletons should be extremely rare • Singletons are useful when

    we need a centralized access to a mutable state • Immutable objects doesn’t need to be a singleton
  8. Favor static @Provides methods • The generated code can directly

    invoke the method instead of having to create a module instance. That method call can be inlined by the compiler • All bindings being static will result in a sizable performance increase • Make your modules abstract and Dagger will fail at compile time if one of the @Provides methods isn’t static
  9. Home readings for Java • Py “Keeping the Daggers Sharp”

    • Mert Simsek “New Android Injector with Dagger 2” • James Shvarts “Demystifying the new Dagger Android Injection API” • Fernando Cejas “Tasting Dagger 2 on Android” • Miroslaw Stanek “Dagger2 - graph creation performance” • CodePath’s guide to Dagger2 • Xoxol_89 @ Habrahabr “Dagger 2. Часть третья. Новые грани возможного” • Dagger2 official page
  10. Home readings for Kotlin Mobile Architecture Club • Juan Ignacio

    Saravia, Keddit https://android.jlelse.eu/keddit-part-10-kotlin-dagger- 2-dependency-injection-a620052a7384 • Antonio Leiva https://antonioleiva.com/dagger-android-kotlin/ • Koin https://github.com/Ekito/koin

More Decks by Royce Mars

Other Decks in Programming