mFe*_*ein 2 java android dependency-injection dagger-2
假设我想创建一个存储库,它将作为我的单一事实来源。在其中,我们将可以访问 REST 调用和数据库连接,以便将 REST 调用保存为缓存。
如果我想通过 Dagger 2 注入这个存储库,但我希望它可以被其他存储库实现替换(我避免使用“模块化”这个词),或者我希望它可以在其他组件上使用,我应该将其创建为存储库模块还是作为存储库子组件?
什么时候应该使用模块,什么时候应该使用子组件来实现模块化?
模块代表相关功能和绑定组,并且可能正是您正在寻找的内容。通过创建文档化且可重用的模块,您可以封装创建存储库的责任,从而允许其他开发人员或团队使用存储库,而无需知道如何或在何处创建它。您甚至可以选择将存储库的构造函数设为包私有,这样您就可以控制它的使用方式。
与 Dagger 1 不同,Dagger 2 期望模块不一定是完整的:它们可以引用它们未定义或指定的绑定。因此,如果您想创建一个依赖于外部组件的可重用模块,您可能需要记录它需要来自外部的绑定类型。(当然,您也可以使用Module.includes自己指定这一点,但这会阻止使用者将您的模块与他们选择的依赖项一起使用。替换依赖项可能是您的测试策略的重要组成部分,例如使用假的集成测试网络后端。)
相反,子组件通常代表不同的范围和生命周期。在 Android 应用程序中,这可能是服务、活动或片段的生命周期,但您也可以定义自己的范围:您还可以选择一些来表示用户的登录范围(即,只要用户登录,就使用相同的对象)已登录,但一旦用户注销或以其他人身份登录,就会出现新对象)。
然而,这两个选择不是非此即彼的,特别是在使用子组件进行封装时。如果您的存储库需要大量绑定,而您不希望从应用程序的其余部分注入这些绑定,则您可以选择将这些绑定绑定到仅包含在子组件中的模块中。那看起来像这样:
@Provides Repository provideRepository(RepositorySubcomponent subcomponent) {
return subcomponent.getRepository(); // defined on the subcomponent
}
Run Code Online (Sandbox Code Playgroud)
同样,您可能还需要在子图中绑定特定的绑定。也许您的应用程序需要两个独立的存储库后端,以及两个独立的存储实例。这可能是创建可重用存储库的问题,因为同一图无法为同一绑定注入不同的后端。(这有时称为“机器人腿”问题,想象一个机器人使用相同的腿和膝盖,但左脚和右脚不同。)使用子组件,您可以选择制作构建器的后端部分:
@Provides @Foo Repository provideFooRepository(
RepositorySubcomponent.Builder builder,
StoneTabletStorage stoneTabletStorage) {
// Inject and use the builder instead, since we're passing in a required value.
// In this example, you'd need to define a method on the builder,
// @BindsInstance Builder storage(Storage storageImpl);
return builder
.storage(stoneTabletStorage)
.build()
.getRepository();
}
@Provides @Bar Repository provideBarRepository(
RepositorySubcomponent.Builder builder,
HolographicQubitStorage holographicQubitStorage) {
return subcomponent
.storage(holographicQubitStorage)
.build()
.getRepository();
}
Run Code Online (Sandbox Code Playgroud)
...或为每个定义单独的子组件:
@Subcomponent(modules = {RepositoryModule.class, StoneTabletStorageModule.class})
public interface FooSubcomponent {
Repository getFooRepository();
}
@Subcomponent(modules = {RepositoryModule.class, HolographicQubitStorageModule.class})
public interface BarSubcomponent {
Repository getBarRepository();
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过在Modules.subcomponents上列出子组件来组合这些技术,从而创建一个可根据需要安装子组件的可重用模块。这允许子组件成为模块的实现细节,使您可以更灵活地更改模块,同时保留其他开发人员和团队使用的绑定。