Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

What is Circular Dependency and How Do We Resolve It?

Rate me:
Please Sign up or sign in to vote.
4.93/5 (21 votes)
5 Jul 2013CPOL2 min read 217.5K   11   5
In this blog post, we will understand what is circular dependency in C# and how to resolve the same by using interfaces.

Can You Define Circular Dependency?

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

Image 1

If you ever try to reference class libraries with each other, Visual Studio throws the below exception.

Image 2

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 it's making a call to the data access layer using the “CustomerDal” class.

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

C#
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);
      }
  }
C#
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.

C#
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:

Image 3

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”.

Image 4

So below is the solution. You can create an altogether different C# project and create an interface “ICustomer” as shown in the below code:

C#
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.

C#
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.

C#
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 into 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.

Image 5

For further reading do watch the below interview preparation videos and step by step video series.

License

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


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
Questionconfused understanding !!! Pin
shikhar gilhotra25-Jan-17 2:21
shikhar gilhotra25-Jan-17 2:21 
AnswerRe: confused understanding !!! Pin
Dexter h4xx0r11-Apr-17 1:49
Dexter h4xx0r11-Apr-17 1:49 
NewsGood Explanation Thanks. Pin
Ammar Shaukat24-Jul-15 1:35
professionalAmmar Shaukat24-Jul-15 1:35 
GeneralMy vote of 3 Pin
joekarthik23-Sep-14 23:13
joekarthik23-Sep-14 23:13 
QuestionClarification on the ICustomer interface on the first example Pin
siberTITAN8-Dec-13 4:37
professionalsiberTITAN8-Dec-13 4:37 

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

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