Top Banner
Introduction to Dagger 2 Marcello Galhardo
60

Introduction to Dagger 2 (Marcello Galhardo)

Jan 22, 2018

Download

Technology

concrete
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Dagger 2 (Marcello Galhardo)

Introduction to Dagger 2Marcello Galhardo

Page 2: Introduction to Dagger 2 (Marcello Galhardo)

@marcellogalhardo

@marcellogalhardo

@MarcelloGalhard

[email protected]

/marcellogalhardo

Android Dev BRslack.androiddevbr.org

Page 3: Introduction to Dagger 2 (Marcello Galhardo)

Do you know...

...inversion of control?

Page 4: Introduction to Dagger 2 (Marcello Galhardo)

Do you know...

...inversion of control?

...Dagger?

Page 5: Introduction to Dagger 2 (Marcello Galhardo)

Why should I use Dependency Injection?

Page 6: Introduction to Dagger 2 (Marcello Galhardo)

Easy reuse of components

Page 7: Introduction to Dagger 2 (Marcello Galhardo)

Safer and easier

refactoring

Page 8: Introduction to Dagger 2 (Marcello Galhardo)

Easier testing

Page 9: Introduction to Dagger 2 (Marcello Galhardo)

Activity

Page 10: Introduction to Dagger 2 (Marcello Galhardo)

Presenter

Activity

Page 11: Introduction to Dagger 2 (Marcello Galhardo)

Presenter

Repository

Activity

Page 12: Introduction to Dagger 2 (Marcello Galhardo)

Presenter

Repository

Activity

Api

Page 13: Introduction to Dagger 2 (Marcello Galhardo)

Presenter

Repository

Activity

CacheApi

Page 14: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 15: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 16: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 17: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 18: Introduction to Dagger 2 (Marcello Galhardo)

● Too much boilerplate code;

Problems!

Page 19: Introduction to Dagger 2 (Marcello Galhardo)

● Too much boilerplate code;

● Hard to maintain the dependency graph.

Problems!

Page 20: Introduction to Dagger 2 (Marcello Galhardo)

Let's talk about Dagger

Page 21: Introduction to Dagger 2 (Marcello Galhardo)

Dagger 2

● No Reflection;

Page 22: Introduction to Dagger 2 (Marcello Galhardo)

Dagger 2

● No Reflection;

● Code generated at build time;

Page 23: Introduction to Dagger 2 (Marcello Galhardo)

Dagger 2

● No Reflection;

● Code generated at build time;

● Easy to debug;

Page 24: Introduction to Dagger 2 (Marcello Galhardo)

Dagger 2

● No Reflection;

● Code generated at build time;

● Easy to debug;

● Easy to maintain the dependency graph.

Page 25: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 26: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 27: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 28: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 29: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 30: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 31: Introduction to Dagger 2 (Marcello Galhardo)

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

MainComponent.java

Page 32: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 33: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 34: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 35: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 36: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 37: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 38: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 39: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 40: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 41: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 42: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 43: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 44: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 45: Introduction to Dagger 2 (Marcello Galhardo)

Activity

Page 46: Introduction to Dagger 2 (Marcello Galhardo)

Application

Activity

Page 47: Introduction to Dagger 2 (Marcello Galhardo)

Application

Component

Activity

Page 48: Introduction to Dagger 2 (Marcello Galhardo)

Application

Component

Activity

MainModule

Page 49: Introduction to Dagger 2 (Marcello Galhardo)

Application

Component

Activity

Modules...MainModule

Page 50: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 51: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 52: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 53: Introduction to Dagger 2 (Marcello Galhardo)

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

Page 54: Introduction to Dagger 2 (Marcello Galhardo)

Repository

CacheApi

Page 55: Introduction to Dagger 2 (Marcello Galhardo)

Repository

MockCacheMockApi

Api Cache

Page 56: Introduction to Dagger 2 (Marcello Galhardo)

Examples

Page 57: Introduction to Dagger 2 (Marcello Galhardo)

github.com/marcellogalhardo/events

Page 58: Introduction to Dagger 2 (Marcello Galhardo)

github.com/marcellogalhardo/locations

Page 59: Introduction to Dagger 2 (Marcello Galhardo)

Questions?

Page 60: Introduction to Dagger 2 (Marcello Galhardo)

www.concretesolutions.com.br

Rio de Janeiro – Rua São José, 90 – cj. 2121Centro – (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