循环依赖解决方案 [英] Circular Dependency Solution

查看:32
本文介绍了循环依赖解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前的项目遇到了循环依赖问题.我们的业务逻辑程序集使用了 SharedLibrary 程序集中的类和静态方法.SharedLibrary 包含一大堆辅助函数,例如 SQL Reader 类、枚举器、全局变量、错误处理、日志记录和验证.

Our current project has ran into a circular dependency issue. Our business logic assembly is using classes and static methods from our SharedLibrary assembly. The SharedLibrary contains a whole bunch of helper functions, such as a SQL Reader class, Enumerators, Global Variables, Error Handling, Logging and Validation.

SharedLibrary 需要访问 Business 对象,但 Business 对象需要访问 SharedLibrary.老开发者通过复制共享库中业务对象的功能解决了这种明显的代码异味(非常反DRY).我花了一天的时间试图阅读有关解决此问题的选项的信息,但我遇到了死胡同.

The SharedLibrary needs access to the Business objects, but the Business objects need access to SharedLibrary. The old developers solved this obvious code smell by replicating the functionality of the business objects in the shared library (very anti-DRY). I've spent a day now trying to read about my options to solve this but i'm hitting a dead end.

我对重新设计架构的想法持开放态度,但只是作为最后的手段.那么我怎样才能拥有一个可以访问业务对象的共享助手库,而业务对象仍然可以访问共享助手库?

I'm open to the idea of architecture redesign, but only as a last resort. So how can i have a Shared Helper Library which can access the business objects, with the business objects still accessing the Shared Helper Library?

推荐答案

您可以创建一个单独的项目,仅用于值对象(无逻辑)和接口.

您的共享库类是否实现了接口,并且业务库依赖于接口(我在这里听到更多可测试和解耦的代码吗?更不用说您从共享库中删除了依赖项).

Have your shared library classes implement the interfaces, and the Business library depend on the interfaces (do I hear more testable and decoupled code here? Not to mention you remove the dependency from the Shared Library).

理想情况下,您的共享库也可以拥有依赖于这个额外项目的业务对象.如果业务对象过于复杂,您也可以将它们转换为接口.

Ideally, you could have the business objects on which your shared library depend on this extra project too. If the business objects are too complex, you could also transform them into interfaces.

您将拥有两个不相互依赖的项目,而仅在另一个只有虚拟"对象(无逻辑)的项目上:

You will have both projects not depending on each other, but only on another project with only "dummy" objects (no logic):

业务 ---> 接口和值对象 <--- 共享库

Business ---> Interfaces and value objects <--- Shared Library

现在这些已经解耦了 =)

Now those are decoupled =)

这篇关于循环依赖解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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