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
114
votes
8 answers

Dependency injection with Jersey 2.0

Starting from scratch without any previous Jersey 1.x knowledge, I'm having a hard time understanding how to setup dependency injection in my Jersey 2.0 project. I also understand that HK2 is available in Jersey 2.0, but I cannot seem to find docs…
donnie_armstrong
  • 1,353
  • 2
  • 10
  • 8
112
votes
15 answers

Requested bean is currently in creation: Is there an unresolvable circular reference?

i am using spring 3, and i have two beans of view scope: 1- Bean1: @Component("bean1") @Scope("view") public class Bean1 { @Autowired private Bean2 bean2; } 2- Bean2: @Component("bean2") @Scope("view") public class Bean2 { @Autowired private…
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
111
votes
7 answers

How to get an instance of IServiceProvider in .NET Core?

IServiceProvider is an interface with single method: object GetService(Type serviceType); It's used to create instances of types registered in .NET Core native DI container. An instance of IServiceProvider itself can be obtained by calling a…
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
107
votes
3 answers

Is it possible to add qualifiers in @RequiredArgsConstructor(onConstructor = @__(@Autowired))?

If I wanted to use the annotation @Qualifier on a constructor dependency injection, I would have something like the following: public class Example { private final ComponentExample component; @Autowired public…
Pau
  • 14,917
  • 14
  • 67
  • 94
107
votes
10 answers

How do I pass values to the constructor on my wcf service?

I would like to pass values into the constructor on the class that implements my service. However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor. I would like to be able to pass in…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
107
votes
6 answers

Google Guice vs. PicoContainer for Dependency Injection

My team is researching dependency injection frameworks and is trying to decide between using Google-Guice and PicoContainer. We are looking for several things in our framework: A small code footprint - What I mean by a small code footprint is we…
austen
  • 3,314
  • 3
  • 25
  • 26
107
votes
7 answers

Angularjs minify best practice

I'm reading http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html and it turned out that angularjs dependency injection has problems if you minify your javascript so I'm wondering if instead of var…
Whisher
  • 31,320
  • 32
  • 120
  • 201
106
votes
7 answers

Dependency Injection & Singleton Design pattern

How do we identify when to use dependency injection or singleton pattern. I have read in lot of websites where they say "Use Dependency injection over singleton pattern". But I am not sure if I totally agree with them. For my small or medium scale…
SysAdmin
  • 5,455
  • 8
  • 33
  • 34
105
votes
7 answers

Python mock Patch os.environ and return value

Unit testing conn() using mock: app.py import mysql.connector import os, urlparse def conn(): if "DATABASE_URL" in os.environ: url = urlparse(os.environ["DATABASE_URL"]) g.db = mysql.connector.connect( …
immrsteel
  • 1,333
  • 4
  • 13
  • 18
105
votes
2 answers

How to set up DAGGER dependency injection from scratch in Android project?

How to use Dagger? How to configure Dagger to work in my Android project? I'd like to use Dagger in my Android project, but I find it confusing. EDIT: Dagger2 is also out since 2015 04 15, and it's even more confusing! [This question is a "stub" on…
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
105
votes
16 answers

What are the benefits of dependency injection containers?

I understand benefits of dependency injection itself. Let's take Spring for instance. I also understand benefits of other Spring featureslike AOP, helpers of different kinds, etc. I'm just wondering, what are the benefits of XML configuration such…
Pavel Feldman
  • 4,621
  • 7
  • 30
  • 31
104
votes
9 answers

What is a Pythonic way for Dependency Injection?

Introduction For Java, Dependency Injection works as pure OOP, i.e. you provide an interface to be implemented and in your framework code accept an instance of a class that implements the defined interface. Now for Python, you are able to do the…
bagrat
  • 7,158
  • 6
  • 29
  • 47
103
votes
2 answers

How to retrieve annotated instance from Guice's injector?

Let's say I have a module: Module extends AbstractModule { @Override protected void configure() { bind(String.class). annotatedWith(Names.named("annotation")). toInstance("DELIRIOUS"); } } and I want to test the module and…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
103
votes
8 answers

How to inject a Map using the @Value Spring Annotation?

How can I inject values into a Map from the properties file using the @Value annotation in Spring? My Spring Java class is and I tried using the $, but got the following error message: Could not autowire field: private java.util.Map Test.standard;…
yathirigan
  • 5,619
  • 22
  • 66
  • 104
102
votes
1 answer

When are .NET Core dependency injected instances disposed?

ASP.NET Core uses extension methods on IServiceCollection to set up dependency injection, then when a type is needed it uses the appropriate method to create a new instance: AddTransient - adds a type that is created again each time it's…
Keith
  • 150,284
  • 78
  • 298
  • 434