循环依赖的解决方案解决方案

2023-09-03 11:21:54 作者:尾戒◎

我们目前的项目已经遇到了一个循环依赖的问题。我们的业务逻辑组件使用的类和静态方法从我们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需要访问业务对象,但业务对象需要访问SharedLibrary。旧的开发人员通过复制到共享库(非常防干烧)业务对象的功能解决了这个明显的code气味。我花了一天时间,现在想了解我的选择来解决这一点,但我打了死胡同。

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?

推荐答案

您只能对值对象(无逻辑)和接口

You could create a separate project only for value objects (no logic) and interfaces.

有您的共享库类实现的接口和商业图书馆取决于接口(做我听到更多的可测性,并在此脱钩code?何况你从删除的依赖共享库)。

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

现在那些脱钩=)