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

Introduction to Dagger 2

Introduction to Dagger 2

An introduction covered with example codes to demonstrate the need and how to use in the simplest way possible the dependency injection framework "Dagger".

5cefbec62d834db21a3823a8e66d59d8?s=128

Marcello Galhardo
PRO

January 24, 2017
Tweet

Transcript

  1. Introduction to Dagger 2 Marcello Galhardo

  2. @marcellogalhardo @marcellogalhardo @MarcelloGalhard marcello.galhardo@gmail.com /marcellogalhardo Android Dev BR slack.androiddevbr.org

  3. Do you know... ...inversion of control?

  4. Do you know... ...inversion of control? ...Dagger?

  5. Why should I use Dependency Injection?

  6. Easy reuse of components

  7. Safer and easier refactoring

  8. Easier testing

  9. Activity

  10. Presenter Activity

  11. Presenter Repository Activity

  12. Presenter Repository Activity Api

  13. Presenter Repository Activity Cache Api

  14. public class Activity extends AppCompatActivity { private Presenter presenter; @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); presenter = new Presenter(); } } public class Presenter { private Repository repository; public Presenter() { repository = new Repository(); } } Activity.java / Presenter.java
  15. public class Activity extends AppCompatActivity { private Presenter presenter; @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); presenter = new Presenter(); } } public class Presenter { private Repository repository; public Presenter() { repository = new Repository(); } } Activity.java / Presenter.java
  16. public class Activity extends AppCompatActivity { private Presenter presenter; @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Api api = new Api(); Cache cache = new Cache(); Repository repository = new Repository(api, cache); presenter = new Presenter(repository); } } Activity.java
  17. public class Activity extends AppCompatActivity { private Presenter presenter; @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Api api = new Api(); Cache cache = new Cache(); Repository repository = new Repository(api, cache); presenter = new Presenter(repository); } } Activity.java
  18. • Too much boilerplate code; Problems!

  19. • Too much boilerplate code; • Hard to maintain the

    dependency graph. Problems!
  20. Let's talk about Dagger

  21. Dagger 2 • No Reflection;

  22. Dagger 2 • No Reflection; • Code generated at build

    time;
  23. Dagger 2 • No Reflection; • Code generated at build

    time; • Easy to debug;
  24. Dagger 2 • No Reflection; • Code generated at build

    time; • Easy to debug; • Easy to maintain the dependency graph.
  25. app/build.gradle dependencies { annotationProcessor "com.google.dagger:dagger-compiler:2.8" compile "com.google.dagger:dagger:2.8" provided "org.glassfish:javax.annotation:2.8" }

  26. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  27. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  28. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  29. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  30. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  31. @Singleton @Component(modules = { MainModule.class, ...}) public interface MainComponent {

    void inject(Activity activity); } MainComponent.java
  32. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  33. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  34. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  35. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  36. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  37. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  38. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  39. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  40. MainModule.java @Module public class MainModule { private static final int

    CACHE_SIZE_10_MB = 10 * 1024 * 1024; private static final int TIMEOUT = 20; @Provides public Presenter providesPresenter(Repository repository) { return new Presenter(repository); } @Provides @Reusable public Repository repository(Cache cache, Api api) { return new Repository(cache, api); } @Provides @Singleton public Cache providesCache() { return new Cache(CACHE_SIZE_10_MB); } @Provides @Reusable public Api providesApi() { return new Api(TIMEOUT); } }
  41. MainApplication.java public class MainApplication extends Application { private MainComponent component;

    @Override public void onCreate() { super.onCreate(); component = DaggerMainComponent.builder().build(); } public MainComponent getComponent() { return component; } }
  42. MainApplication.java public class MainApplication extends Application { private MainComponent component;

    @Override public void onCreate() { super.onCreate(); component = DaggerMainComponent.builder().build(); } public MainComponent getComponent() { return component; } }
  43. MainApplication.java public class MainApplication extends Application { private MainComponent component;

    @Override public void onCreate() { super.onCreate(); component = DaggerMainComponent.builder().build(); } public MainComponent getComponent() { return component; } }
  44. MainApplication.java public class MainApplication extends Application { private MainComponent component;

    @Override public void onCreate() { super.onCreate(); component = DaggerMainComponent.builder().build(); } public MainComponent getComponent() { return component; } }
  45. Activity

  46. Application Activity

  47. Application Component Activity

  48. Application Component Activity MainModule

  49. Application Component Activity Modules... MainModule

  50. Activity.java public class Activity extends AppCompatActivity { @Inject Presenter presenter;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainApplication application = getMainApplication(); MainComponent component = application.getComponent(); component.inject(this); } }
  51. Activity.java public class Activity extends AppCompatActivity { @Inject Presenter presenter;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainApplication application = getMainApplication(); MainComponent component = application.getComponent(); component.inject(this); } }
  52. Activity.java public class Activity extends AppCompatActivity { @Inject Presenter presenter;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainApplication application = getMainApplication(); MainComponent component = application.getComponent(); component.inject(this); } }
  53. Activity.java public class Activity extends AppCompatActivity { @Inject Presenter presenter;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainApplication application = getMainApplication(); MainComponent component = application.getComponent(); component.inject(this); } }
  54. Repository Cache Api

  55. Repository MockCache MockApi Api Cache

  56. Examples

  57. github.com/marcellogalhardo/events

  58. github.com/marcellogalhardo/locations

  59. Questions?

  60. www.concretesolutions.com.br Rio de Janeiro – Rua São José, 90 –

    cj. 2121 Centro – (21) 2240-2030 São Paulo - Av. Nações Unidas, 11.541 3º andar - Brooklin - (11) 4119-0449 We help companies create successful digital products