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
198
votes
5 answers

What is Castle Windsor, and why should I care?

I'm a long-time Windows developer, having cut my teeth on win32 and early COM. I've been working with .NET since 2001, so I'm pretty fluent in C# and the CLR. I'd never heard of Castle Windsor until I started participating in Stack Overflow. I've…
David Hill
  • 4,102
  • 2
  • 23
  • 19
183
votes
11 answers

Unable to resolve ILogger from Microsoft.Extensions.Logging

I've configured my console application's Main like so var services = new ServiceCollection() .AddLogging(logging => logging.AddConsole()) .BuildServiceProvider(); And then I try to use it in another class like so private readonly ILogger…
reggaemahn
  • 6,272
  • 6
  • 34
  • 59
178
votes
3 answers

Spring: Why do we autowire the interface and not the implemented class?

Example interface IA { public void someFunction(); } @Resource(name="b") class B implements IA { public void someFunction() { //busy code block } public void someBfunc() { //doing b things } } @Resource(name="c") class C…
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
164
votes
6 answers

Can someone explain Microsoft Unity?

I've been reading the articles on MSDN about Unity (Dependency Injection, Inversion of Control), but I think I need it explained in simple terms (or simple examples). I'm familiar with the MVPC pattern (we use it here), but I just can't really grasp…
163
votes
10 answers

Is ServiceLocator an anti-pattern?

Recently I've read Mark Seemann's article about Service Locator anti-pattern. Author points out two main reasons why ServiceLocator is an anti-pattern: API usage issue (which I'm perfectly fine with) When class employs a Service locator it is very…
159
votes
7 answers

Passing data into "router-outlet" child components

I've got a parent component that goes to the server and fetches an object: // parent component @Component({ selector : 'node-display', template : ` ` }) export class…
Zack
  • 13,454
  • 24
  • 75
  • 113
159
votes
11 answers

Unit testing code with a file system dependency

I am writing a component that, given a ZIP file, needs to: Unzip the file. Find a specific dll among the unzipped files. Load that dll through reflection and invoke a method on it. I'd like to unit test this component. I'm tempted to write code…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
158
votes
7 answers

How to inject dependencies into a self-instantiated object in Spring?

Let's say we have a class: public class MyClass { @Autowired private AnotherBean anotherBean; } Then we created an object of this class (or some other framework have created the instance of this class). MyClass obj = new MyClass(); Is it…
Igor Mukhin
  • 15,014
  • 18
  • 52
  • 61
155
votes
15 answers

Dependency injection through constructors or property setters?

I'm refactoring a class and adding a new dependency to it. The class is currently taking its existing dependencies in the constructor. So for consistency, I add the parameter to the constructor. Of course, there are a few subclasses plus even more…
Niall Connaughton
  • 15,518
  • 10
  • 52
  • 48
154
votes
4 answers

Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)

I'm a little confused concerning when to use ${...} compared to #{...}. Spring's documentation only uses #{...}, but there are plenty of examples that use ${...}. Furthermore, when I started with SpEL I was told to use ${...} and it works fine. For…
sjngm
  • 12,423
  • 14
  • 84
  • 114
152
votes
5 answers

Is there a pattern for initializing objects created via a DI container

I am trying to get Unity to manage the creation of my objects and I want to have some initialization parameters that are not known until run-time: At the moment the only way I could think of the way to do it is to have an Init method on the…
147
votes
4 answers

What is javax.inject.Named annotation supposed to be used for?

I am trying to understand the javax.inject package and I am not clear what the javax.inject.Named annotation is supposed to be used for. The Javadoc does not explain the the idea behind it. Javadoc is at…
ams
  • 60,316
  • 68
  • 200
  • 288
146
votes
21 answers

Must Dependency Injection come at the expense of Encapsulation?

If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle…
urig
  • 16,016
  • 26
  • 115
  • 184
145
votes
4 answers

Ioc/DI - Why do I have to reference all layers/assemblies in application's entry point?

(Related to this question, EF4: Why does proxy creation have to be enabled when lazy loading is enabled?). I'm new to DI, so bear with me. I understand that the container is in charge of instantiating all of my registered types but in order to do so…
diegohb
  • 1,857
  • 2
  • 16
  • 34
142
votes
8 answers

how to unit test asp.net core application with constructor dependency injection

I have a asp.net core application that uses dependency injection defined in the startup.cs class of the application: public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options…
ganjan
  • 7,356
  • 24
  • 82
  • 133