Member-only story
Don’t blame the dependency injection framework
Over the last couple of months I’ve heard and read quite a few statements that say that Dependency Injection frameworks are bad things that you should avoid like the plague. In my opinion that’s just a result of rejecting something because it has been misused too long. Don’t get me wrong. I’ve been using them for years and fully acknowledge the consequences of misusing those frameworks and the effect on your code base. However, I’ve also learned that every tool has advantages and disadvantages. Knowing its strengths and weaknesses is part of the job of being a professional software developer. Dismissing a tool because of the latter seems a bit ignorant to me. So let’s talk about where this is coming from.
Why do people use them
Consider the (contrived) diagram below.
In a design like this, I can think of several reasons why developers decide to use a dependency injection framework. For one, they may want to promote developers designing their classes in such a way that they only depend on abstractions (1). This is a good thing in my opinion, since depending on things that are more stable helps prevent the ripple effect. And since an abstraction is more stable than an implementation, we’re good. A nice side-effect of all of this is that it allows you to switch out one implementation with another (2)…