【问题标题】:Expose Multibinding through a component dependency通过组件依赖暴露多重绑定
【发布时间】:2018-12-12 11:58:19
【问题描述】:

我目前正在开发一个多模块应用程序。 基本上有4个模块:

  • 带有 UI 的应用模块
  • 带有模型/接口的合同模块
  • 具有实际业务逻辑的实现模块
  • 依赖注入模块,允许为应用提供实现

这意味着应用程序不直接依赖于实施模块。

我基本上有以下设置:

应用程序组件

@Component(
        dependencies = {
                BaseComponent.class
        },
        modules = {
                AndroidInjectionModule.class,
                AndroidSupportInjectionModule.class,
                ContributesAndroidInjectorModule.class
        }
)
@ApplicationScope
public interface ApplicationComponent extends AndroidInjector<MyApplication> {
    @Component.Builder
    interface Builder {

        Builder baseComponent(BaseComponent baseComponent);

        ApplicationComponent build();
    }
}

基础组件

@Component(
        modules = {
                ...
        }
)
@Singleton
public interface BaseComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder context(@ApplicationContext Context context);

        @BindsInstance
        Builder configuration(AppConfiguration configuration);

        BaseComponent build();
    }

    ExposedDependency exposedDependency();
}

此设置完美运行,除了在 BaseComponent 中安装使用 @ContributesAndroidInjector 的模块:我找不到将包含 AndroidInjector 的地图暴露在 BaseComponent 上并添加到 ApplicationComponent 的方法。

有没有办法在 BaseComponent 中公开某种映射以将 AndroidInjectors 添加到 ApplicationComponent 中?

(我会尝试在没有敏感信息的情况下将此问题提取到 git repo)

【问题讨论】:

    标签: android dagger-2 dagger


    【解决方案1】:

    不是一个直接的答案,但我最终在我的业务逻辑模块中创建了抽象活动/片段/服务,并在 DI 模块中创建了具体类。

    具体类从应用程序中获取一个组件并使用它来注入依赖项。

    几乎是 AndroidInjector 之前的 Dagger 方式...

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多