问题
Here's the setup I have in a vs2008 solution:
Data layer in a project named MyProject.Data
Web application in a project named MyProject.Web
MyProject.Web has a reference to MyProject.Data
In MyProject.Web I have a class I use called "MySite.Utils"
I want to be able to use MySite.Utils in MyProject.Data but I can't because it would cause a circular reference.
One solution which is NOT possible is creating a third project and moving "MySite.Utils" in there because MySite.Utils actually uses MyProject.Data (thus it needs to reference it and another circular reference would be created)
What's the best/easiest way to fix this?
回答1:
You need to move MySite.Utils to MyProject.Data by the sound of it
回答2:
The best fix is to simplify things... for example, is that utility code data utility code, or ui utility code. Perhaps split it into 2 dlls; that might make things simpler.
After that, interfaces are a reasonable option, but you might need some IoC framework to provide the implementations. IoC is the most common way of getting around this type of problem. For example, you declare your ICustomerRepository in a reference assembly; everything references that. Your DAL implements the interface, but the utils project no longer needs to reference the DAL - just the interface assembly. Your DAL can now reference the utils - or it might just know about another interface IDataUtils (better to split it up more meaningfully, of course). The glue here is the IoC container, such as Castle Windsor.
Finally, and don't do this, but even though the IDE doesn't let you, it is possible to create circular references in .NET (via the command line tools); it is legal, but it gets very messy very quickly, and it is hard to repair a broken build. Don't go there!!
回答3:
Defeat coupling with dependency injection.
Program to an interface.
回答4:
MySite.Utils shouldn't reference any other project in your solution. Any classes that reference another solution within Utils should be moved into that solution it references.
回答5:
I believe that the ONLY way to fix this would be to move all the inter-related functionality in one assembly so that there are no circular references. Sorry. :(
Perhaps think about changing the architecture somehow that this is not required?
回答6:
Sounds like you could benefit (and enjoy!) from reading this...
http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756
来源:https://stackoverflow.com/questions/451068/vs2008-circular-references-c