为什么Dagger子组件在模块中而不是直接在父组件中声明? [英] Why are Dagger subcomponents declared in a module and not in the parent component directly?

查看:65
本文介绍了为什么Dagger子组件在模块中而不是直接在父组件中声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 subcomponents = 属性是在组件的模块上而不是直接在组件上设置的?对我来说这似乎不是很直观,所以我想一定是有原因的.

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

推荐答案

从某种意义上说,子组件位于模块中更有意义:它们是私有实现细节,不一定要公开显示,而那些是 are 仍然可以直接在组件上列为构建器方法.为了比较起见,您也不能直接在组件上定义 @Provides @Binds 方法.这些也会影响该组件的实现细节,并且不一定是公开可见的.

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.

但是,您的观点是正确的;截至2019年4月29日,这是一个公开且经过分类的问题(#1463),由Google贡献者David P. Baker提交.

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:

[...]

建议将 subcomponents 添加到 dagger.Component dagger.Subcomponent dagger.producers.ProductionComponent dagger.producers.ProductionSubcomponent .

这将在概念上简化这些情况并减少样板,因为不需要空模块.

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

这篇关于为什么Dagger子组件在模块中而不是直接在父组件中声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆