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

Introdução ao Dagger 2

Introdução ao Dagger 2

Uma introdução abordada com códigos de exemplo para demonstrar a necessidade e como utilizar da forma mais simples possível o framework de injeção de dependência "Dagger".

5cefbec62d834db21a3823a8e66d59d8?s=128

Marcello Galhardo
PRO

January 24, 2017
Tweet

Transcript

  1. Introdução ao Dagger 2 Marcello Galhardo

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

  3. Você conhece... ...inversão de controle?

  4. Você conhece... ...inversão de controle? ...Dagger?

  5. Por que usar Injeção de Dependência?

  6. Reusar componentes

  7. Segurança ao refatorar

  8. Facilidade nos testes

  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. • Muito código boilerplate; Problemas!

  19. • Muito código boilerplate; • Dificuldade em manter a árvore

    de dependências. Problemas!
  20. Vamos falar de Dagger

  21. Dagger 2 • Não usa Reflection, código gerado em buildtime;

  22. Dagger 2 • Não usa Reflection, código gerado em buildtime;

    • Fácil de depurar;
  23. Dagger 2 • Não usa Reflection, código gerado em buildtime;

    • Fácil de depurar; • Facilidade de manter árvores de dependência.
  24. 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" }

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

    void inject(Activity activity); } MainComponent.java
  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. 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); } }
  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. 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; } }
  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. Activity

  45. Application Activity

  46. Application Component Activity

  47. Application Component Activity MainModule

  48. Application Component Activity Modules... MainModule

  49. 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); } }
  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. Repository Cache Api

  54. Repository MockCache MockApi Api Cache

  55. Exemplos

  56. github.com/marcellogalhardo/events

  57. github.com/marcellogalhardo/locations

  58. 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 Ajudamos empresas a criar produtos digitais de sucesso