Downsides of Dependency Injection
What I want to highlight in this post is one issue that may arise when using dependency injection (DI), in particular when using a dependency injection container. To be completely clear I'm not advocating against DI, I'm always using it and I would strongly suggest to use it if you are using a framework that supports it (for example ASP.NET core).
I will be focused on .NET and C# and some terminology will be specific to these technologies.
I updated this post on 08/09/2025 to clarify and improve some points.
What problems does DI solve? I really like what wikipedia says (bold is mine)
In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs.
If class A needs to use class B, it shouldn't also create an instance of it. This will give A too much responsibility since beside its actual functionalities it has to manage the lifetime of the instance of B.
To use SOLID principles, if class A serve some purpose, managing also the lifetime of an instance of class B breaks the single responsibility principle because A would end up with having at least 2 responsibilities.