Questions tagged [dependency-injection]

Dependency Injection, is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties). It is a special form of the more general dependency inversion.

Dependency injection is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties).

Use the tag for:

  • The principles of dependency injection
  • When/How/Should one use dependency injection in a given, detailed situation
  • The way the injection is performed (at construction, at the call of a method, or via properties)
  • Usage of dependency injection along with other design patterns, or/and in unusual situations

More information:

Disambiguation:

658 questions
121
votes
9 answers

Understanding dependency injection

I'm reading about dependency injection (DI). To me, it is a very complicated thing to do, as I was reading it was referencing inversion of control (IoC) as well and such I felt I was going to be in for a journey. This is my understanding: Instead of…
97
votes
18 answers

Dependency injection: How to sell it

Let it be known that I am a big fan of dependency injection (DI) and automated testing. I could talk all day about it. Background Recently, our team just got this big project that is to built from scratch. It is a strategic application with complex…
Mel
  • 1,121
33
votes
4 answers

Does "Inversion of Control" promote "Anemic Domain Model"?

When I used IoC Container in my last project, I ended up with anemic entities and most of my business logic in Stateless Services. I have seen projects written by other developers that utilize "Inversion of Control" and they are always…
Mag20
  • 3,301
12
votes
3 answers

How many injections is acceptable in one class when using dependency injection

I'm using Unity in C# for dependency injection, but the question should be applicable for any language and framework that is using dependency injection. I'm trying to follow the SOLID-principle and therefore I got a lot of abstractions. But now I'm…
smoksnes
  • 473
9
votes
3 answers

How is dependency injection not just moving the complexity into a separate class?

I've been looking into using the Typhoon framework for dependency injection this week. I get that separating the construction of objects is beneficial for replacing arbitrary components with mocks during unit testing, and so far I have seen benefits…
Victor
  • 947
6
votes
5 answers

Is supplying concrete class or primitive type only still called dependency injection?

For example, I remember most examples of dependency injection I see is something like this: public interface Fruit{ } public class FruitBox{ public Fruit fruit; public FruitBox(Fruit fruit){ this.fruit=fruit; } } which can…
ocomfd
  • 5,712
5
votes
3 answers

Is it ok to have many dependencies in a class that just delegates work?

I'm going through the code of the biggest program I've ever created from scratch and seeing if there are things that I can improve in the design. When I first created the program, I was using Singleton everywhere, but since then I've removed them…
5
votes
2 answers

What is a relation between Dependency Injection, single instance, and singletons?

I read So Singletons are bad, then what? which was a great explanation of dependency injection as the solution for inversion of control. The asker assumed singleton and single instance were synonyms, and as others pointed out singleton is a more…
5
votes
3 answers

How often do you use DI container in your ASP.NET MVC application

While reading a book, I came across DI(dependency Injector) and the subsequent DI Container tool. Previously, I developed an application following a tutorial on asp.net website which never used such tool. So, my question can be summed-up in…
Pankaj Upadhyay
  • 5,070
  • 12
  • 44
  • 60
4
votes
2 answers

Why would I choose Unity over Autofac

I'm looking to start a new application and I want to use Dependency Injection. I have a lot of "Microsoft is the only way to go guys" in our shop so of course Unity is the way they wanted to go. However I am leaning more towards Autfac because of…
Mark
  • 277
4
votes
5 answers

Dependency injection into method calls

Let's use PHP here for examples and illustraion purposes but the question is language and framework agnostic. Many experts say Service Locator (anti)pattern should be avoided and recommend using Dependency Injection instead. Then we can find 3 ways…
4
votes
1 answer

Instantiating vs Injection

I'm trying to better understand the pros/cons of instantiating vs injecting in a web application type development. What are the pros & cons for each? When should they be used (what sort of scenario we looking at)?
Aeseir
  • 247
4
votes
1 answer

Data Flow in Dependency Injection

What we have A complex tree model stored across multiple XML files Java model classes which represent the XML model All classes as POJOs All classes annotated with jaxb jaxb to read the XML files A tree of handler classes each handler is…
3
votes
4 answers

Shouldn't the scope of an injected dependency be dictated by the underlying implementation?

So, I'm binding an InMemoryCachedCarRepositoryDecorator to an ICarRepository, and obviously I should bind it as a singleton, because otherwise it wouldn't work (as intended). So this got me thinking: Shouldn't the implementation dictate the scope of…
3
votes
3 answers

How do things other than services fit into the Dependency Injection pattern?

Wikipedia explains: A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. But studying AngularJS's DI implementation, I noticed it includes many…
J.Todd
  • 3,823
1
2 3