带有Dagger2的多层/库体系结构:设计范围,组件,模块 [英] Multi-layer / libraries architecture with dagger2: designing scopes, components, modules

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

问题描述

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

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

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

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个依赖项组件应具有范围. (例如,域层使用服务组件从公司服务获取数据,传感器组件使用传感器组件获取设备传感器数据/连接性等).

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天全站免登陆