Why are Dagger subcomponents declared in a module and not in the parent component directly?

◇◆丶佛笑我妖孽 提交于 2021-01-28 12:26:27

问题


Why is the subcomponents = attribute set on a module of a component and not on the component directly? This doesn't seem very intuitive to me so I guess there must be a reason.

@Component(modules = ExampleModule.class)
public interface AppComponent 

@Module(subcomponents = ActivityComponent.class)
public abstract class ExampleModule

回答1:


In a sense, it makes more sense for subcomponents to be on modules: They're private implementation details that are not necessarily exposed publicly, and ones that are exposed can still be listed as builder methods on the component directly. For comparison's sake, you also cannot define @Provides or @Binds methods directly on components; those also affect the implementation details of the component and are not necessarily visible publicly.

However, your point stands; as of April 29, 2019, this is an open and triaged issue (#1463), filed by Google contributor David P. Baker.

We see a pattern of people creating otherwise empty modules just to add subcomponents to components:

[...]

Propose adding subcomponents to dagger.Component, dagger.Subcomponent, dagger.producers.ProductionComponent, and dagger.producers.ProductionSubcomponent.

This would simplify these cases conceptually and reduce boilerplate because there is no need for an empty module.



来源:https://stackoverflow.com/questions/55908839/why-are-dagger-subcomponents-declared-in-a-module-and-not-in-the-parent-componen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!