ASP.NET MVC: How to avoid circular reference between 2 projects
My application has at least 2 projects, the OrganizationMangement and the ContactManagement. Previously, I referenced ContactManagement because I need to work with a class located in the OrganizationManagement. Now I need to do the reverse but I'm 开发者_如何学运维getting the following error "Cannot reference OrganizationManagement ... to avoid circular reference."
I guess the error makes sense. But, how can I avoid that? I mean I need those classes just to transfert data from one project to another.
To solve the problem, I copied the class to a folder located in the other project and tried this
var contact = (Contact)TempData["contact"];
Now, I'm getting this error: Cannot convert implicitly ContactManagement.Contact to OrganizationManagement.Contact... an explicit conversion exists...
Thanks for helping
It may not be as bad as you think - a project can always refer to its own classes without needing an explicit reference.
However, when this kind of structural problem crops up, it's usually telling you there's a flaw in the overall design.
I'd guess that there's an implicit third project that you haven't defined yet, that your Organization and Contact projects will each need a reference to. Once you've moved the class in question into the new project, create a reference to it in each of your existing ones, and you'll be all set.
Of course, this may necessitate other structural changes - sorting out this kind of problem can turn out to be a real can of worms.
Bottom line: circular references usually indicate that there's a bit more thought needed to work out what the dependencies in your object model really are.
Maybe you should refactor all the common domain-related classes to their own new project that can then be referenced by the other projects.
On the other hand I could suggest a very bad workaround for the error you encounter by defining an explicit conversion between the structurally identical classes that only differ in name. But please don't do so. Put all the domain stuff (i.e. entitiy-related classes like Contact, Person, Organization, Customer etc.) to its own project and reference this from the projects that need the classes.
Compile both projects and copy the assemblies to a folder "libs". Dont reference the projects but the compiled assemblies.
This works for me. I have 1 project Shop with a Backend ShopBackend. Then I have a project MarketPlace with a a backend MarketPlaceBackend. Both main projects have not a lot in common. Markeplace is a very small application.
The ShopBackend has a class Order that accesses the ShopDatabase. In MarketPlace I use the Assembly ShopBackend to get a list of orders.
On the other hand in Shop I need a list of the participants of MarketPlace. Thats why I call the MarketPlace assembly there.
Yes this design hurts sometimes: It is hard to get the versions right if the signature of methods changed. But in my case applications are realy separate and they have separate databases.
What I want to say: a circular reference can easily be a design issue, but it doesnt have to be. Imagine that both applications came from different companies. I gues it would be ok if Microsoft.dll uses some methods off google.dll and google.dll uses methods of microsoft.dll.
精彩评论