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".

Marcello Galhardo

January 24, 2017
Tweet

More Decks by Marcello Galhardo

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. Dagger 2 • Não usa Reflection, código gerado em buildtime;

    • Fácil de depurar; • Facilidade de manter árvores de dependência.
  6. 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); } }
  7. 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); } }
  8. 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); } }
  9. 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); } }
  10. 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); } }
  11. 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); } }
  12. 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); } }
  13. 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); } }
  14. 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); } }
  15. 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; } }
  16. 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; } }
  17. 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; } }
  18. 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; } }
  19. 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); } }
  20. 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); } }
  21. 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); } }
  22. 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); } }
  23. 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