Circular dependency in C#

Posted on 2006-03-31
Medium Priority
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
Question by:huonglt
LVL 12

Expert Comment

ID: 16342112
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?


Expert Comment

ID: 16343844
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.

Expert Comment

ID: 16348226
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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16357606
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

Accepted Solution

c_myers earned 500 total points
ID: 16361562
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).


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:

This is part 4 or 5 where he talks about Repositories, Biz logic, and persistence which may be especially interesting to you:


Author Comment

ID: 16377208
Well, I reviewed my design and made some changes to it. Now it's fine.
Thanks very much for your support

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question