循环依赖对干 [英] Circular dependencies versus DRY

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

问题描述

我设计一个名为core.xml.dll和可重用的类库,包含2个组件(其中包括)core.string.dll。

I'm designing a re-usable class library that contains 2 assemblies (amongst others) named core.xml.dll and core.string.dll.

在XML组件引用才能使用一些字符串辅助方法的串组件。

The xml assembly references the string assembly in order to use some string helper methods.

然而现在有一个字符串的方法,将是受益于利用包含在XML组件的方法。

However now there is an string method that would be benefit from using a method contained in the xml assembly.

如果我引用的XML组件与柱组件我会创建一个圆形的依赖,将无法建立两个组件从源头code。 (即鸡和蛋的问题)。

If I reference the xml assembly from the string assembly I will have created a circular dependency and will be unable to build both assemblies from source code. (ie chicken and the egg problem).

为了遵循不要重复自己的原则,我想避免重复的功能在这两个组件。如果我发现在执行中的错误我只想解决它在一个地方。

In order to follow the "Don't Repeat Yourself" principle I would like to avoid duplicating the functionality in both assemblies. If I find a bug in the implementation I only want to fix it in one place.

虽然我可以合并组件为一体,这是不理想的,因为它减少了组件的凝聚力。

While I could merge the assemblies into one, this is not ideal as it reduces the cohesiveness of the assembly.

我需要重新构建,重新部署整个组件只是一个小的变化,以一个特定的类。此外,最终,有这么多的依赖关系我可能会最终有一个巨大的图书馆装配。

I would need to re-build and re-deploy the entire assembly just for a small change to a specific class. Also, eventually, with so many dependencies I would probably end up with one huge library assembly.

因此​​,在可重复使用的一套库组件的背景是什么在这里使用的最佳方法?此外,如何在.NET框架本身处理这个问题?

So in the context of a re-usable set of library assemblies what is the best approach to use here? Also, how does the .NET framework itself deal with this issue?

(在反射似乎System.Configuration.dll引用system.xml.dll的,反之亦然。这实际上是正确的,如果是的话怎么会是循环依赖管理?)

(In Reflector it appears that System.Configuration.dll references System.XML.DLL and vice versa. Is this actually correct, if so how is the circular dependency managed?)

推荐答案

同意刀。通知depedencies是一个设计的气味。重构它无情地!

Agree with Knives. Circular depedencies is a design smell. Refactor it Mercilessly!

这可以在业务对象是紧耦合的情况下,具有挑战性的。在大多数情况下,这可以通过依赖注入来解决。

This can be challenging in the case where business objects are tightly coupled. In most cases this can be solved through dependency injection.

伪C ++示例:

class Employee {
    Company company;
};

class Company {
    vector<Employee> employees;
};

整蛊?不neccesarily:

Tricky? Not neccesarily:

template<class CompanyT>
class Employee {
    CompanyT company;
};

class Company {
    vector<Employee<Company> > employees;
};

更原始的类型,它必须依赖于一个更高的层次,可以抽象与任何类型的其他类型的工作,只要它履行其合同。

More primitive types, which must depend on a higher level, can be abstracted to work with any sort of other type, so long as it fulfills its contracts.

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

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