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

232
votes
13answers
60k 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, ...
87
votes
4answers
26k views

Difference between Dependency Injection (DI) & 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 ...
85
votes
18answers
8k views

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 ...
79
votes
6answers
75k views

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

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 ...
73
votes
7answers
5k views

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 ...
42
votes
10answers
5k 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 ...
41
votes
5answers
7k 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 ...
30
votes
3answers
3k views

How does dependecy injection increase coupling?

On the Wikipedia page on dependency injection, the disadvantages section tells us this: Dependency injection increases coupling by requiring the user of a subsystem to provide for the needs of ...
30
votes
6answers
5k 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 ...
18
votes
4answers
2k 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 ...
18
votes
5answers
9k views

What's the best way to build a factory using NInject?

I am pretty comfortable with dependency injection using NInject in MVC3. While working in an MVC3 application, I developed a custom Controller Creation Factory using NInject, so any controller that is ...
17
votes
6answers
960 views

Should the usage of DI/IoC remove all occurrences of the “new” keyword?

Should the usage of Dependency Injection and an Inversion of Control container remove all occurrences of the "new" keyword from your code? In other words, should every object/dependency, no matter ...
17
votes
6answers
5k 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 ...
16
votes
8answers
1k views

Is Dependency Injection worth it outside of UnitTesting

Given a constructor that will never, ever, have to use any different implementations of several objects that it initializes, is it still practical to use DI? After all, we might still want to unit ...
15
votes
3answers
8k views

What is constructor injection?

I have been looking at the terms constructor injection and dependency injection while going through articles on (Service locator) design patterns. When I googled about constructor injection, I got ...
15
votes
2answers
717 views

Is DDD-Lite a pattern language for dependency injection?

I stumbled upon Greg Young's talk 7 Reasons why DDD Projects Fail where he mentions something he calls DDD-Lite at 7:20. Summarizing, he basically says some use DDD as a pattern languages (entities, ...
15
votes
3answers
2k 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: ...
14
votes
3answers
3k views

Interception vs Injection: a framework architecture decision

There's this framework that I'm helping to design. There are some common tasks that should be done using some common components : Logging, Caching and raising events in particular. I am not sure if ...
14
votes
6answers
579 views

Do abstractions have to reduce code readability?

A good developer I work with told me recently about some difficulty he had in implementing a feature in some code we had inherited; he said the problem was that the code was difficult to follow. From ...
13
votes
5answers
1k views

Does following TDD inevitably lead to DI?

I learned to do Test Driven Development (TDD), Dependency Injection (DI) and Inversion of Control (IoC) all at the same time. When I write code using TDD I always end up using DI in my class's ...
12
votes
1answer
2k 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 ...
11
votes
3answers
385 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 ...
11
votes
2answers
1k 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 ...
10
votes
4answers
637 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 ...
9
votes
5answers
1k views

Can I use Dependency Injection without breaking Encapsulation?

Here is my Solution and projects: BookStore (solution) BookStore.Coupler (project) Bootstrapper.cs BookStore.Domain (project) CreateBookCommandValidator.cs CompositeValidator.cs IValidate.cs ...
9
votes
4answers
1k views

When to use interfaces (unit testing, IoC?)

I suspect I've made a schoolboy error here, and am looking for clarification. A lot of the classes in my solution (C#) - dare I say the majority - I've ended up writing a corresponding interface for. ...
9
votes
2answers
676 views

What's the practical difference between the styles of dependency injection?

I'm new to dependency injection and I have a few questions about which style I shouldI use in my applications. I've just read Inversion of Control Containers and the Dependency Injection pattern by ...
9
votes
1answer
1k views

Multiple Zend application code organisation

For the past year I have been working on a series of applications all based on the Zend framework and centered on a complex business logic that all applications must have access to even if they don't ...
9
votes
3answers
2k views

Which classes should be autowired by Spring (when to use dependency injection)?

I have been using Dependency Injection in Spring for some time now, and I understand how it works and what are some pros and cons of using it. However, when I'm creating a new class I often wonder - ...
8
votes
4answers
783 views

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 ...
8
votes
5answers
528 views

How to TDD test that objects are being added to a collection if the collection is private?

Assume that I planned to write a class that worked something like this: public class GameCharacter { private Collection<CharacterEffect> _collection; public void Add(CharacterEffect e) ...
8
votes
1answer
3k views

Dependency Injection/IoC container practices when writing frameworks

I've used various IoC containers (Castle.Windsor, Autofac, MEF, etc) for .Net in a number of projects. I have found they tend to be frequently abused and encourage a number of bad practices. Are ...
8
votes
2answers
5k 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
951 views

Keeping an MVC model loosely coupled from the DB?

I like to keep my code testable and have decided to go with the Dependency-Injection strategy for my current MVC framework, which definitely has proven to be a great way to ensure loosely coupled ...
7
votes
4answers
4k views

How to use Dependency Injection in conjunction with the Factory pattern

Consider a module that is responsible for parsing files of any given type. I am thinking of using the strategy pattern to tackle this problem as I have already explained over here. Please refer to the ...
7
votes
3answers
560 views

Is the architecture of my project a common one? What's its name?

I am responsible for a software project for a few years now (a Java desktop application, which is mostly event-driven). I started it from scratch, and at the beginning of this project I made some ...
7
votes
1answer
591 views

Problem with understanding “seam” word

I'm reading "Dependency Injection in .NET" of Mark Seemann (it's fantastic, and must have) and author often uses word "SEAM", but I can't understand what does it means? Here is an example of using ...
7
votes
1answer
284 views

Who should initialize dependencies in a TDD application?

I'm trying to learn implementing TDD with mocking/fake objects. One of the questions I have is how to initialize a dependency in an application which implements TDD? An example from this article ...
7
votes
2answers
504 views

How do I inject test objects when the real objects are created dynamically?

I want to make a class testable using dependency injection. But the class creates multiple objects at runtime, and passes different values to their constructor. Here's a simplified example: public ...
7
votes
3answers
2k views

IValidatableObject vs Single Responsibility

I like the extnesibility point of MVC, allowing view models to implement IValidatableObject, and add custom validation. I try to keep my Controllers lean, having this code be the only validation ...
7
votes
2answers
841 views

c++ Model View Presenter: Where to construct presenter?

I'm using the Model View Presenter (MVP) pattern as described in The Humble Dialog Box paper (pdf) with an MFC project. I'm sure the issue is the same with most GUI toolkits. The thing that's ...
6
votes
3answers
405 views

Recommendations on integrating DI/IoC container into an existing application

I am now faced with integrating an inversion of control (IoC) container into an existing application, and I'm looking for some recommendations on how that can most easily be accomplished with the ...
6
votes
4answers
612 views

Should injecting dependencies be done in the ctor or per method?

Consider: public class CtorInjectionExample { public CtorInjectionExample(ISomeRepository SomeRepositoryIn, IOtherRepository OtherRepositoryIn) { this._someRepository = ...
6
votes
3answers
232 views

In dependency injection, is there a simple name for the counterpart of the injected object?

In tutorials and books, I have never seen a single word describing the object that the injected object is injected into. Instead, other terms are used, like "injection point" which don't denote the ...
6
votes
2answers
301 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 ...
6
votes
3answers
218 views

Dependency Injection vs Mixing Levels of Abstraction

I've been reading Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin. One point he makes: G34 Functions should descend only one level of abstraction However, I'm ...
6
votes
5answers
364 views

When to use DI and when to create yourself in Java

I've a decent amount of OOP with various languages but am pretty new to Java. I'm reading through a lot of tutorials where large numbers of objects are created within the code of a class, and I'm ...
6
votes
4answers
3k 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 ...
6
votes
2answers
391 views

How to use DI and DI containers

I am building a small PHP mvc framework (yes, yet another one), mostly for learning purposes, and I am trying to do it the right way, so I'd like to use a DI container, but I am not asking which one ...
6
votes
4answers
689 views

Acceptable placement of the composition root using dependency injection and inversion of control containers

I've read in several sources including Mark Seemann's 'Ploeh' blog about how the appropriate placement of the composition root of an IoC container is as close as possible to the entry point of an ...