$30 off During Our Annual Pro Sale. View Details »

Dagger 2 - Ciklum Speaker's Corner

Dagger 2 - Ciklum Speaker's Corner

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 touched only very quickly

Constantine Mars

November 30, 2017
Tweet

More Decks by Constantine Mars

Other Decks in Programming

Transcript

  1. Dagger 2 Manage dependencies in Google style Constantine Mars Team

    Lead, Senior Developer @ DataArt
  2. What is dependency injection?

  3. Composition != inheritance Member variables need to be initialized

  4. Creating objects with “new” keyword

  5. Initializing new objects Via constructor parameters

  6. Composition. The simplest example

  7. Applying inheritance and composition When creating new objects

  8. See dependencies in code • Car depends on Engine. •

    Engines may vary. • Need different engines for testing and production
  9. Dagger and JSR-330

  10. Evolution of Dagger • Dagger 2 - Google, Greg Kick

    • Dagger - Square, Jake Wharton • Guice - Google, Jesse Wilson
  11. Inversion of control

  12. Reflection vs Compile time Dagger 2 vs Dagger 1

  13. JSR-330

  14. Simple piece of magic...

  15. Default constructor injection

  16. Injection through parameters

  17. So, how to make it work?

  18. How does injection work?

  19. Components & Modules Pic. by Miroslaw Stanek from Azimo http://frogermcs.github.io/dagger-graph-creation-performance/

  20. Module declaration

  21. Module annotations

  22. Component declaration

  23. Component annotations

  24. Component initialization

  25. Injection

  26. Adding modules

  27. Custom scopes

  28. Objects lifecycle Pic. by Miroslaw Stanek from Azimo http://frogermcs.github.io/dagger-graph-creation-performance/

  29. And again - Components & Modules Pic. by Miroslaw Stanek

    from Azimo http://frogermcs.github.io/dagger-graph-creation-performance/
  30. This mysterious “plus”...

  31. Declaring Subcomponent

  32. Adding Module instance

  33. Adding subcomponent

  34. Managing subcomponents lifetime

  35. Scope

  36. @ActivityScope

  37. @ActivityScope

  38. Custom scope

  39. Custom scope

  40. Custom scope

  41. So… what should we inject? • Demo data sources /

    modules • Presenters • Singletons • Test implementations • All other dependencies :)
  42. Best practices

  43. New Android Injector

  44. New Android Injector

  45. 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
  46. Favor constructor injection over field injection BAD:

  47. Favor constructor injection over field injection BETTER:

  48. Favor @Inject over @Provides • @Provides methods should not duplicate

    the constructor boilerplate • Code is easier to understand when coupled concerns are in one place
  49. Favor @Inject over @Provides - no need in provides method

    in Module
  50. 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
  51. Singletons should be extremely rare BAD: GOOD:

  52. 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
  53. Favor static @Provides methods BAD:

  54. Favor static @Provides methods

  55. Use Kotlin, insert Koin :)

  56. 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
  57. 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
  58. Thank you! :) @ConstantineMars @DataArt