依赖于不同范围的其他组件的组件(不同范围的组件层次结构)

ult*_*aon 5 dependency-injection composition dagger-2

我的 Android 项目中有复杂的多层架构。

目前我想使用以下 DI 组件和模块的结构:

[Data Layer]
    @DataScope //scope is used for caching (Singleton) some Data Layer entities for whole application
    - DataComponent //exposes just interfaces which should be used on the BL Layer
        //Modules exposes entities for internal (Data Layer) injections and entities which exposed by DataComponent for BL Layer
        * DataModule1
        * DataModule2
        * DataModule3

[Business Logic Layer] (also has component dependency on DataComponent)
    @BlScope //scope is used for caching (Singleton) some BL Layer entities for whole application
    - BlComponent //exposes just interfaces which should be used on the Service Layer & Presentation Layer
        //Modules exposes entities for internal (BL Layer) injections and entities which exposed by BLComponent for the Service Layer & Presentation Layer
        * BlModule1
        * BlModule2

[Service Layer] (also has component dependency on BlComponent) - this layer has Android specific entities (Android Service, ContentProvider) not related to the Presentation Layer
    @ServiceScope //scope is used for caching (Singleton) some Service Layer entities for whole application
    - ServiceComponent //exposes just interfaces which should be used on the Presentation Layer
        * ServiceModule //Module exposes entities for internal (Service Layer) injections and entities which exposed by ServiceComponent for the Presentation Layer

[Presentation Layer] (also has component dependency on: ServiceComponent, BlComponent)
    @PresentationScope //scope is used for caching (Singleton) some Presentation Layer entities for whole application
    - PresentationComponent //exposes just interfaces which should be used on the current layer
        * PresentationModule //Module exposes entities injections on the current layer
Run Code Online (Sandbox Code Playgroud)

ServiceComponent 和 BlComponent 不公开类似的接口。

要构建主图,我使用以下代码:

DataComponent dataComponent = DaggerDataComponent.builder().<addModules>.build();
BlComponent dataComponent = DaggerBlComponent.builder().<addModules>.dataComponent(dataComponent).build();
ServiceComponent serviceComponent = DaggerServiceComponent.builder().<addModule>.blComponent(blComponent).build();
PresentationComponent presentationComponent = DaggerPresentationComponent.builder().<addModule>.blComponent(blComponent).serviceComponent(serviceComponent).build();
Run Code Online (Sandbox Code Playgroud)

在 PresentationLayer 中,我仅使用“presentationComponent”来提供来自 ServiceComponent/Layer 和 BLComponent/Layer 的所需依赖项。

目前上面的场景不起作用,因为 PresentationComponent 依赖于 2 个有错误的作用域组件

“...取决于多个作用域组件:...”

尽管它允许将一个作用域组件与许多非作用域组件一起使用。该架构旨在限制在上层使用内部层实体,同时在每个层/模块上进行独立测试(单元和仪器)。

任何人都可以帮助我理解它是 Dagger 处理器的错误还是可取的行为?(以及为什么?) Dagger2 存储库上的问题:https : //github.com/google/dagger/issues/747#issuecomment-303526785

Dav*_*son 4

根据此处的修复,这在 Dagger 2 的更高版本中不再是问题

在旧版本的 Dagger 2 上,可以通过声明依赖于 Dagger 组件超类型的组件来解决该问题,如此处的 GitHub 示例所示

interface AppComponent {
  App app();
}

@Component(dependencies = AppComponent.class)
interface RequestComponent { ... }

@Component
interface EnglishAppComponent extends AppComponent {}

@Component
interface SpanishAppComponent extends AppComponent {}
Run Code Online (Sandbox Code Playgroud)