0

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 ?

  • It's all answered here :P http://stackoverflow.com/questions/871405/why-do-i-need-an-ioc-container-as-opposed-to-straightforward-di-code – Ruben Bartelink Oct 18 '10 at 08:08

1 Answers1

4

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
  • 161,458
  • 45
  • 265
  • 341