构造器注入:多少依赖关系太多? [英] 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屋!