Back to this again. You keep alluding to me being incompetent or having a lack of comprehension skills. You can stop with the indirect insults and the weaseling out of offering concrete examples with my being incompetent in your mind, as a lame excuse.
This is your only “reason” for not using DI. A waste of effort. Your other argument on DI breaking encapsulation was never proven or properly explained either.
I’ll give you one disadvantage that DI may cause. Complication. It makes a system more complicated in some cases.
0:1 against DI.
Tom had shown clearly how DI allows polymorphism, whereas your singleton pattern, in your only bit of example code, does not. What is better, to have an option open to have polymorphism available with DI or to have none at all with a singleton?
1:1 now.
Also, Jeff pointed out, internally declared dependencies can lead to “spooky actions at a distance”. He also said, “a class should make its dependencies obvious.” The spooky action at a distance problem is avoidable with DI and the externalization of dependencies is achievable with DI.
2:1 for DI.
I pointed out modern and modular code should also be testable. It should be possible to test classes as whole units on their own, thus the term unit testing. These classes should be loosely coupled, yet they may have dependencies and would need them for tests to run successfully. This is achievable with and also without DI. In most cases DI makes it easier to test, especially in larger systems. Even Jakob Jenkov says, “You should keep in mind that even if your application may never need a different implementation of some component, it can still be useful to be able to inject different mock implementations during unit testing.”
3:1 for DI.
You point out in your “DI is Evil” article, that Jakob Jenkov notes when DI is not beneficial. What you and he fails to point out is that it also doesn’t seriously hurt or is a major mistake to use it. Not beneficial != evil/ bad.
Still 3:1 for DI.
One advantage of DI not mentioned yet, I believe, is the ability to inject external configuration.
4:1 for DI.
You go on to quote from David Lungren, who says, “Just like any pattern it can become an anti-pattern when overused, and used incorrectly. If you are never going to be injecting a different dependency why are you using a dependency injector?” Again, that doesn’t say it is wrong, just possibly unnecessary. He also never says what incorrect use of DI is. I have yet to read it anywhere, where the argumentation made any concrete sense against the use of DI, except for the added complexity issue.
The cost to use DI is very little. It’s the cost of some added complexity. So the price you pay for using DI is very little. Whereas the gains are greater than the losses.
- Looser coupling
- Polymorphism as an option
- Identifiable dependencies to classes
- Easier to Test Code
- Increased Modularity
- And if you are a fan of AOP, it enables a much easier weaving of aspects throughout the application (oh, and this makes the score 5:1 for DI!)
Scott