Circular dependencies

北战南征 提交于 2019-11-26 12:28:12

问题


I have 2 projects.

Project#2 has a reference to Project#1

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

Is there a way out of this?


回答1:


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

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

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.




回答2:


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




回答3:


Merge the two into one or redesign.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


Circular reference can be done as seen in a previous question, but you should not do it for the reasons everybody already stated here.




回答8:


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




回答9:


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




回答10:


Everyone will tell you this is a bad design do not do it etc. However sometimes it is easier said than done and moving the implementation into a separate common code is not desirable. For such cases instead of calling the other package directly, emit an event from one package and handle it in the other. That way you do no need to make the other component a dependency in the first component.

Another way if you still want to keep the implementation in separate packages is to derive your logic classes form interfaces and define those in a separate package. This works if you have a way to instantiate the implementation, for example via dependency injection or other means.




回答11:


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 .


来源:https://stackoverflow.com/questions/2052579/circular-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!