Share
Embed
Dependency Injection with Dagger 2
Overview of how to use Dagger 2 and the benefits of Dependency Injection frameworks
Sample project that goes along with this talk: https://github.com/bgogetap/Dagger2Demo
Brandon Gogetap
April 13, 2016
Transcript
75 slides
-
What is Dependency Injection? • Act of providing your objects
with the dependencies they will need -
What is Dependency Injection? • Act of providing your objects
with the dependencies they will need • “new”ing can be expensive and wasteful -
What is Dependency Injection? • Act of providing your objects
with the dependencies they will need • “new”ing can be expensive and wasteful • Dependency Injection allows for easier reuse of dependencies -
Dependency Injection as a Pattern public final class DataRetriever {
public DataRetriever(…) { /*. . .*/ } public Call<Object> getWeather(String city) { OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("api_key")) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("...") .client(client) .build(); WeatherService service = retrofit.create(WeatherService.class); return service.getWeatherForCity(city); } } -
Dependency Injection as a Pattern public final class DataRetriever {
private final WeatherService service; public DataRetriever(WeatherService service) { this.service = service; } public Call<Object> getWeather(String city) { return service.getWeatherForCity(city); } } -
Dependency Injection as a Pattern • This is something we
all do every day -
Dependency Injection as a Pattern • This is something we
all do every day • Model/Library classes look clean -
Dependency Injection as a Pattern • This is something we
all do every day • Model/Library classes look clean • What about calling classes? -
Dependency Injection as a Pattern final class DataPresenter {
DataPresenter() { } void loadWeather() { OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("api_key")) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("...") .client(client) .build(); WeatherService service = retrofit.create(WeatherService.class); DataRetriever retriever = new DataRetriever(service); retriever.getWeather("test"); } } -
Dependency Injection as a Pattern final class DataPresenter {
private final DataRetriever retriever; DataPresenter() { OkHttpClient client = new OkHttpClient.Builder() /* … */; Retrofit retrofit = new Retrofit.Builder() .baseUrl("...") .client(client) .build(); WeatherService service = retrofit.create(WeatherService.class); retriever = new DataRetriever(service); } void loadWeather() { retriever.getWeather("test"); } } -
Dependency Injection as a Pattern • Traditional dependency injection shifts
boilerplate code to calling classes -
Dependency Injection as a Pattern • Traditional dependency injection shifts
boilerplate code to calling classes • Leads to cluttered Presenters/Views/etc. -
Dependency Injection as a Pattern • Traditional dependency injection shifts
boilerplate code to calling classes • Leads to cluttered Presenters/Views/etc. • Mocking or changing dependencies is difficult -
Dependency Injection as a Pattern • Traditional dependency injection shifts
boilerplate code to calling classes • Leads to cluttered Presenters/Views/etc. • Mocking or changing dependencies is difficult • Ideally we’d want all instantiation of dependencies in centralized area -
Dependency Injection with a Framework • Frameworks help you do
the work of “passing around” dependencies -
Dependency Injection with a Framework • Frameworks help you do
the work of “passing around” dependencies • Reduce the amount of “new” calls in your classes -
Dependency Injection with a Framework • Frameworks help you do
the work of “passing around” dependencies • Reduce the amount of “new” calls in your classes • Concentrate your dependencies in easy to find areas -
Dependency Injection with Dagger 2 • Dagger 2 is a
dependency injection framework developed by Google -
Dependency Injection with Dagger 2 • Dagger 2 is a
dependency injection framework developed by Google • Forked from Dagger which was created by Square (surprise!) -
Dependency Injection with Dagger 2 • Dagger 2 is a
dependency injection framework developed by Google • Forked from Dagger which was created by Square (surprise!) • Group your dependency instantiation in “Modules” -
Dependency Injection with Dagger 2 • Dagger 2 is a
dependency injection framework developed by Google • Forked from Dagger which was created by Square (surprise!) • Group your dependency instantiation in “Modules” • Provide scope to your dependencies using “Components” -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules @Module final class ApplicationModule {
private final Application application; ApplicationModule(Application application) { this.application = application; } @Provides Context provideApplicationContext() { return application; } @Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .addInterceptor(new AuthInterceptor("apiKey")) .build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("http://api.openweathermap.org/data/2.5/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } } -
Dagger 2 - Modules • Define and instantiate the dependencies
that will be provided -
Dagger 2 - Modules • Define and instantiate the dependencies
that will be provided • Can only contain one scope (can be mixed with unscoped dependencies) -
Dagger 2 - Modules • Define and instantiate the dependencies
that will be provided • Can only contain one scope (can be mixed with unscoped dependencies) • Scoped dependencies must match scope of Component -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components @Singleton @Component(modules = ApplicationModule.class) interface ApplicationComponent
{ MainComponent plus(MainModule mainModule); ForecastComponent plus(ForecastModule forecastModule); void inject(MyApplication application); void inject(MainActivity mainActivity); } -
Dagger 2 - Components • The “glue” between Modules and
Injected classes -
Dagger 2 - Components • The “glue” between Modules and
Injected classes • Allow you to manage scope -
Dagger 2 - Components • The “glue” between Modules and
Injected classes • Allow you to manage scope • Build the “dependency graph” out of only the modules you need for current state -
Dagger 2 - Components • The “glue” between Modules and
Injected classes • Allow you to manage scope • Build the “dependency graph” out of only the modules you need for current state • Scoped dependencies will be reused if injected by same component instance -
Dagger 2 - Components • The “glue” between Modules and
Injected classes • Allow you to manage scope • Build the “dependency graph” out of only the modules you need for current state • Scoped dependencies will be reused if injected by same component instance • Creating component in Activity#onCreate == new instances after config change -
Dagger 2 - Injection • Constructor Injection • Field Injection
• Method Injection -
Dagger 2 - Constructor Injection public final class DataRetriever {
private final WeatherService service; @Inject public DataRetriever(WeatherService service) { this.service = service; } public Call<Object> getWeather(String city) { return service.getWeatherForCity(city); } } -
Dagger 2 - Field Injection public final class ModalView {
@Inject SharedPreferences sharedPreferences; public ModalView(Context context) { /*. . .*/ Injector.<…>getComponent(context).inject(this); } } -
Dagger 2 - Method Injection • Very few use cases
• I don’t have an example -
Dagger 2 Summary • Modules provide dependencies • Components inject
classes • Scoped dependencies persist for life of component -
Dependency Injection as a Pattern public final class DataRetriever {
private final WeatherService service; public DataRetriever(WeatherService service) { this.service = service; } public Call<Object> getWeather(String city) { return service.getWeatherForCity(city); } } -
Dependency Injection as a Pattern public final class DataRetriever {
private final WeatherService service; @Inject public DataRetriever(WeatherService service) { this.service = service; } public Call<Object> getWeather(String city) { return service.getWeatherForCity(city); } } -
Dependency Injection as a Pattern final class DataPresenter {
private final DataRetriever retriever; DataPresenter() { OkHttpClient client = new OkHttpClient.Builder() /* … */; Retrofit retrofit = new Retrofit.Builder() .baseUrl("...") .client(client) .build(); WeatherService service = retrofit.create(WeatherService.class); retriever = new DataRetriever(service); } void loadWeather() { retriever.getWeather("test"); } } -
Dependency Injection as a Pattern final class DataPresenter {
private final DataRetriever retriever; @Inject DataPresenter(DataRetriever retriever) { this.retriever = retriever; } void loadWeather() { retriever.getWeather("Omaha").enqueue(new WeatherCallback()); } } -
Dagger 1 or Dagger 2? Dagger 2 • No reflection
(~13% performance gain) • Less flexible • Almost perfect compile time checks • Runtime errors are rare and easily avoided • Easier to debug* Dagger 1 • More flexible • Object graphs aren’t typed • Potential for Runtime failures • Inject calls in super class inject base class • Not a huge deal, but a nicety -
Benefits of using a DI Framework • Makes maintaining code
easier • Makes modifying the dependencies easier -
Benefits of using a DI Framework • Makes maintaining code
easier • Makes modifying the dependencies easier • Makes mocking for testing possible -
Benefits of using a DI Framework • Makes maintaining code
easier • Makes modifying the dependencies easier • Makes mocking for testing possible • Makes following single responsibility principle easier -
Single Responsibility Principle • Every class should have a specific
purpose • Splitting up code may seem like it’s increasing complexity, but it makes maintenance easier in the future -
Single Responsibility Principle • Every class should have a specific
purpose • Splitting up code may seem like it’s increasing complexity, but it makes maintenance easier in the future • Manually dealing with Bundles and re-setting up your state on config changes is a pain -
Single Responsibility Principle • Every class should have a specific
purpose • Splitting up code may seem like it’s increasing complexity, but it makes maintenance easier in the future • Manually dealing with Bundles and re-setting up your state on config changes is a pain • DI Frameworks make passing dependencies around to “helper” classes easier -
App Organization/Architecture • Bottom line is your views (Fragments, ?
extends View, Activities) should be dumb -
App Organization/Architecture • Bottom line is your views (Fragments, ?
extends View, Activities) should be dumb • Delegate the handling of “state” to something else besides your views -
App Organization/Architecture • Bottom line is your views (Fragments, ?
extends View, Activities) should be dumb • Delegate the handling of “state” to something else besides your views • Tell your views what to show and have them tell you what the user did -
App Organization/Architecture • Bottom line is your views (Fragments, ?
extends View, Activities) should be dumb • Delegate the handling of “state” to something else besides your views • Tell your views what to show and have them tell you what the user did • All of these combined make testing / maintaining far easier -
Links Dagger 2 Talks: Jake Wharton - https://www.parleys.com/tutorial/5471cdd1e4b065ebcfa1d557/ Gregory Kick
- https://www.youtube.com/watch?v=oK_XtfXPkqw Source: GitHub repo - https://github.com/google/dagger App Architecture Dependency Injection scope management: Mortar - https://github.com/square/mortar Concrete (Dagger 1) - https://github.com/JayNewstrom/Concrete Patterns: Mosby (MVP Library) - https://github.com/sockeqwe/mosby Scoop (Library - Use Views instead of Fragments) - https://github.com/lyft/scoop Android Testing Codelab (MVP Example) - https://codelabs.developers.google.com/codelabs/android-testing/ index.html?index=..%2F..%2Findex#0