DI, 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)

learn more… | top users | synonyms

387
votes
13answers
129k views

So Singletons are bad, then what?

There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, ...
62
votes
10answers
10k views

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
50
votes
5answers
13k views

Should I use Dependency Injection or static factories?

When designing a system I am often faced with the problem of having a bunch of modules (logging, database acces, etc) being used by the other modules. The question is, how do I go about providing ...
158
votes
7answers
165k views

What does the Spring framework do? Should I use it? Why or why not?

So, I'm starting a brand-new project in Java, and am considering using Spring. Why am I considering Spring? Because lots of people tell me I should use Spring! Seriously, any time I've tried to get ...
10
votes
4answers
962 views

Gradual approaches to dependency injection

I'm working on making my classes unit-testable, using dependency injection. But some of these classes have a lot of clients, and I'm not ready to refactor all of them to start passing in the ...
72
votes
5answers
20k views

When is it not appropriate to use the dependency injection pattern?

Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with ...
103
votes
4answers
51k views

Difference between Dependency Injection (DI) and Inversion of Control (IOC)

I've been seeing a lot of references of Dependency Injection (DI) & Inversion Of Control (IOC), but I don't really know if there is a difference between them or not. I would like to start using ...
16
votes
3answers
1k views

Legitimate “real work” in a constructor?

I am working on a design, but keep hitting a roadblock. I have a particular class (ModelDef) that is essentially the owner of a complex node tree built by parsing an XML schema (think DOM). I want to ...
31
votes
7answers
4k views

When NOT to apply Dependency Inversion?

I am currently trying to figure out SOLID. So Dependency inversion means that any two classes should communicate via interfaces, not directly. E.g. if class A has a method, that expects a pointer to ...
25
votes
4answers
3k views

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 "...
20
votes
6answers
10k views

Dependency injection ; good practices to reduce boilerplate code

I have a simple question, and I'm not even sure it has an answer but let's try. I'm coding in C++, and using dependancy injection to avoid global state. This works quite well, and I don't run in ...
9
votes
4answers
6k views

Use Dependency Injection For Data Objects?

I'm just learning about dependency injection, and am stuck on something. Dependency Injection recommends sending dependent classes through the constructor, but I'm wondering if this is necessary for ...
91
votes
18answers
11k views

Dependency injection: How to sell it [closed]

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 ...
27
votes
11answers
2k views

Did a gradual shift in methodology of writing code affect system performance? And Should I care?

Lest you be confused by what's to follow, here is the driving idea behind the question: I always intended the question to be what it is. I may not have articulated it well originally. But intent ...
12
votes
2answers
2k views

What is the difference between DI and IoC? [duplicate]

Possible Duplicate: Difference between Dependency Injection (DI) & Inversion of Control (IOC) I'm new to Inversion of Control, Dependency Injection and everything related to these stuff, ...
17
votes
3answers
3k views

Sell me on IoC containers, please

I've seen several recommend use of IoC containers in code. The motivation is simple. Take the following dependency injected code: class UnitUnderTest { std::auto_ptr<Dependency> d_; public: ...
16
votes
2answers
3k views

Domain-Driven-Design - external dependencies in the Entity problem

I'd like to start Domain-Driven-Design, but there are several problems I'd like to solve before starting :) Let's imagine I have a Groups and Users and when user wants to join a group, I'm calling ...
15
votes
4answers
1k views

Why do we need frameworks for dependency injection? [closed]

I've been reading up more on the Inversion of Control principle and Dependency Injection as an implementation of it and am pretty sure I understand it. It seems to be basically saying 'don't declare ...
13
votes
3answers
547 views

Do dependency injection frameworks pose a dependency risk?

I have been refactoring an existing system to use dependency injection, and that work has been going smoothly. After a while I noticed that a large number of in-house libraries became dependent upon ...
10
votes
2answers
8k views

Dependency injection with n-tier Entity Framework solution

I am currently designing an n-tier solution which is using Entity Framework 5 (.net 4) as its data access strategy, but am concerned about how to incorporate dependency injection to make it testable / ...
8
votes
3answers
6k views

Injecting dependencies (DI) in c++ applications

I am playing with dependency injection, but i am not sure I am doing it right. Especially, I am not sure what should be the correct way to build classes with injected dependencies. Say I have a class ...
5
votes
2answers
415 views

Can higher-order functions in FP be interpreted as some kind of dependency injection?

According to this article, in object-oriented programming / design dependency injection involves a dependent consumer, a declaration of a component's dependencies, defined as interface contracts, an ...
3
votes
4answers
3k views

How to handle “circular dependency” in dependency injection

The title says "Circular Dependency", but it is not the correct wording, because to me the design seems solid. However, consider the following scenario, where the blue parts are given from external ...
1
vote
1answer
428 views

Is circular Dependency Injection a good practice?

I'm trying the Dependency Injection pattern in some new classes, more specifically in a Transaction System and stumbled into a dilemma. I have a Person class with a create_new_transaction method, and ...
9
votes
4answers
499 views

Dependency Inversion expands the API, results in unnecessary tests

This question has bothered me for a few days, and it feels like several practices contradict each other. Example Iteration 1 public class FooDao : IFooDao { private IFooConnection fooConnection;...
8
votes
4answers
668 views

What complexity do DI frameworks add?

The currently most upvoted answer to a very recent question states that DI containers are an "enterprise software" pattern, used when the object graph is very large and complex. I suspect that 95% ...
8
votes
4answers
775 views

How could dependency injection be integrated into the language? [closed]

I've been thinking a bit on how dependency injection could be better integrated directly into a C# like language. I've come up with a potential solution I'd like to hear your opinion on. I haven't ...
5
votes
4answers
214 views

How does a Dependency Injection/IOC Container know which implementation to use?

When you use an IoC container, like so: var svc = IoC.Resolve<IShippingService>(); How does the IoC Container choose which implementation of IShippingService to instantiate? Further, if I am ...
3
votes
2answers
721 views

Initialization of objects in a system using dependency injection

This is a follow up question to the following post: Injecting dependencies (DI) in c++ applications In a system that uses DI, someone, somewhere should be responsible to create the various objects ...
2
votes
2answers
422 views

when using dependency injection, should I always pass an interface

When using depending injection, you generally pass everything around as an interface (perhaps with the exception of primitives and strings). That allows you to easily chance the behavior, without ...
1
vote
1answer
219 views

Does merely parameterizing a dependency meet the requirements for Inversion of Control?

When I find a concrete dependency inside an extension method, I have been attempting to remove the (concrete) dependency by parameterising it like so // original implementation public static List<...
0
votes
2answers
3k views

Initialize in Field, Constructor, or Argument

If I know what a field will be initialized to, should I initialize it in the field, constructor, or receive it as a parameter? I am asking about best practices. All three options effectively provide ...