Multibinding doesn't work with Component Dependencies
See original GitHub issueDagger docs states:
A binding in a subcomponent can depend on a multibound set or map from its parent, just as it can depend on any other binding from its parent. But a subcomponent can add elements to multibound sets or maps that are bound in its parent as well, by simply including the appropriate @Provides methods in its modules.
However, the same is not true when using component dependencies. If one has many small components that shares dependencies using “component dependencies” - a “child” component that has a dependency to another component might not be able to contribute to its Graph.
For example, consider the following classes:
@Component
interface CommonComponent {
fun factoryBindings(): Map<String, Factory>
}
@Component(
modules = [FeatureModule::class]
dependencies = [CommonComponent::class]
)
interface FeatureComponent
@Module
interface FeatureModule {
// Contributes to factories map.
@[Binds IntoMap FactoryKey("featureFactory")]
fun featureFactory(impl: FeatureFactory): CustomFactory
}
class CompositeCustomFactory @Inject constructor(
val factoriesMap: Map<String, Factory>
)
In this if I use FeatureComponent
to inject the CompositeCustomFactory
, I would only receive the ones contributed inside CommonComponent
- ignoring the ones inside FeatureComponent
(e.g., FeatureFactory
).
Note that using a subcomponent
setup, it’d work as expected.
Is this a design decision and/or limitation of Dagger, a bug when dealing with component dependencies or am I doing something wrong?
Obs: #1186 is related but the use cases are slightly different so I opened this PR.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Hi Marcello, no need to provide an example. I think @Chang-Eric is right that the reason you don’t get a conflict is because the
FactoryComponent
is installing into the map with typeMap<String, CustomFactory>
.However, I think the suggestion in https://github.com/google/dagger/issues/2027#issuecomment-670589644 still applies, just make sure your map types are the same.
Hey @bcorso,
The workaround from https://github.com/google/dagger/issues/1112#issuecomment-474985867 is working as expected and it is enough for my use case and therefore, I will close this issue.
Thanks a lot for yours and @Chang-Eric help! ✌️