Dependency Injection in Android With Dagger2 (6)
Multi-Module Components
Components can have multiple Modules. So if you have huge Modules that provide tons of services, it is good to split it up.
Let’s create a UseCaseModule
to hanle all the UseCase
s.
@Module
class UseCasesModule {
@Provides
fun fetchQuestionsUseCase(stackOverflowApi: StackoverflowApi) = FetchQuestionsUseCase(stackOverflowApi)
@Provides
fun fetchQuestionDetailsUseCase(stackOverflowApi: StackoverflowApi) = FetchQuestionDetailsUseCase(stackOverflowApi)
}
Some dagger conventions:
- Components can use more than 1 module.
- Modules of the same Component share the same object-graph.
- Dagger automatically instantiate modules with no argument constructor.
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email