Dependency Injection != Lot of arguments
The number of arguments that you are going to use depends on your personal code, with DI you focus on the dependencies that you need to achieve something, of course you will need at least these dependencies, even if you do not code the class in Dependency Injection / IoC Template terms. If you have too many arguments, you may have to rethink your project.
If in doubt, think in terms of support.
“If I need to change something, where will it be? And if I make this change, how many other places will be affected by the change?”
There are workarounds, just to say a few:
- Wrap 2 or more dependencies as a new dependency (the likelihood is that when you need several dependencies, you will not need the entire API of these dependencies, and therefore, you can hide part of it behind the new interface.)
- As Spock said, you can create a parameter object (the implementation to your left: a list of parameters or an object structure).
Depending on your programming language, you will probably find more useful solutions than others (option 1 may be more suitable for languages like C ++, where each dependency can significantly increase compilation time, while option 2, will probably be used with languages like PHP because the user requires less coding and effort).
source share