205 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
131 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
115 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
  • 57.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.1k
86 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
55 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
  • 785
54 votes

Dependency Injection: Field Injection vs Constructor Injection?

Field injection is a bit too "spooky action at a distance" for my taste. Consider the example you provided in your Google Groups post: public class VeracodeServiceImplTest { @Tested(...
Robert Harvey's user avatar
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
50 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,870
47 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
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
39 votes
Accepted

What are the benefits of dependency injection in cases where almost everyone needs access to a common data structure?

in the case where almost everyone needs to know about a certain data structure, why is Dependency Injection any better than a global object? Dependency injection is the best thing since sliced bread, ...
Mike Nakis's user avatar
  • 32.1k
39 votes

What are the benefits of dependency injection in cases where almost everyone needs access to a common data structure?

There are plenty of reasons why non-mutable globals are evil in OOP. That is a dubious claim. The link you use as evidence refers to state - mutable globals. They are decidedly evil. Read only ...
Telastyn's user avatar
  • 109k
36 votes
Accepted

Why do we need frameworks for dependency injection?

We don't need the frameworks. It is entirely possible to implement dependency injection manually, even for a relatively large project. On the other hand, using a framework makes it easier, ...
Jules's user avatar
  • 17.7k
33 votes

Dependency Injection: Field Injection vs Constructor Injection?

The argument of less test initialization boiletplate is valid, but there are other concerns that must be taken into account. First of all, you have to answer a question: Do I want my class to be ...
Maciej Chałapuk's user avatar
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
28 votes
Accepted

Legitimate "real work" in a constructor?

And, besides what Ross Patterson suggested, consider this position which is the exact opposite: Take maxims such as "Thou Shalt Not Do Any Real Work In Thy Constructors" with a grain of salt. A ...
Mike Nakis's user avatar
  • 32.1k
28 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,676
25 votes

Is dependency injection by hand a better alternative to composition and polymorphism?

The best article I've ever read on the topic was James Shore's. I've been doing "DI by Hand" for ages, as part of abstraction and polymorphism. So after I got into the professional world and I kept ...
Nick's user avatar
  • 741
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.1k
24 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
23 votes

Dependency Injection: Field Injection vs Constructor Injection?

Field injection gets a definite "No" vote from me. Like Robert Harvey, it is a bit too automagic for my taste. I prefer explicit code over implicit, and tolerate indirection only as/when it provides ...
Marjan Venema's user avatar
22 votes

What does `Resolving` mean in programing?

The difference is slight, but consistent. Transforming a representation into another one involves taking the same data and expressing it in another format - for instance, a hexadecimal into a decimal ...
Kilian Foth's user avatar
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.5k
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

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