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:

644 questions
610
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…
262
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,219
  • 4
  • 17
  • 12
163
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,695
  • 11
  • 67
  • 88
162
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…
Arkadiusz Kałkus
  • 1,770
  • 2
  • 12
  • 15
131
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,224
  • 9
  • 64
  • 81
121
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
  • 984
  • 2
  • 7
  • 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…
MyDaftQuestions
  • 1,881
  • 4
  • 16
  • 14
119
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,421
  • 3
  • 19
  • 14
97
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,121
  • 4
  • 12
  • 14
76
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,229
  • 1
  • 9
  • 13
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
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
50
votes
7 answers

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

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that at least resembles SOLID, but we're not quite…
JD Davis
  • 1,367
  • 1
  • 15
  • 23
50
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,073
  • 7
  • 38
  • 58
1
2 3
42 43