什么是循环依赖关系,我该如何解决? [英] What is a circular dependency and how can I solve it?

查看:585
本文介绍了什么是循环依赖关系,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景


我有一个解决方案,我有(超过)2个项目.

第一个项目具有对第二个项目的项目引用.第二个项目没有第一个项目的引用.

好吧,在第一个项目中,我定义了一个可继承的类类型,我希望第二个项目中的某些类可以从该类类中继承.

问题


很明显,如果我想继承第一个项目中定义的类型,那么在第二个项目中,我需要向第一个项目添加一个项目引用,以便能够看到类型并继续.

问题是,当我尝试添加项目引用时,出现以下错误消息:

问题


有人可以用其他简单的词来解释我(如果在错误中暗含代码的情况下,也可以使用代码示例)什么是循环依赖?最重要的事情是:我该怎么做才能解决它? (在回答之前,请先阅读我研究的最后一句话.)

研究


这是我第一次听到"循环依赖"一词;我已阅读 文章,但我一无所知.

无论如何,我看到了许多循环依赖问题,例如 ,以及从何而来我已经在那个问题中看到,似乎循环依赖意味着两个项目不能同时引用它们,而这两个项目中只有一个可以引用另一个项目. 而且所有回答该问题的人都说过诸如"重新设计是解决方案"或"循环依赖不是很好的做法"之类的东西,但是,重新设计在我的情况下,这意味着在两个项目中都定义相同的类型,我认为这也不是一种好习惯,当然,构建一个额外的程序集/项目只是为了存储单个类型以在两个项目中引用该程序集.我认为这是最糟糕的主意.

解决方案

什么是排毒?

为了了解什么是循环冗余,最好先了解什么是循环以及对编译器意味着什么.

假设您有一个项目,并且在一个类中,您定义了以下内容:

 Public Class MyClass
    'Some code here
    Private MyString As String
    'Some code there
End Class
 

在编译项目时,编译器会遇到String类,该类在名为System的DLL文件中定义.然后它将将该DLL链接到您的项目,因此在运行时,在对字符串进行定义或执行操作时,将加载System.dll来执行这些操作.

现在,假设您在课堂上进一步有了以下定义

 'Some code here
Private MyObjet as CustomClass1
'Some code there
 

假设CustomClass1是在您的另一个项目Project2.DLL中定义的:

 Public Class CustomClass1
    'Your customr class code
End Class
 

因此,在编译第一个项目时,编译器将运行到CustomClass1定义中,它知道它位于Project2.dll中,因此将在之前编译Project2,以便能够在第一个项目中添加该引用./p>

这就是依赖,它是分层的,必须有一个起点.甚至String类也依赖于其他类,最后,它们都依靠字节或位来完成工作,因为那是计算机唯一可以做的事情,同时使用10进行播放.

所以圆形部分

因此,如果在Project2中有一个引用(第一个字段定义或类似的东西)链接到第一个项目,那么会发生什么呢?

  • 编译器读取您的第一个项目,然后运行到CustomClass1
  • 然后尝试编译Project2,因为在那里定义了CustomClass1
  • 然后它会运行到您的第一个项目中定义的类
  • 它会尝试编译您的第一个项目,以便将其链接到第二个项目
  • 然后运行到CustomClass1
  • 然后它尝试编译Project2
  • 我想你明白了...

因此,在某些时候,编译器会显示错误,表示无法编译,因为它无法理解您要执行的操作...

是的,计算机真是愚蠢.

如何解决?

解决此类问题有时很困难,但是基本思想是建立一个层次结构,将基类(不需要依赖关系的基类)放在一起,然后在它们之上建立.

将相互依赖的所有类放在一起,它们构成了您在应用程序中尝试做的事情的层.

Scenario


I have a solution on which I have (more than) 2 projects.

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 this article from MSDN but I understood nothing.

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.

解决方案

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.

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 CustomClass1 is defined in another project of yours, named Project2.DLL :

Public Class CustomClass1
    'Your customr class code
End Class

So when compiling your first project, the compiler will run into CustomClass1 definition, 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.

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 1 and 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 ?

  • 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...

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.

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

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