'Expose Dagger provided dependencies through component or module in multi-module app?
I am looking for some guidance on the best code structure for dagger in multi module android apps. Lets say we have featureModuleA and featureModuleB, both of which depend on commonCodeModule.
Id like commonCodeModule to provide some dependencies using dagger, to both featureModuleA and featureModuleB.
Would it be better to expose those dependencies using a Component from commonCodeModule, therefore both feature modules need to create that component at injection time OR is it better to expose those dependencies by allowing both feature modules to use the Module residing in commonCodeModule?
Solution 1:[1]
Do they need to share the instances from the graph of the commonModule? If yes, then it would be cleaner to use component dependency. Component approach is a bit messier than just exposing ready-made modules, but you more control over lifecycle of objects in the component.
In addition, if you have smaller set of dependencies coming from commonModule you can seed them using @BindsInstance
in your featureModule components. Binding instances will deprive your feature module components of knowledge about any kind of component coming from the commonModule.
My two cents on exposing modules: If featureModuleA and featureModuleB can create their own instances of the classes in the commonModules, then you can expose Modules.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nikola Despotoski |