开发者

How many dependencies should be passed using a ctor?

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开发者_如何学Go class B has a dependency on class C, does that mean class A should receive all required dependencies upon construction ?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜