C# 循环依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2052579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:02:33  来源:igfitidea点击:

Circular dependencies

c#visual-studiodependenciespackage-design

提问by mrblah

I have 2 projects.

我有2个项目。

Project#2 has a reference to Project#1

项目#2 引用了项目#1

Now I need to reference Project#2 in Project#1, but vs.net is complaining about a circular dependency.

现在我需要在 Project#1 中引用 Project#2,但是 vs.net 抱怨循环依赖。

Is there a way out of this?

有没有办法解决这个问题?

采纳答案by Craig Wilson

Absolutely not. Circular dependencies are a indication of bad design. I don't mean to be harsh. There are some ways out of this.

绝对不。循环依赖是糟糕设计的标志。我并不是要苛刻。有一些方法可以解决这个问题。

1) You can refactor common code to another project, say Project#0

1)您可以将公共代码重构为另一个项目,例如 Project#0

2) You can fix your design, which is probably the way to go.

2)您可以修复您的设计,这可能是要走的路。

Uncle Bob has a good article on Packaging Principles which includes the Acyclic Dependencies Principle. http://www.objectmentor.com/resources/articles/granularity.pdf. Read this to know why cyclic dependencies are a bad thing.

鲍勃叔叔有一篇关于打包原则的好文章,其中包括非循环依赖原则。 http://www.objectmentor.com/resources/articles/granularity.pdf。阅读本文以了解为什么循环依赖是一件坏事。

回答by David M

Merge the two into one or redesign.

将两者合二为一或重新设计。

回答by wj32

No. Structure your projects properly. Try using some sort of ordering based on abstraction - low-level to high-level.

不。正确构建您的项目。尝试使用某种基于抽象的排序 - 从低级到高级。

回答by Adam Ralph

This points to a problem in your design. If there is a genuine need for two or more of your types to be mutually aware then they should exist in the same assembly.

这表明您的设计存在问题。如果您的两个或多个类型确实需要相互了解,那么它们应该存在于同一个程序集中。

回答by codekaizen

Refactor your projects to take the common elements out into a "Project #0" which both Project #1 and Project #2 reference.

重构您的项目以将公共元素放入“项目#0”中,项目#1 和项目#2 都引用该项目。

回答by David

I reallydon't mean to be a smart-aleck, but better program design is the answer.

真的不是想成为一个聪明的人,但更好的程序设计就是答案。

回答by R Samuel Klatchko

A circular dependency means that these are no longer two independent projects (because there it is impossible to build only one of them).

循环依赖意味着它们不再是两个独立的项目(因为不可能只构建其中一个)。

You need to either refactor so that you have only a one way dependency or you should merge them into a single project.

您需要重构以便您只有一种方式依赖,或者您应该将它们合并到一个项目中。

回答by Alfred Myers

Circular reference canbe done as seen in a previous question, but you should not do itfor the reasons everybody already stated here.

循环引用可以像在上一个问题中看到的那样完成,但是由于每个人都已经在这里说明的原因,您不应该这样做

回答by Amit

This seems to be a design flaw, nothing else. Re-design is the solution.

这似乎是一个设计缺陷,没有别的。重新设计是解决方案。

回答by K.B.Raj

I don't think it is a good solution but still we can do by following these steps

我不认为这是一个好的解决方案,但我们仍然可以按照以下步骤进行

  • add the reference
  • browse and
  • go to Debug folder of dll project,
  • find the .dll and Add .
  • 添加参考
  • 浏览和
  • 转到dll项目的Debug文件夹,
  • 找到 .dll 并添加 .