views:

47

answers:

1

If I have class A which has a dependency on class B, then class B could be passed in to the ctor of class A.

What about if class B has a dependency on class C, does that mean class A should receive all required dependencies upon construction ?

+2  A: 

In general terms, Dependency Injection would suggest that your classes should have passed all dependencies in the constructor.

However, for your example, it seems to me that A depends on B and B depends on C. In other words, A only needs to have passed B in the constructor; because B will already be constructed using the C instance. In other words, if we wrote the code without a DI framework:

 C c = new C();
 B b = new B(c);
 A a = new A(b);
driis