views:

759

answers:

6

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?

+11  A: 

You need to move MySite.Utils to MyProject.Data by the sound of it

A: 

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?

Vilx-
WTF? Why the downvote? Isn't the accepted answer just the same as what I said?
Vilx-
I'd amend your answer to say "one" way.
MatthewMartin
+4  A: 

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!!

Marc Gravell
A: 

Sounds like you could benefit (and enjoy!) from reading this...

http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756

matt_dev
Who mentioned querystring?
Marc Gravell
Whoah...that was a major brain fart...sorry :-PAt least he can benefit from the link...doh!
matt_dev
sorry that was my fault. i edited my post to try and make things clearer
+2  A: 

Defeat coupling with dependency injection.

Program to an interface.

David B
+2  A: 

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.

Will