Circular dependency in C#

Posted on 2006-03-31
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

    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?

    LVL 5

    Expert Comment

    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.
    LVL 4

    Expert Comment

    This is a serious Code Smell (

    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 Comment

    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
    LVL 4

    Accepted Solution

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

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    network + 7 59
    I need C# converted to 1 34
    import issue in bit values 3 36
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now