Sunday, February 5, 2017

Circular Dependency

When two library try to add reference of each other than visual studio gives the error or circular dependency.

Like you have a library StudentLib and MiddleLayer. StudentLib has a reference of MiddleLayer library. Now you need to add reference of MiddleLayer in StudenLib. Now when you try to add the reference of MiddleLayer in StudenLib it will give error of circular dependency.

Circular dependency problem can be solved by using interface.

ModdleLayer contains a class Student

    public class Student 
    {

        public int Namer { get; set; }

        public int Code { get; set; }

        public void Add()
        {

            StudentDAL obj = new StudentDAL();
            obj.Add();
        }
    }

StudentLib contains a class StudentDALayer

    public class StudentDAL
    {
        public void Add(Student obj)
        {

        }
    }

You can see here Student class required StudentDALayer class to call Add method. And Student class need Student class to access all properties of Student. But can't do so.


So this type of problem can resolved by using interface. Because both classes in different library.

    public interface IStudent
    {
        int Code { get; set; }
        int Namer { get; set; }

        void Add();
    }


Student class

    public class StudentDAL
    {
        public void Add(NSIStudent.IStudent obj)
        {

        }
    }

Middle Layer

    public class Student 
    {

        public int Namer { get; set; }

        public int Code { get; set; }

        public void Add()
        {

            StudentDAL obj = new StudentDAL();
            obj.Add(this);
        }

    }


No comments:

Followers

Link