Thursday, July 4, 2013

C# circular dependency interview questions with answers.

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.

No comments: