We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Circular dependency in C#

huonglt
huonglt asked
on
Medium Priority
2,610 Views
Last Modified: 2008-01-09
Hi everyone,
I create a blank solution, then create two class projects in it. It's a requirement that i must create a reference from each project to the other. But VS doesn't allow me to do it. It said that it makes the circular dependency problem. I know what it means.
Some people told me that i can create another assembly to solve the problem.
But for some reasons, i still want to keep some classes in its asembly now.
Please help me to solve it.
Thanks in advance
Comment
Watch Question

Commented:
There is no way around this, you are going to have to redesign your layer architecture. Maybe you can explain your exact dependencies and we can work through whether or not it is an ideal design.

For example:

A is the first class project, which has classes P, Q, R.
B is the second class project which has classes X, Y, Z.

Class P uses class X and class Q.
Class X uses class Q.

Assuming this was, in the first place, a good class aggregation design, then you would have to move Q into a third assembly which A and B both referenced.

Can you give details of your classes and the reasons you are cross-referencing the two assemblies?

Andy
Basically you need a managerial assembly.  A project of which it's sole purpose to gather and distribute resources from the other projects.  If your solution will ever need to be updated, this design will make for an efficient upgrade.



Alot of the constraints set in compiler are by design, stemming from universal coding standards.

Commented:
This is a serious Code Smell (http://xp.c2.com/CodeSmell.html).

You should never get yourself into this situation. And if you find yourself in this situation, there are usually several easy ways out of it depending on what the particular circumstances are.

Let's start at the beginning...
Why is it that you need to have the projects co-dependent?

Author

Commented:
My work was that I created a blank solution, then I created two class projects: one is Sample.BUL and the other is Sample.Entity representing for my business logic and my entities.
Some classes in the BUL layer needs to use entity classes, then I added a reference from the Sample.BUL to Sample.Entity. VS.NET allowed me to do so.
But then some classes in the Entity layer needs to use business classes, I added a reference again from the Sample.Entity to Sample.BUL, but in this casse VS.NET warned me that this would cause circular dependency problem.
I've tried some other ways to solve it, but nothing happen
For some reasons, i still want to put these classes in its assembly now.
Pls, help me
Thanks a lot
Commented:
Ok, that's your problem.

If you have a proper tiered design (which is what it sounds like you're trying to do), the entity layer should NEVER need to hit the BUL layer. If it does, then you designed something wrong somewhere.

I strongly suggest you get the book Domain Driven Design (a.k.a. DDD, book url = http://domaindrivendesign.org/book/index.html) and properly set up your domain, aggregates, and repositories to act upon the entities in your aggregates.

The whole Windows DNA n-tier design of the 90's is really bad and has been replaced (or extended) with far better models that work with a lot of the software out there (such as Object-relational mappers like NHibernate).

http://patternshare.org/default.aspx/Home.DDD.CoreDomain

Scott Bellware (a strong blogger on Domain-driven design, NHibernate, Test-driven development - TDD, etc) has a neat series of blog posts where he goes through the process of creating an application using DDD, TDD, and NHibernate.

It starts here:
http://codebetter.com/blogs/scott.bellware/archive/2005/12/29/136000.aspx

This is part 4 or 5 where he talks about Repositories, Biz logic, and persistence which may be especially interesting to you:
http://codebetter.com/blogs/scott.bellware/archive/2006/01/04/136178.aspx

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Well, I reviewed my design and made some changes to it. Now it's fine.
Thanks very much for your support
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.