Click here to Skip to main content
Click here to Skip to main content

What is Circular dependency and how do we resolve it?

By , 5 Jul 2013
Rate this:
Please Sign up or sign in to vote.

Can you define circular dependency?

Circular dependency is a situation where “classlibrary1” references “classlibrary2” and “classlibrary2” is trying to reference “classlibrary1”.


If you ever try to reference class libraries with each other visual studio throws the below exception.


Can you explain a scenario where circular dependency situation occurs?


Consider a situation of a 3 layer architecture where the middle layer is consuming data access layer. Below is a simple “Customer” class which belongs to the middle layer and its making call to the data access layer using the “CustomerDal” class.

For now assume that both these classes are in different c# project.

  public class Customer : ICustomer

    {
        public int CustomerCode { get; set; }
        public string CustomerName { get; set; }
      
        public void Add()
        {
            CustomerDal oDal = new CustomerDal();
            oDal.Add(this.CustomerCode,
                             this.CustomerName);
        }
    }
public class CustomerDal
    {
        public void Add(int CustomerCode,
                        string CustomerName)
        {
          
            // database insert code goes over here
        }
    }
Now if you see the “CustomerDal” class the add method has data passed in variables. It would be great if we can pass the “Customer” object directly. In other words we are looking at a modified version of “CustomerDal” as shown below.In order to achieve the same “CustomerDal” project needs to reference the “Customer” project.
public class CustomerDal
    {
        public void Add(Customer cust)
        {
            
            // database insert code goes over here
        }
    }
 

Now already the “Customer” project is referencing the “CustomerDal” project and if you try to reference the “Customer” project in “CustomerDal” project, it would throw a circular dependency error as shown below.

How can we overcome the circular dependency problem?
Circular dependency problem can be overcome by using interfaces or events. So for the above problem we can introduce an interface in between the “MiddleTier” and “Dal”
So below is the solution. You can create an altogether different c# project and create an interface “ICustomer” as shown in the below code.
public interface ICustomer
    {
        int CustomerCode { get; set; }
        string CustomerName { get; set; }
    }
This interface project you will implement in the middle layer project on the  “Customer” class.
public class Customer : ICustomer
    {
        public int CustomerCode { get; set; }
        public string CustomerName { get; set; }
        
        public void Add()
        {
            CustomerDal oDal = new CustomerDal();
            oDal.Add(this); // passing the object ( polymorphism)
        }

      
    }
Now in the data access layer project you can reference the interface project and use the interface rather than passing the individual variables in the method.  When the middle layer makes a call to data access layer he will pass “this”. Due to polymorphism the customer object would be automatically type casted to the data access layer in the interface format. Please see the code of “Customer” class where in the “Add” method we have passed “this” object.
public class CustomerDal
    {
        public void Add(ICustomer cust)
        {
            
            // database insert code goes over here
        }
    }

 Note: - Circular dependency is a sign of bad design and tight coupling. So when you get in to a circular dependency situation rather than fixing it directly by interfaces or events, question yourself why you landed in this situation. 

You can also watch the below where i have explained what is circular dependency and how to resolve the issue by using interfaces. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
QuestionClarification on the ICustomer interface on the first example PinmembersiberTITAN8-Dec-13 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 5 Jul 2013
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid