Questions tagged [dependency-injection]

Dependency Injection, is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties). It is a special form of the more general dependency inversion.

Dependency injection is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties).

Use the tag for:

  • The principles of dependency injection
  • When/How/Should one use dependency injection in a given, detailed situation
  • The way the injection is performed (at construction, at the call of a method, or via properties)
  • Usage of dependency injection along with other design patterns, or/and in unusual situations

More information:

Disambiguation:

669 questions
622
votes
13 answers

So Singletons are bad, then what?

There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, there are still many cases where I can't see a…
264
votes
7 answers

What does the Spring framework do? Should I use it? Why or why not?

So, I'm starting a brand-new project in Java, and am considering using Spring. Why am I considering Spring? Because lots of people tell me I should use Spring! Seriously, any time I've tried to get people to explain what exactly Spring is or what…
sangfroid
  • 3,259
172
votes
8 answers

Criticism and disadvantages of dependency injection

Dependency injection (DI) is a well known and fashionable pattern. Most of engineers know its advantages, like: Making isolation in unit testing possible/easy Explicitly defining dependencies of a class Facilitating good design (single…
167
votes
10 answers

When is it not appropriate to use the dependency injection pattern?

Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with automated testing? Are there any situations were you…
Tom Squires
  • 17,835
133
votes
13 answers

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on StackOverflow emphasize testing only a unit, not its…
dsimcha
  • 17,284
123
votes
5 answers

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

I understand that directly instantiating dependencies inside a class is considered bad practise. This makes sense as doing so tightly couples everything which in turn makes testing very hard. Almost all the frameworks I've come across seem to favour…
tom6025222
  • 1,014
  • 2
  • 8
  • 10
121
votes
9 answers

Understanding dependency injection

I'm reading about dependency injection (DI). To me, it is a very complicated thing to do, as I was reading it was referencing inversion of control (IoC) as well and such I felt I was going to be in for a journey. This is my understanding: Instead of…
118
votes
4 answers

Difference between Dependency Injection (DI) and Inversion of Control (IOC)

I've been seeing a lot of references of Dependency Injection (DI) & Inversion Of Control (IOC), but I don't really know if there is a difference between them or not. I would like to start using one or both of them, but I'm a little confused as to…
Elijah Manor
99
votes
7 answers

Should I use Dependency Injection or static factories?

When designing a system I am often faced with the problem of having a bunch of modules (logging, database acces, etc) being used by the other modules. The question is, how do I go about providing these components to other components. Two answers…
RokL
  • 2,451
96
votes
18 answers

Dependency injection: How to sell it

Let it be known that I am a big fan of dependency injection (DI) and automated testing. I could talk all day about it. Background Recently, our team just got this big project that is to built from scratch. It is a strategic application with complex…
Mel
  • 1,141
78
votes
4 answers

Dependency Injection: Field Injection vs Constructor Injection?

I know this is a hot debate and the opinions tend to change over time as to the best approach practice. I used to use exclusively field injection for my classes, until I started reading up on different blogs (exs: petrikainulainen and schauderhaft…
Eric B.
  • 1,249
55
votes
4 answers

Why do we need frameworks for dependency injection?

I've been reading up more on the Inversion of Control principle and Dependency Injection as an implementation of it and am pretty sure I understand it. It seems to be basically saying 'don't declare your class members' instantiations within the…
54
votes
2 answers

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is completely made up for the purpose of example public…
Lotok
  • 1,809
54
votes
3 answers

What is constructor injection?

I have been looking at the terms constructor injection and dependency injection while going through articles on (Service locator) design patterns. When I googled about constructor injection, I got unclear results, which prompted me to check in…
TheSilverBullet
  • 1,091
  • 1
  • 11
  • 22
52
votes
8 answers

When NOT to apply the Dependency Inversion Principle?

I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A has a method, that expects a pointer to an object of type class B, then…
Vorac
  • 7,169
1
2 3
44 45