In my current project we have an ApplicationComponent, which holds various modules including a NetworkModule:
DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.networkModule(new NetworkModule())
.apiModule(new ApiModule())
.build();
I now want to setup a UserServices module that will provide at least a UserServices object, and that object requires some of the provides from the other modules for configuration. For example, the NetworkModule provides an HttpLoggingInterceptor.Level that is used for other network calls in the app, and I would like to use the same in the UserServices calls.
Example from the debug version of the NetworkModule:
@Module
public class NetworkModule extends BaseNetworkModule {
HttpLoggingInterceptor.Level getHttpLoggingInterceptorLevel() {
return HttpLoggingInterceptor.Level.BODY;
}
}
I want to keep the UserServices provide in its own module for the sake of encapsulation.
How do I set up the UserServices module so it can be included in the same component, and have access to the Provides in the NetworkModule for configuring the UserServices object that it provides?
Thanks
I figured this out through a little trial and error. If the dependency module is not included in the component already then I can simply add the includes parameter to the @Module annotation:
@Module(includes= {
NetworkModule.class,
ApiModule.class
})
In my case, the dependency module and some others are used in several places and all are needed independently in the component itself. Fortunately, it appears that Dagger will wire stuff up for me across modules in the same component. So I really didn't need to do anything special in this case -- Just this in the component:
@Singleton
@Component(modules = {
ApplicationModule.class,
NetworkModule.class,
SchedulerModule.class,
ApiModule.class,
UserServicesModule.class
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With