I'm reading this great tutorial that explains how @Component.Builder
works in Dagger 2. The author did a good job and the article is straight forward, but there still are some confusing I need to clarify: the default implementation of Dagger 2 looks something like this:
The component:
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MainActivity mainActivity);
SharedPreferences getSharedPrefs();
}
The module:
@Module
public class AppModule {
Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides
Application providesApplication() {
return application;
}
@Provides
@Singleton
public SharedPreferences providePreferences() {
return application.getSharedPreferences(DATA_STORE,
Context.MODE_PRIVATE);
}
}
Component instantiating:
DaggerAppComponent appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this)) //this : application
.build();
According to the article, we can simplify this code even more by avoiding passing arguments to the module constructor using @Component.Builder
and @BindsInstance
annotations, then the code will look like this:
The component:
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MainActivity mainActivity);
SharedPreferences getSharedPrefs();
@Component.Builder
interface Builder {
AppComponent build();
@BindsInstance Builder application(Application application);
}
}
The module:
@Module
public class AppModule {
@Provides
@Singleton
public SharedPreferences providePreferences(
Application application) {
return application.getSharedPreferences(
"store", Context.MODE_PRIVATE);
}
}
And the component instantiating:
DaggerAppComponent appComponent = DaggerAppComponent.builder()
.application(this)
.build();
I almost understand how the above code works, but here is the part I don't understand: how did we get from appModule(new AppModule(this))
to application(this)
when we are instantiating the component?
I hope the question was clear and thanks.
tl;dr Dagger will create any no-arg constructor models itself if you don't pass them in and usage of @BindsInstance
might be better optimized than providing types from a module.
First you had a component that requires an Application
to be constructed. So you construct the module and pass it to the component.
Now, with the component builder, you can just bind single objects to a component. This is an alternative to what we did above. There is no longer the need for a module and we can just directly hand Dagger the object that we want in our component.
As it is with @Binds
to provide interface implemenetations, you can often assume that Dagger can and will optimize features like those better than the simple approach using a module, since the intention is more clearly marked.
So using the @BindsInstance
will add the type to our component so that we no longer need the module to provide it. We can now also remove the parameter from the module constructor.
how did we get from appModule(new AppModule(this)) to application(this) when we are instantiating the component?
Since Dagger can instantiate no-args modules itself there is no longer the need to explicitly add the module to the component, and we can replace that line with the new .application(this)
call.