构造函数注入:多少依赖太多了? [英] Constructor Injection: How many dependencies is too many?
问题描述
我已经使用手动构造函数注入 DI 有一段时间了.我注意到的一件事是我的构造函数开始变得相当长.
I've been using manual constructor injection DI for a little bit now. One thing that I notice is that my constructors are starting to get rather long.
我有一个依赖于一堆小对象的类 - 有时在 6 到 10 之间.随着我继续将我的应用程序分解成更小的块,我可以看到这个数字随着时间的推移而增加.这是常见问题吗?
I have a class that depends on a bunch of little objects - anywhere between 6 and 10 sometimes. As I continue to break my application into smaller chunks, I could see this number increasing over time. Is this a common problem?
显然,这在很大程度上取决于项目.然而,基本的问题是:
Obviously this is going to depend a great deal on the project. However, the basic question is this:
您什么时候开始对类的依赖项数量感到不舒服?您使用了哪些策略来减少这些依赖性?
When do you start to get uncomfortable with the number of dependencies that a class has? What are some strategies that you use to reduce these dependencies?
推荐答案
这可能表明具有 6-10 个依赖项的类本身需要重构.
This may be a sign that the class with the 6-10 dependencies itself needs to be refactored.
这篇关于构造函数注入:多少依赖太多了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!