Questions tagged [dependency-injection]

Dependency Injection is a set of software design principles and patterns that enables you to reduce coupling between components

Dependency injection (DI) is a set of software and for object-oriented programming () involving dynamically injecting (inserting) into a software component dependencies (service components) that it needs to function, without needing the dependent component to hard-code a dependency on the service. This reduces coupling between the dependent consumer and the service.

Benefits of Dependency Injection

  • Separation of concerns.
  • Boilerplate-code reduction in application classes because all work to initialize dependencies is handled by the composer.
  • Configurable components makes application easily extendable.
  • Unit testing is easy with mock objects.

Disadvantages of Dependency Injection

  • If overused, it can lead to maintenance issues because effect of changes are known at runtime.
  • Dependency injection hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.

Background questions

Resources / books

The following list of books (in print) focus specifically on Dependency Injection.

Books are presented in opposite order of publication

Related Patterns

26300 questions
402
votes
12 answers

Which .NET Dependency Injection frameworks are worth looking into?

Which C#/.NET Dependency Injection frameworks are worth looking into? And what can you say about their complexity and speed.
pbreault
  • 14,176
  • 18
  • 45
  • 38
365
votes
16 answers

What's the difference between the Dependency Injection and Service Locator patterns?

Both patterns seem like an implementation of the principle of inversion of control. That is, that an object should not know how to construct its dependencies. Dependency Injection (DI) seems to use a constructor or setter to "inject" it's…
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
355
votes
10 answers

How to avoid Dependency Injection constructor madness?

I find that my constructors are starting to look like this: public MyClass(Container con, SomeClass1 obj1, SomeClass2, obj2.... ) with ever increasing parameter list. Since "Container" is my dependency injection container, why can't I just do…
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
349
votes
19 answers

What are the downsides to using dependency injection?

I'm trying to introduce dependency injection (DI) as a pattern here at work and one of our lead developers would like to know: What, if any, are the downsides to using the dependency injection pattern? Note I'm looking here for an, if possible,…
Epaga
  • 38,231
  • 58
  • 157
  • 245
320
votes
10 answers

Spring @Autowire on Properties vs Constructor

So since I've been using Spring, if I were to write a service that had dependencies I would do the following: @Component public class SomeService { @Autowired private SomeOtherService someOtherService; } I have now run across code that uses…
GSUgambit
  • 4,459
  • 6
  • 25
  • 31
319
votes
7 answers

How do the major C# DI/IoC frameworks compare?

At the risk of stepping into holy war territory, What are the strengths and weaknesses of these popular DI/IoC frameworks, and could one easily be considered the best? ..: Ninject Unity Castle.Windsor Autofac StructureMap Are there any other…
ocodo
  • 29,401
  • 18
  • 105
  • 117
307
votes
19 answers

How can I inject a property value into a Spring Bean which was configured using annotations?

I have a bunch of Spring beans which are picked up from the classpath via annotations, e.g. @Repository("personDao") public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao { // Implementation omitted } In the Spring XML file,…
Dónal
  • 185,044
  • 174
  • 569
  • 824
304
votes
23 answers

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using…
teabot
  • 15,358
  • 11
  • 64
  • 79
290
votes
4 answers

What exactly is Field Injection and how to avoid it?

I read in some posts about Spring MVC and Portlets that field injection is not recommended. As I understand it, field injection is when you inject a Bean with @Autowired like this: @Component public class MyComponent { @Autowired private…
T. Jung
  • 3,327
  • 3
  • 11
  • 19
289
votes
22 answers

Do I need dependency injection in NodeJS, or how to deal with ...?

I currently creating some experimental projects with nodejs. I have programmed a lot Java EE web applications with Spring and appreciated the ease of dependency injection there. Now I am curious: How do I do dependency injection with node? Or: Do I…
Erik
  • 11,944
  • 18
  • 87
  • 126
257
votes
4 answers

.NET Core DI, ways of passing parameters to constructor

Having the following service constructor public class Service : IService { public Service(IOtherService service1, IAnotherOne service2, string arg) { } } What are the choices of passing the parameters using .NET Core IOC…
user9124444
236
votes
4 answers

Dependency Inject (DI) "friendly" library

I'm pondering the design of a C# library, that will have several different high level functions. Of course, those high-level functions will be implemented using the SOLID class design principles as much as possible. As such, there will probably be…
Pete
  • 11,313
  • 4
  • 43
  • 54
229
votes
7 answers

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

I started to convert my asp.net core RC1 project to RC2 and faced with problem that now IHttpContextAccessordoes not resolved. For sake of simplicity I created new ASP.NET RC2 project using Visual Studio Template ASP.NET Core Web Application (.Net…
YuriyP
  • 4,210
  • 4
  • 25
  • 35
225
votes
10 answers

Passing Parameters JavaFX FXML

How can I pass parameters to a secondary window in javafx? Is there a way to communicate with the corresponding controller? For example: The user chooses a customer from a TableView and a new window is opened, showing the customer's info. Stage…
Alvaro
  • 11,797
  • 9
  • 40
  • 57
208
votes
5 answers

How to explain dependency injection to a 5-year-old?

What is a good way to explain dependency injection? I found several tutorials on Google, but none of them that would assume the reader is just a Java beginner. How would you explain this to a novice?
user198313
  • 4,228
  • 6
  • 24
  • 19