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
10
votes
3 answers

Accessing the DI container

I'm starting a new project and setting up the base to work on. A few questions have risen and I'll probably be asking quite a few in here, hopefully I'll find some answers. First step is to handle dependencies for objects. I've decided to go with…
Andre
  • 3,150
  • 23
  • 23
10
votes
1 answer

Using Simple Injector with Castle Proxy Interceptor

I'm using Simple Injector in my asp.net mvc 4 project. I can't figure out how can I use Simple Injector with castle proxy interceptor.
10
votes
1 answer

Anyway to @Inject/@Autowire an inner class into an outer class?

In Spring/JSR-330, is there a way to properly declare an inner class which requires dependency injection, such that I can inject it into the outer class? For example: @Component public class TestClass{ // How to declare this class? private…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
10
votes
2 answers

asp.net identity 2.0 unity not resolving default user store

i get the following exception when trying to configure Unity using Unity.Mvc5 with an MVC 5 application using Identity 2.0 and the Identity 2.0 Samples boilerplate. i have read this SO Configure Unity DI for ASP.NET Identity and i'm still not clear…
10
votes
6 answers

In Python, what's a good pattern for disabling certain code during unit tests?

In general I want to disable as little code as possible, and I want it to be explicit: I don't want the code being tested to decide whether it's a test or not, I want the test to tell that code "hey, BTW, I'm running a unit test, can you please not…
guidoism
  • 7,820
  • 8
  • 41
  • 59
10
votes
2 answers

Xml configuration or Configuration through code?

I personally like the option to configure StructureMap from C# code. From what I understand, one of the advantages of DI, is that we can easily swap in a new concrete instance. But, if the configuration is defined in code, then the concrete…
Amith George
  • 5,806
  • 2
  • 35
  • 53
10
votes
2 answers

How do you inject jdbiFactory DAOs into a Dropwizard Command?

I'm starting to work with Dropwizard and I'm trying to create a Command that requires to use the database. If someone is wondering why I'd want to do that, I can provide good reasons, but this is not the point of my question anyway. It's about…
ggalmazor
  • 710
  • 6
  • 18
10
votes
3 answers

Windsor resolve IEnumerable

Via Windsor I register multiple implementation types to a single interface type : public class WindsorInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( …
BaltoStar
  • 8,165
  • 17
  • 59
  • 91
10
votes
1 answer

How to Resolve type based on end-user configuration value?

I have an interface (call it IAcmeService) that has multiple implementations. FileSystemAcmeService DatabaseAcmeService NetworkAcmeService The end-user needs to be able to select which implementation will be used and also save that…
Rick
  • 5,029
  • 2
  • 33
  • 34
10
votes
2 answers

Why is the function in angular's DI inline annotation a array element?

I have a question for the angularjs folks here. So, I am using angular for quite a while now. However, every time when I am writing a new Controller or something that is using dependency injection, I find myself miswriting the the inline…
patchrail
  • 2,007
  • 1
  • 23
  • 33
10
votes
2 answers

How to specify an @Alternative producer method in beans.xml?

Say I have: public interface Foo {...} public class AltProducer { private Foo altFoo; @Produces @Alternative public Foo getAltFoo() { return altFoo; } } What do I need to put in beans.xml so that my AltProducer's @Produces method will get…
Kricket
  • 4,049
  • 8
  • 33
  • 46
10
votes
2 answers

Dependency injection with Akka

I use Guice in my application quite a lot. Recently i start to learn akka actors and felt like refactoring my application with it. However upfront i am already wondering how all my guice will work with actors. I went on searching on google and it…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
10
votes
4 answers

DI container, factory, or new for ephemeral objects?

When dealing with objects that require data known only at runtime, such as a username and password, where should object instantiation happen: by using new, in a factory, or in a DI container? For example, I could just new an object once I have the…
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
10
votes
1 answer

Dependency Injection in a Java 7 standalone application

I would like to use dependency injection in a large Java 7 standalone application, but I am not really sure where to start. I have written a small test application: public class Main { @Inject MyInterface myInterface; public static…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
10
votes
6 answers

phpstorm symfony2 missing service warning

I have installed the Symfony2 plugin for Phpstorm but I can't get the IDE to see these existing services or other injected objects. Can these be fixed somehow, so the warnings go away?
androidu
  • 4,678
  • 6
  • 36
  • 51
1 2 3
99
100