'Dagger2 dependent-component vs subcomponent
What's the difference between Subcomponents
and Dependent-Components
with specific scope in action? What's pros or cons of every one?
My Story: Recently I decided to stop using dagger-android
and completely switched to traditional dagger
for dependency injection in the whole of my project. Do you have any recommendations for creating component/subcomponent per fragment/activity?
Solution 1:[1]
If you create sub-component, all your component's dependencies will get available in your sub-component too.
You can access sub-component only via parent component instance. The sub-component, scope is considered smaller than that of its parent component. So, your parent component and sub-component can't use the same scope annotation.
If you create dependent-component, then you have to explicitly expose dependency in your parent component. Then only, the child-component can use it. You can use dependent-component directly, but you'll have to provide instance of parent component to build a dependent-component.
Get more details from these videos: https://www.youtube.com/playlist?list=PLrnPJCHvNZuA2ioi4soDZKz8euUQnJW65
Solution 2:[2]
Just watched an interview, and the guy said that the downside of Subcomponents is that if you change anything inside them, the whole AppComponent gets regenerated (and in big codebases that can be very slow), while Dependent-Components are separate entities.
Will research that more and correct the post if it is wrong.
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 | Rahul Rastogi |
Solution 2 | russabit |