Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor Injection: How many dependencies is too many? [closed]

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.

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?

like image 613
Runcible Avatar asked Jun 04 '09 22:06

Runcible


People also ask

Is it difficult to inject dependency by constructor?

Frameworks that apply the Constrained Construction anti-pattern can make using Constructor Injection difficult. The main disadvantage to Constructor Injection is that if the class you're building is called by your current application framework, you might need to customize that framework to support it.

What is constructor dependency injection?

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.

How many way IOC can do dependency injection?

There are three types of dependency injection — constructor injection, method injection, and property injection.


1 Answers

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

like image 86
Jacob B Avatar answered Oct 21 '22 14:10

Jacob B