how to resolve circular dependency

帅比萌擦擦* 提交于 2019-12-11 11:09:51

问题


I have added another project "xml" to my project "synchronise".

So program.cs (in the xml project) runs the getDetails(), which runs the FectchDetails() in the synchronise project and returns the result as an object to the xml/getDetails().

If an error occurs in the Synchronise/FecthDetails() I want to re run the xml/getDetails().

Ive tried xml.getDetails, but it is saying it doesnt exist, because its not reference to the project so I try to add the xml project to the synchroinse but its telling me I cant do this as it would cause circular dependency....how can I resolve this

thanks


回答1:


If you encounter an error within Synchronise/FecthDetails(), propbably you can throw an application exception and catch that in xml/getDetails. Then you can decide whether to re-try or inform the user about it.

I am sorry if I misunderstood you question. if possible post some psuedo-code.




回答2:


Basically, you have a project X depending on project Y (X --> Y) , and project Y depending on project X ( Y --> X).

In other words, something like: ( X <---> Y)

This situation means that the compiler does not know what to compile first, and therefore complains.

To solve this, look for common things / pieces of logic that can be moved out from one or both of the projects, and create a third project that can be built before both of the others.

Place all common stuff in this new project, and you should be fine; your dependency should then be as below, where it does not matter if X or Y is compiled first, as long as Z is compiled before both of them:

( X --> Z <-- Y )




回答3:


You get circular dependency when:

A depends on B and B depends on A.

If you think both need this dependency then they should belong to one project.




回答4:


you can move FetchDetails() to another project and reference to this project from both xml and synchronise projects



来源:https://stackoverflow.com/questions/21853067/how-to-resolve-circular-dependency

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