vb.net 什么是循环依赖,我该如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38042130/
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
What is a circular dependency and how can I solve it?
提问by ElektroStudios
Scenario
设想
I have a solution on which I have (more than) 2 projects.
我有一个解决方案,我有(超过)2 个项目。
The first project has a project reference to the second project. The second project doesn't have a reference to the first project.
第一个项目具有对第二个项目的项目引用。第二个项目没有对第一个项目的引用。
Well, in the first project I defined a inheritable class-type on which I would like that some classes from the second project inherits from it.
好吧,在第一个项目中,我定义了一个可继承的类类型,我希望第二个项目中的一些类继承自它。
Problem
问题
Obviouslly, If I want to inherit the type defined in the first project, in the second project I need to add a project reference to the first project to be able see the type and go on.
很明显,如果我想继承第一个项目中定义的类型,在第二个项目中我需要添加对第一个项目的项目引用,以便能够看到类型并继续。
The problem is that when I try to add the project reference, I get this error message:
问题是,当我尝试添加项目引用时,收到此错误消息:
Question
题
Someone could explain me with other simple words (maybe with a code example too in case of code is implied in the error) what is a circular dependency?, and the most important thing: what can I do to solve it? (please read the last prhases of my research before answering).
有人可以用其他简单的词来解释我(如果错误中隐含了代码,也可以使用代码示例)什么是循环依赖?最重要的是:我能做些什么来解决它?(请在回答之前阅读我研究的最后一句话)。
Research
研究
Is the first time that I hear the term "Circular dependency"; I've read thisarticle from MSDN but I understood nothing.
第一次听到“循环依赖”这个词;我已经从 MSDN 上阅读了这篇文章,但我什么都不懂。
Anyways I seen many questions of circular dependencys like this, and from what I've seen in that question seems that a circular dependency means that two projects cannot reference between them at the same time, just one of those two projects can reference the other; and also all the people who answered in that question said things like "Re-design is the solution" or "Circular dependencies are not good practices", however, re-designing in my case will mean define the same type in both projects, which I don't think that could be good practices neither, and of course building an additional assembly/project just to store a single type to reference that assembly in both projects ...is the worst idea I think.
反正我见过像圆形dependencys的许多问题这个,从我在这个问题见过看来,循环依赖意味着两个项目不能在它们之间在同一时间基准,只是这两个项目中的一个可以与其他参考; 并且所有回答该问题的人都说“重新设计是解决方案”或“循环依赖不是好的做法”,但是,在我的情况下重新设计将意味着在两个项目中定义相同的类型,即我认为这也不是好的做法,当然,构建一个额外的程序集/项目只是为了存储一个类型以在两个项目中引用该程序集......我认为这是最糟糕的想法。
回答by ChrisF
A circular dependency is where Project A depends on something in Project B and project B depends on something in Project A. This means to compile Project A you must first compile Project B, but you can't do that as B requires A to be compiled. This is the problem that circular dependencies cause.
循环依赖是项目 A 依赖于项目 B 中的某些内容,而项目 B 依赖于项目 A 中的某些内容。这意味着要编译项目 A,您必须先编译项目 B,但您不能这样做,因为 B 需要编译 A . 这就是循环依赖导致的问题。
If you introduce a circular dependency to a project that you've already built it can be hard to spot as the standard build options don't remove the existing object files thus enabling you to build A (or B) first. You'll only spot it when you try on a different machine that's never built the solution before or if you do a clean & build.
如果您向已构建的项目引入循环依赖项,则很难发现,因为标准构建选项不会删除现有的目标文件,从而使您能够首先构建 A(或 B)。只有当您尝试使用以前从未构建过解决方案的另一台机器时,或者您进行了清理和构建时,您才会发现它。
re-designing in my case will mean define the same type in both projects, which I don't think that could be good practices neither.
在我的情况下重新设计意味着在两个项目中定义相同的类型,我认为这也不是好的做法。
In this case you need to create a third project "C" which contains the classes that both A and B depend on so they no longer depend on each other. You might get away with just splitting the classes up so that dependencies can be sorted that way without creating the third project.
在这种情况下,您需要创建第三个项目“C”,其中包含 A 和 B 都依赖的类,因此它们不再相互依赖。您可能只需将类分开,这样就可以在不创建第三个项目的情况下对依赖项进行排序。
回答by Martin Verjans
What is a depedency ?
什么是依赖?
In order to understand what circular depedency is, it is better to understand what is a depedency and what it means to the compiler.
为了理解循环依赖是什么,最好理解什么是依赖以及它对编译器意味着什么。
Let's say you have a project and, in a class, you have the following defined :
假设您有一个项目,并且在一个类中,您定义了以下内容:
Public Class MyClass
'Some code here
Private MyString As String
'Some code there
End Class
When compiling your project, the compiler runs into the String class, which is defined in a DLL file called System. It will then link that DLL to your project, so at run-time, when defining or doing operation on the string, the System.dll will be loaded to perform those.
编译项目时,编译器会运行到 String 类,该类在名为 System.dll 的 DLL 文件中定义。然后它将该 DLL 链接到您的项目,因此在运行时,当定义或对字符串执行操作时,将加载 System.dll 以执行这些操作。
Now, let's say you have, further in your class, the following definition
现在,假设您在课堂上有以下定义
'Some code here
Private MyObjet as CustomClass1
'Some code there
And let's say CustomClass1is defined in another project of yours, named Project2.DLL :
假设CustomClass1在您的另一个项目中定义,名为 Project2.DLL :
Public Class CustomClass1
'Your customr class code
End Class
So when compiling your first project, the compiler will run into CustomClass1definition, it knows it lays into Project2.dll and therefore will compile Project2 before, in order to be able to add that reference in your first project.
因此,在编译您的第一个项目时,编译器会遇到CustomClass1定义,它知道它位于 Project2.dll 中,因此会在之前编译 Project2,以便能够在您的第一个项目中添加该引用。
That's what a depedency is, it's hierarchical, there must be a starting point. Even the String class is dependant on other classes, and at the end, they all rely on bytes or bits to do the job, because that's the only thing a computer can do, play with 1and 0.
这就是依赖,它是有等级的,必须有一个起点。甚至 String 类也依赖于其他类,最后,它们都依赖字节或位来完成这项工作,因为这是计算机唯一可以做的事情,使用1和0。
So the circular part
所以圆形部分
So if you have, in Project2, a reference (a field definition, or something like that) that link to your first project, what happens ?
因此,如果您在 Project2 中有一个链接到您的第一个项目的参考(字段定义或类似的东西),会发生什么?
- The compiler reads your first project, then runs into
CustomClass1 - Then it tries to compile Project2, since CustomClass1 is defined there
- Then it runs to a class defined in your first project
- It tries to compile your first project in order to link it to the second
- Then it runs to
CustomClass1 - Then it tried to compile Project2
- I guess you got it...
- 编译器读取您的第一个项目,然后运行到
CustomClass1 - 然后它尝试编译 Project2,因为 CustomClass1 是在那里定义的
- 然后它运行到您的第一个项目中定义的类
- 它尝试编译您的第一个项目以将其链接到第二个
- 然后它运行到
CustomClass1 - 然后它尝试编译 Project2
- 我猜你明白了...
So at some point the compiler displays an error, saying it cannot compile, as it doesn't understand what you're trying to do...
所以在某些时候编译器会显示一个错误,说它无法编译,因为它不明白你想要做什么......
Yes, computers are that stupid.
是的,电脑就是这么蠢。
How to solve it ?
如何解决?
Solving these kind of issue is sometimes difficult, but the basic idea is to build up a hierarchical structure, put the base class (those which don't need depedencies) together, then build up on them.
解决这类问题有时很困难,但基本思想是建立一个层次结构,将基类(那些不需要依赖的)放在一起,然后在它们之上构建。
Take all the classes that depend on each other and put them together, they form a layer for something you try to do in your application.
将所有相互依赖的类放在一起,它们形成一个层,用于您在应用程序中尝试执行的操作。
回答by Muhammed Younus
instead of using projects as references use the builded DLL of that project for referencing.. if you do the same in every modules of solution u can solve it. if u have two projects named as 'main' and 'sub' then in the main project add DLL of Sub project as refrence file and the same time in Sub project add the DLL of main project as reference..
而不是使用项目作为引用使用该项目的构建 DLL 来引用..如果您在解决方案的每个模块中都这样做,您可以解决它。如果你有两个名为“main”和“sub”的项目,那么在主项目中添加子项目的DLL作为参考文件,同时在子项目中添加主项目的DLL作为参考..


