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. Introdução ao Dagger 2
    Marcello Galhardo

    View full-size slide

  2. @marcellogalhardo
    @marcellogalhardo
    @MarcelloGalhard
    [email protected]
    /marcellogalhardo
    Android Dev BR
    slack.androiddevbr.org

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. Reusar
    componentes

    View full-size slide

  7. Segurança ao
    refatorar

    View full-size slide

  8. Facilidade
    nos testes

    View full-size slide

  9. Presenter
    Activity

    View full-size slide

  10. Presenter
    Repository
    Activity

    View full-size slide

  11. Presenter
    Repository
    Activity
    Api

    View full-size slide

  12. Presenter
    Repository
    Activity
    Cache
    Api

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  17. ● Muito código
    boilerplate;
    Problemas!

    View full-size slide

  18. ● Muito código
    boilerplate;
    ● Dificuldade em manter
    a árvore de
    dependências.
    Problemas!

    View full-size slide

  19. Vamos falar de Dagger

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. Dagger 2
    ● Não usa Reflection,
    código gerado em
    buildtime;
    ● Fácil de depurar;
    ● Facilidade de manter
    árvores de dependência.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. 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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  39. 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;
    }
    }

    View full-size slide

  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;
    }
    }

    View full-size slide

  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;
    }
    }

    View full-size slide

  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;
    }
    }

    View full-size slide

  43. Application
    Activity

    View full-size slide

  44. Application
    Component
    Activity

    View full-size slide

  45. Application
    Component
    Activity
    MainModule

    View full-size slide

  46. Application
    Component
    Activity
    Modules...
    MainModule

    View full-size slide

  47. 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);
    }
    }

    View full-size slide

  48. 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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  51. Repository
    Cache
    Api

    View full-size slide

  52. Repository
    MockCache
    MockApi
    Api Cache

    View full-size slide

  53. github.com/marcellogalhardo/events

    View full-size slide

  54. github.com/marcellogalhardo/locations

    View full-size slide

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

    View full-size slide