Constructor Injection: How many dependencies is too many?

I would not worry about it.

Instead, I would worry about the class being too complex.

A class with many dependencies that uses them all but has no loops or if statements is fine. In some code I was working on recently there were around 14 dependencies in a class. However, there was only one path through the code and no logical way to group the dependencies into better classes.

A class with a small number of dependencies that contains many branch statements or complex loop conditions should be simplified.


This may be a sign that the class with the 6-10 dependencies itself needs to be refactored.


Runcible,

Here is a link to the Castle Windsor project. It is an Inversion of Control container. These containers allow factory classes to collect your dependencies together and inject them as a single object into your constructor.

http://www.castleproject.org/container/index.html

I have heard good things about Windsor. Spring also makes an IoC container, and there are others.


I would think no more than three or four. If you are getting more than that, I would start thinking about how well you are abstracting your concerns. A single repository object, for example, should fulfill all of your data retrieval needs within the class in question.