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
142
votes
23 answers

How to inject window into a service?

I'm writing an Angular 2 service in TypeScript that will make use of localstorage. I want to inject a reference to the browser window object into my service since I don't want to reference any global variables like Angular 1.x $window. How do I do…
lokanx
  • 1,429
  • 2
  • 10
  • 5
142
votes
3 answers

What "things" can be injected into others in Angular.js?

I'm having a little hard time understanding Dependency Injection in Angular. So my question is, can anyone explain which of the "types", like Controller, Factory, Provider, etc can we inject into others, including other instances of same…
user1527166
  • 3,229
  • 5
  • 26
  • 26
141
votes
9 answers

Inject nestjs service from another module

I've got a PlayersModule and an ItemsModule. I want to use the ItemsService in the PlayersService. When I add it by injection: import { Injectable } from '@nestjs/common'; import { InjectModel } from 'nestjs-typegoose'; import { ModelType, Ref }…
Fairydhwen
  • 1,595
  • 3
  • 11
  • 10
137
votes
2 answers

Spring injects dependencies in constructor without @Autowired annotation

I'm experimenting with examples from this official Spring tutorials and there is a dependency on this code: https://github.com/spring-guides/gs-async-method/tree/master/complete If you look at the code on AppRunner.java class, I have 2…
winter
  • 2,687
  • 4
  • 18
  • 27
131
votes
6 answers

Difference between @Bean and @Autowired

Why can't I use @Autowired in this case? @SpringBootApplication public class Application { @Autowired BookingService bookingService; public static void main(String[] args) { bookingService.book("Alice", "Bob", "Carol"); …
zhuochen shen
  • 1,673
  • 4
  • 16
  • 24
129
votes
10 answers

How to handle dependency injection in a WPF/MVVM application

I am starting a new desktop application and I want to build it using MVVM and WPF. I am also intending to use TDD. The problem is that I don´t know how I should use an IoC container to inject my dependencies on my production code. Suppose I have the…
Fedaykin
  • 4,482
  • 3
  • 22
  • 32
127
votes
4 answers

Difference between dependency injection and dependency inversion

Two design patterns namely Dependency Injection and Dependency Inversion exist out there, Articles are there in the net trying to explain the difference. But the need to explain it in easier words is still there. Any one out there to come up to ? I…
122
votes
3 answers

ContextLoaderListener or not?

A standard spring web application (created by Roo or "Spring MVC Project" Template) create a web.xml with ContextLoaderListener and DispatcherServlet. Why do they not only use the DispatcherServlet and make it to load the complete configuration? I…
Ralph
  • 118,862
  • 56
  • 287
  • 383
121
votes
5 answers

Dependency injection, inject with parameters

I'm using vNext implementation of DI. How to pass parameters to constructor? For example, i have class: public class RedisCacheProvider : ICacheProvider { private readonly string _connectionString; public RedisCacheProvider(string…
120
votes
12 answers

Is there an alternative to bastard injection? (AKA poor man's injection via default constructor)

I most commonly am tempted to use "bastard injection" in a few cases. When I have a "proper" dependency-injection constructor: public class ThingMaker { ... public ThingMaker(IThingSource source){ _source = source; } But then,…
Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
119
votes
2 answers

On IServiceProvider what are the differences between the GetRequiredService and GetService methods?

What are the differences between IServiceProvider.GetRequiredService() and IServiceProvider.GetService()? When is it a better idea to use GetRequiredService()?
Art Base
  • 1,739
  • 3
  • 15
  • 23
117
votes
4 answers

.NET Core IServiceScopeFactory.CreateScope() vs IServiceProvider.CreateScope() extension

My understanding is that when using the built in the dependency injection, a .NET Core console app will require you to create and manage all scopes yourself whereas a ASP.NET Core app will create and manage the HttpRequest scope by default through…
Connie King
  • 1,171
  • 2
  • 8
  • 4
117
votes
9 answers

What is the difference between Strategy pattern and Dependency Injection?

Strategy pattern and Dependency Injection both allow us to set / inject objects at run time. What is the difference between Strategy pattern and Dependency Injection?
Nero
  • 1,173
  • 2
  • 8
  • 4
117
votes
6 answers

Dependency Injection with classes other than a Controller class

At this point I'm injecting things into my Controllers with ease, in some cases building my own ResolverServices class. Life is good. What I cannot figure out how to do is get the framework to automatically inject into non-controller classes. What…
117
votes
8 answers

exclude @Component from @ComponentScan

I have a component that I want to exclude from a @ComponentScan in a particular @Configuration: @Component("foo") class Foo { ... } Otherwise, it seems to clash with some other class in my project. I don't fully understand the collision, but if I…
ykaganovich
  • 14,736
  • 8
  • 59
  • 96