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

Marcello Galhardo

January 24, 2017
Tweet

More Decks by Marcello Galhardo

Other Decks in Programming

Transcript

  1. Introduction to 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. Do you know...
    ...inversion of control?

    View full-size slide

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

    View full-size slide

  5. Why should I use
    Dependency Injection?

    View full-size slide

  6. Easy reuse of
    components

    View full-size slide

  7. Safer and
    easier
    refactoring

    View full-size slide

  8. Easier testing

    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. ● Too much boilerplate
    code;
    Problems!

    View full-size slide

  18. ● Too much boilerplate
    code;
    ● Hard to maintain the
    dependency graph.
    Problems!

    View full-size slide

  19. Let's talk about Dagger

    View full-size slide

  20. Dagger 2
    ● No Reflection;

    View full-size slide

  21. Dagger 2
    ● No Reflection;
    ● Code generated at build
    time;

    View full-size slide

  22. Dagger 2
    ● No Reflection;
    ● Code generated at build
    time;
    ● Easy to debug;

    View full-size slide

  23. Dagger 2
    ● No Reflection;
    ● Code generated at build
    time;
    ● Easy to debug;
    ● Easy to maintain the
    dependency graph.

    View full-size slide

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

    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. @Singleton
    @Component(modules = {
    MainModule.class,
    ...})
    public interface MainComponent {
    void inject(Activity activity);
    }
    MainComponent.java

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

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

  44. Application
    Activity

    View full-size slide

  45. Application
    Component
    Activity

    View full-size slide

  46. Application
    Component
    Activity
    MainModule

    View full-size slide

  47. Application
    Component
    Activity
    Modules...
    MainModule

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

  52. Repository
    Cache
    Api

    View full-size slide

  53. Repository
    MockCache
    MockApi
    Api Cache

    View full-size slide

  54. github.com/marcellogalhardo/events

    View full-size slide

  55. github.com/marcellogalhardo/locations

    View full-size slide

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

    View full-size slide