Skip to main content
Share Your Experience: Take the 2024 Developer Survey
208 votes
Accepted

Criticism and disadvantages of dependency injection

First, I would like to separate the design approach from the concept of frameworks. Dependency injection at its simplest and most fundamental level is simply: A parent object provides all the ...
Berin Loritsch's user avatar
130 votes
Accepted

What's the difference between using dependency injection with a container and using a service locator?

When the object itself is responsible for requesting its dependencies, as opposed to accepting them through a constructor, it's hiding some essential information. It's only mildly better than the very ...
Carl Raymond's user avatar
  • 1,010
116 votes

Criticism and disadvantages of dependency injection

Dependency injection is, like most patterns, a solution to problems. So start by asking if you even have the problem in the first place. If not, then using the pattern most likely will make the code ...
JacquesB's user avatar
  • 59.7k
105 votes

Managing and organizing the massively increased number of classes after switching to SOLID?

Now, to build a simple file saving application you have a class to check if the file already exists, a class to write the metadata, a class to abstract away DateTime.Now so you can inject times for ...
David Arno's user avatar
  • 39.3k
85 votes

What's the difference between using dependency injection with a container and using a service locator?

Imagine you are a worker in a factory that makes shoes. You are responsible for assembling the shoes and so you'll need a lot of things in order to do that. Leather Measuring tape Glue Nails Hammer ...
Rowan Freeman's user avatar
57 votes

Criticism and disadvantages of dependency injection

In my experience, there are a number of downsides to dependency injection. First, using DI does not simplify automated testing as much as advertised. Unit testing a class with a mock implementation ...
Eric's user avatar
  • 805
52 votes

Using a "Pass-through (God) Service" is bad, right?

This is not a God object. It seems like it is because there is so much here, but, in a way, it's doing nothing at all. There is no behavior code here. This isn't an omnipotent God that does ...
candied_orange's user avatar
51 votes
Accepted

How much is too much Dependency Injection?

As always, It Depends™. The answer depends on the problem one is trying to solve. In this answer, I'll try to address some common motivating forces: Favour smaller code bases If you have 4,000 lines ...
Mark Seemann's user avatar
  • 3,910
49 votes
Accepted

Dependency Injection vs Static Methods

There is no reason why this needs to be injected. This is just a function, it has no dependencies, so just call it. It can even be static if you want as it looks to be pure. One can write unit ...
Jon Raynor's user avatar
  • 11.5k
43 votes

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

Yes This is the whole point of the term "cross-cutting concern" - it means something that does not fit neatly in the SOLID principle. This is where idealism meets up with reality. People semi-new ...
Klom Dark's user avatar
  • 588
42 votes
Accepted

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

No. SOLID exists as guidelines to account for inevitable change. Are you really never going to change your logging library, or target, or filtering, or formatting, or...? Are you really not going to ...
Telastyn's user avatar
  • 109k
32 votes
Accepted

Technical term to denote opposite of dependency injection?

Is "tight coupling" an adequate term that stands as an antonym to DI? No. Tight coupling is much more than what dependency injection deals with. Dependency injection externalizes a ...
candied_orange's user avatar
30 votes
Accepted

What is the "right" way to implement DI in .NET?

Don't think yet about the tool that you are going to use. You can do DI without an IoC Container. First point: Mark Seemann has a very good book about DI in .Net Second: composition root. Make sure ...
Miyamoto Akira's user avatar
30 votes

Managing and organizing the massively increased number of classes after switching to SOLID?

Tasks that used to take 5-10 files can now take 70-100! This is the opposite of the single-responsibility principle (SRP). To get to that point, you must have divided up your functionality in a very ...
John Bollinger's user avatar
29 votes

Is Poor Man's Dependency Injection a good way to introduce testability to a legacy application?

The critique about Poor Man's Injection in NerdDinner has less to do with whether or not you use a DI Container than it does about setting up your classes correctly. In the article, they state that ...
Robert Harvey's user avatar
27 votes

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

For logging I think it is. Logging is pervasive and generally unrelated to the service functionality. It's common and well-understood to use the logging framework singleton patterns. If you don't, you'...
Brian Agnew's user avatar
  • 4,686
26 votes
Accepted

Can we completely replace inheritance using strategy pattern and dependency injection?

Sure, but we call that composition and delegation. The Strategy Pattern and Dependency Injection might seem structurally similar but their intents are different. The Strategy Pattern allows runtime ...
candied_orange's user avatar
24 votes

Dependency Injection: Should I create a Car class to hold all of its parts?

What good are a bunch of car parts sitting on a race track doing anyone? If all your car class does is hold car parts it's as useful as a wet bag of parts. Usage As the driver, what I want is ...
candied_orange's user avatar
24 votes

Is Poor Man's Dependency Injection a good way to introduce testability to a legacy application?

You make a false assumption here over what "poor man's DI" is. Creating a class that has a "shortcut" constructor that still creates coupling is not poor man's DI. Not using a container, and ...
David Arno's user avatar
  • 39.3k
23 votes

Criticism and disadvantages of dependency injection

I followed Mark Seemann's advice from "Dependency injection in .NET" - to surmise. DI should be used when you have a 'volatile dependency', e.g. there's a reasonable chance it might change. So if ...
Rob's user avatar
  • 241
22 votes

Dependency injection: should I use a framework?

The key of DI containers is the abstraction. DI containers abstract this concern for you. As you may guess, it has a noticeable impact on the code, often translated into a fewer number of constructors,...
Laiv's user avatar
  • 14.7k
22 votes
Accepted

Does there exist a programming language specifically designed for dependency injection?

Yes, there is indeed. Sort of. Newspeak has no static state and no global state. This means that the only possible way to get access to a dependency is to have it explicitly injected. Obviously, this ...
Jörg W Mittag's user avatar
21 votes

Criticism and disadvantages of dependency injection

My biggest pet peeve about DI was already mentioned in a few answers in a passing way, but I'll expand on it a bit here. DI (as it is mostly done today, with containers etc.) really, REALLY hurts code ...
Vilx-'s user avatar
  • 5,350
20 votes

Refactoring if-else logic to reflect the OOP principles

That code var human = new Human(); human.IsFather = false; human.IsMother = false; human.IsSon = true; human.Isdaughter = false; var newHuman = HumanHelper.GetHuman(human); Console.WriteLine(...
Spotted's user avatar
  • 1,690
19 votes
Accepted

How to know when to use dependency injection?

how to know if something should be a dependency? Whenever you need something done but you don't want to do it here. The big thing I'd like you to understand about reference passing (or Dependency ...
candied_orange's user avatar
18 votes
Accepted

How to use dependency injection and avoid temporal coupling?

There are several ways to deal with the initialization problem: As answered in https://softwareengineering.stackexchange.com/a/334994/301401, init() methods are a code smell. Initializing an object ...
pschill's user avatar
  • 1,860
17 votes
Accepted

React Native - Is using a singleton the best alternative to DI?

Dependency injection does not need to be complex at all, and it's absolutely worth learning and using. Usually it's complicated by the usage of dependency injection frameworks, but they aren't ...
Samuel's user avatar
  • 9,197
15 votes

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

My 2 cents ... Yes and no. You should never really violate the principles you adopt; but, your principles should always be nuanced and adopted in service to a higher goal. So, with a properly ...
svidgen's user avatar
  • 14.7k
15 votes
Accepted

How do you deal with dependencies that require the object you inject into?

Take for example a Potato, which is arguably a type of Food. Generally, potatoes don't whip out a food processor and turn themselves into cottage pie when you put them next to ground beef and carrots. ...
Dogs's user avatar
  • 1,176
15 votes

Dependency Injection: Should I create a Car class to hold all of its parts?

it looks like I should not define a Car class just to hold car parts because all car parts will depend on each other and this soup of parts is a car. Am I right? No. The point of dependency ...
Michael Borgwardt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible