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

Dependency Injection on Android

Karumi
June 12, 2016
50

Dependency Injection on Android

Slides used during the talk Dependency Injection on Android

Karumi

June 12, 2016
Tweet

Transcript

  1. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection on Android Pedro Vicente Gómez Sánchez Senior Android Developer at Karumi [email protected] @pedro_g_s github.com/pedrovgs
  2. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Alberto Gragera Technical Director Jorge Barroso Senior Android Developer Davide Mendolia Senior Full Stack Engineer Sergio Gutierrez Senior Full Stack Engineer
  3. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Agenda • Introduction. • What can I do using Dependency Injection? • Scoped Graphs. • Dagger on Android. • Dagger 2.
  4. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Introduction Inversion of Control vs Dependency Inversion vs Dependency Injection
  5. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Inversion of Control The natural evolution of the structured and imperative programming and the usage of reusable frameworks. The execution point where you software takes the control is not defined by your software.
  6. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Inversion One of the most important S.O.L.I.D principles. Depend on abstractions, forget about implementation details. Provide implementation details depending on abstractions can be tedious.
  7. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection Software Design Pattern used to facilitate the usage of Dependency Inversion. Provides dependencies given some previous configuration where abstractions are linked with concretions.
  8. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Why? The usage of IoC, Dependency Inversion and Dependency Injection provides give us the following benefits: • Testability. • Decoupling. • Modularity.
  9. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection Example The presenter inside this fragment has to be instantiated with all the dependency tree. Without a dependency injector this can be tedious.
  10. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection Example All the dependencies are passed in construction.
  11. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection Example The implementation details are part of the Dependency Injector configuration.
  12. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dependency Injection Example Now the Presenter is injected into the Fragment.
  13. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? • Do not link the class to the runtime lifecycle or frameworks. • Improve testability. • Decouple your code and improve our software design. • Detect code smells related to class dependencies and SRP violations. • Change the implementation details in build time. • Extract implementation details out of the business logic.
  14. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? Do not link the class to the runtime lifecycle.
  15. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? Detect code smells related to class dependencies and single responsibility principle violations.
  16. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? Change the implementation details in build time.
  17. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? Decouple your code and improve our software design. Using a Dependency Injector combined with the correct usage of Dependency Inversion we can easily develop the same feature at the same time in different teams and layers.
  18. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? Extract implementation details out of the business logic. Develop software being agnostic of the Framework or the Runtime used. Implementation details are provided using the dependency injector and your business logic does not depend on it.
  19. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    What can I do? The dependency container provide us different configurations to provide dependencies: • Singletons. • Lazy Initialization. • Provided dependencies. • Named dependencies. • Override dependencies already configured. • Custom qualifiers.
  20. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Scoped Graphs Dependencies provided by the Dependency Container or Object Graph inside the Application class are linked to the Application lifecycle. We need more than one graph to improve the Dependency Injector usage and performance. At the same time, some dependencies are needed just when some components like Activities or Custom Views are initialized.
  21. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Scoped Graphs Application Graph Activity A Graph Activity B Graph Create Activity A Create Activity B
  22. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 1. Add Dagger and Dagger Compiler as dependencies:
  23. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 2. Create an Application extension and configure the Object Graph:
  24. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 3. Configure Dagger Modules:
  25. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 4. Initialize your Application Object Graph:
  26. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 5. Create a method to extend the Application Object Graph:
  27. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger on Android Step 6. Create a BaseActivity with the code needed to extend the Application Graph.
  28. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Step 7. Create an abstract method requesting the Activity modules: Dagger on Android
  29. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Step 8. Extend the Application Object Graph using the Activity modules: Dagger on Android
  30. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Can you repeat, please? Dependency Injection on Android Workshop
  31. Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

    Dagger 2 • Configuration based on components not in graphs. • Generated code is now something you can read. • Reflection free. • 13 % faster. • Full graph validated in build time.