0

The current program results in stackoverflow exception which i know why. How could I avoid the circular dependency here. How can i make the three classes independent of each other though the classes are dependent to each other(just imagine that the methods inside those classes refer each other).

namespace CircularDependency_1
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            B b = new B();
            Console.WriteLine("executed");
            Console.ReadLine();
        }
    }

    public class B
    {
        public A a;

        public B()
        {
            a = new A();
            Console.WriteLine("Creating B");
        }
    }

    public class A
    {
        public B b;

        public A()
        {      
             b = new B();
            Console.WriteLine("Creating A");
        }
    }

    public class C
    {
        public A a;
        public B b;

        public C ()
        {
            a = new A();
            b = new B();
            Console.WriteLine("Creating C");
        }
    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kumaran Raj K
  • 365
  • 1
  • 11
  • 1
    why `new`-ing `A` and `B` in the other's constructor? What are the rationales behind it? What is it expected to do? You just want 1 A and 1B instance and they refer to each other? Without these info, your question is simply like: I have a class `class A { A() { new A(); } }` and how should I fix it? No meaningful answer – Adrian Shum Feb 16 '17 at 02:33

2 Answers2

1

You should not be new'ing up your objects. Instead, you need to pass them as arguments to the constructor. You need to refactor your code to:

public class A {
  B _b;
  public A(B b) {
    _b = b;
    Console.WriteLine("Creating A");
  }
}
public class B {
  A _a;
  public B(A a) {
    _a = a;
    Console.WriteLine("Creating B");
  }
}
public class C {
  A _a;
  B _b;
  public C (A a, B b) {
    _a = a;
    _b = b;
    Console.WriteLine("Creating C");
  }
}

Then you need to refactor functions out of A (or B) into another class D:

public class A {
  D _d;
  public A(D d) {
    _d = d;
    Console.WriteLine("Creating A");
  }
}
public class B {
  A _a;
  D _d;
  public B(A a, D d) {
    _a = a;
    _d = d;
    Console.WriteLine("Creating B");
  }
}

public class C {
  A _a;
  B _b;
  public C (A a, B b) {
    _a = a;
    _b = b;
    Console.WriteLine("Creating C");
  }
}

public class D {
  public D () {
    Console.WriteLine("Creating D");
  }
}

You can then create objects as

D d = new D();
A a = new A(d);
B b = new B(a, d);
C c = new C(a, b);
Console.WriteLine("executed");
Console.ReadLine();

See Circular Dependency in constructors and Dependency Injection on how to refactor your classes to remove circular references

user2321864
  • 2,207
  • 5
  • 25
  • 35
0

That's not dependency injection, you are creating it in the constructor. You should keep the A and B constructors empty, and do something like this in C:

public class C
    {
        public A a;
        public B b;

        public C ()
        {
            a = new A();
            b = new B();
            a.setB(b);
            b.setA(a);
        }
    }

In the other hand, you should check if you really need to have this double reference.

EDIT: I see that you are not really using class C. If you want to do it in main, is the same thing:

 static void Main(string[] args)
    {
        A a = new A();
        B b = new B();
        a.setB(b);
        b.setA(a);
    }
leoxs
  • 425
  • 1
  • 5
  • 15