使用 dagger2 的多层/库架构:设计范围、组件、模块 [英] Multi-layer / libraries architecture with dagger2: designing scopes, components, modules

查看:30
本文介绍了使用 dagger2 的多层/库架构:设计范围、组件、模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 多层多库 Android SDK 项目中使用 dagger 2.

I'm using dagger 2 in a multi-layered, multi-library Android SDK project.

我正在不同层(数据、业务逻辑、表示等)上开发许多不同的库,并使用 dagger 将它们粘合在一起.

I'm developing many different libraries on different layers (data, business logic, presentation etc...) and using dagger to glue them together.

我最重要的要求是架构的每个库都应该是可独立使用的(及其依赖组件),并且开发人员应该决定在他想要的任何层上进行构建:

My most important requirement is that each library of the architecture should be usable stand-alone (with its dependent components) and that a developer should decide to build on top on any layer he wants:

例如:

  • 使用下面的所有内容重写所有表示层
  • 随意替换堆栈的任何部分以增强或改变行为

现在我已经为每个库创建了一个具有自定义范围的 Dagger 组件,但有时我有一个依赖于 2-3 个其他组件的组件,而 Dagger 抱怨只应限定 1 个依赖组件的范围.(Es. 域层使用服务组件从公司服务获取数据,使用传感器组件获取设备传感器数据/连接性或其他).

For now I've created for each library a Dagger Component with a custom scope but sometimes I've a component that depends on 2-3 other components and Dagger complain that only 1 dependency component should be scoped. (Es. Domain layer using a service component to obtain data from a company service and a sensor component to obtain device sensor data / connectivity or whatever).

我无法摆脱作用域,因为我需要将这些组件设置为作用域/单例.

I can't get rid of scopes because I need those component to be scoped / singleton.

我目前的解决方法是将依赖组件传递给模块构造函数,但这看起来像是一种解决方法,我想知道使用 Dagger 2 处理这种需求的正确方法是什么.

My current workaround is to pass the dependency component to the module constructor, but this look like a workaround and I would like to know what's the right way to approach this kind of requirement with Dagger 2.

复杂性也不能很好地扩展,替换中间的一块需要扩展替换实现的模块之一,这对用户来说根本不友好.

The complexity is also not scaling very well and replacing a piece in the middle require to extend one of the module replacing the implementation, which is not user friendly at all.

我已经阅读了关于子组件的内容,但看起来这些子组件不能单独使用,除非您为每个组件编写一个组件,它们还为实际实现声明了模块,因此它们不能被其他一些实现替换.

I've read about subcomponents but looks like those can't be used stand-alone unless you write a component for each, they also declare modules for the actual implementation so they can't be just replaced with some other implementation.

有人可以用匕首分享他们的架构详细阐述这些概念,重点是一个库项目,供其他开发人员用来组装零件和重用组件吗?

Can someone share their architecture with dagger or elaborate on these concepts with the focus of a library project to be used by other developers to assemble parts and reuse components?

(这个问题已经在 dagger2 问题跟踪器上提出但被关闭指向堆栈溢出 - 仅使用模块也不合适,因为这些要求库的用户知道要组装哪个模块以及如何组装,我无法设置不强制特定实现的模块依赖项)

(this question has been asked initially on the dagger2 issue tracker but was closed pointing me to stack overflow -- using only module is also not suitable cause these require the user of the library to know which module to assemble and how and I have no way of setting up modules dependencies that do not force a specific implementation)

推荐答案

很抱歉,我认为这个想法行不通,因为需求有点矛盾.

I'm sorry but I don't think this idea will work because the requirements are a little bit contradictory.

即:

我不能只提供模块而不提供组件(如 Dagger 2 问题跟踪器的线程中所建议的那样)

I can't provide modules only and not components (as recommended in the thread on the Dagger 2 issue tracker)

我最重要的要求是架构的每个库都应该是可独立使用的(及其依赖组件),并且开发人员应该决定在他(或她)想要的任何层上进行构建

My most important requirement is that each library of the architecture should be usable stand-alone (with its dependent components) and that a developer should decide to build on top on any layer he (or she) wants

一起设计将极其困难.Dagger 2 在编译时使用静态代码生成.组件需要知道它们的子组件,或者,依赖组件需要知道它们的父组件.要拥有一个交钥匙 Dagger 2 解决方案,其中消费者所做的只是从组件调用注入,您需要至少部分设置对象图.这将使您难以实现所需的模块化.

will be extremely difficult to engineer together. Dagger 2 uses static code generation at compile time. Components need to know about their sub-components or, alternatively, dependent components need to know about their parents. To have a turnkey Dagger 2 solution where all the consumer does is call inject from a component, you will need to setup the object graph at least partially. This is going to make it difficult to achieve the modularity you require.

因此,我认为有关问题跟踪器线程的建议(仅提供模块并让消费者设置组件)是正确的解决方案.您可以使用您自己的范围注解和/或 JSR-330 @Singleton 等注解来提供有关您的库的使用者应如何设置其对象图的提示.

Hence, I think the advice on the issue tracker thread (provide modules only and let the consumer set up Components) is the correct solution. You can use your own scope annotations and/or the JSR-330 @Singleton etc. annotations to provide hints to how the consumer of your library should set up their object graph.

最后,Amir Ziarati 最近发布了一个使用 Dagger 2 的具有多个模块的 Android 项目示例.我希望它有帮助,链接是这里

Lastly, Amir Ziarati recently posted an example of an Android project with multiple modules using Dagger 2. I hope it helps, the link is here

这篇关于使用 dagger2 的多层/库架构:设计范围、组件、模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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