I have a Dagger object graph that is built from many smaller object graphs by using component dependency. Each component is essentially its own object graph and it exposes a few objects up to satisfy the dependencies of other components up the tree. The objects graphs within each component are irrelevant (so I'm not talking about Dagger @Modules at all.)
@Component(dependencies = [RepositoryComponent::class])
interface AppComponent {
(Similar setup for other components)
With vanilla Dagger, it is up to me to create each component by calling their respective builder.
val appComponent = DaggerAppComponent
.builder()
.build()
(Usually in Application class.)
But I also need to supply the component dependency by creating a Factory and adding to its create method
@Component(dependencies = [RepositoryComponent::class])
interface AppComponent {
@Component.Factory
interface Factory {
fun create(repositoryComponent: RepositoryComponent): AppComponent
}
}
and then calling the factory's create method instead of the builder
val appcComponent = DaggerAppComponent
.factory()
.create(repositoryComponent = ...)
All of this is standard component dependency, more or less straight from the Dagger docs
Things get tough when you realize you have to reproduce the whole thing for RepositoryComponent, ApiComponent and DatabaseComponent. The final "component creation" starts to balloon:
val apiComponent = ApiComponent.Factory
.create()
val databaseComponent = DatabaseComponent.Factory
.create()
val repositoryComponent = RepositoryComponent.Factory
.create(
apiComponent,
databaseComponent
)
val appComponent = AppComponent.Factory
.create(repositoryComponent)
}
You can imagine this won't scale once my app is 100 Dagger components. My application knows about the entire component tree. I'm leaking api code up to the app layer. Is there anyway to reduce this boiler plate? How do other teams handle this with larger apps?
