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.

Filter by
Sorted by
Tagged with
26 votes
4 answers
8k 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 ...
  • 379
605 votes
13 answers
261k 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, ...
  • 20.5k
97 votes
7 answers
32k 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 ...
  • 2,371
128 votes
11 answers
40k 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 ...
  • 17.1k
262 votes
7 answers
296k 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 ...
  • 3,219
75 votes
4 answers
51k views

Dependency Injection: Field Injection vs Constructor Injection?

I know this is a hot debate and the opinions tend to change over time as to the best approach practice. I used to use exclusively field injection for my classes, until I started reading up on ...
  • 1,219
153 votes
8 answers
71k 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 ...
  • 17.5k
12 votes
4 answers
1k 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 ...
  • 991
8 votes
4 answers
2k 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 ...
  • 2,613
119 votes
4 answers
69k 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 ...
user avatar
44 votes
2 answers
44k views

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is ...
  • 1,709
12 votes
4 answers
18k views

Dependency injection for a library with internal dependencies

Background I am working on a class library in support of a web site. The library combines related APIs from several different vendors, all of which have their own particular nuances and domain ...
  • 24.9k
151 votes
8 answers
60k views

Criticism and disadvantages of dependency injection

Dependency injection (DI) is a well known and fashionable pattern. Most of engineers know its advantages, like: Making isolation in unit testing possible/easy Explicitly defining dependencies of a ...
97 votes
18 answers
13k 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 ...
54 votes
3 answers
89k 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 ...
50 votes
7 answers
12k views

Managing and organizing the massively increased number of classes after switching to SOLID?

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that ...
  • 1,337
48 votes
7 answers
12k views

When NOT to apply the Dependency Inversion Principle?

I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A has a method, that ...
  • 6,955
32 votes
4 answers
4k 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 "...
  • 3,271
27 votes
6 answers
15k 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 ...
  • 383
25 votes
9 answers
14k views

Dependency injection: should I use a framework?

I have recently worked on a Python project where we did dependency injection heavily (because we must in order for the app to be testable), but we didn't use any framework. At times it was a little ...
  • 20.8k
20 votes
2 answers
8k views

Problem with understanding "seam" word

I'm reading "Dependency Injection in .NET" by Mark Seemann (it's fantastic, and must have) and the author often uses the word "seam". But I can't understand what it means. Here is an example of using ...
18 votes
4 answers
21k 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 ...
  • 283
13 votes
4 answers
17k 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 ...
  • 1,065
11 votes
3 answers
4k views

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 ...
  • 453
9 votes
3 answers
14k views

React Native - Is using a singleton the best alternative to DI?

I've been reading a lot about the singleton pattern and how it's "bad" because it makes the classes using it hard to test so it should be avoided. I've read some articles explaining how the singleton ...
1 vote
1 answer
255 views

Proper way of handling large Dependency Injected constructors [duplicate]

I have many dataservices/classes injected into a constructor. public MyClassConstructor (IAclass A, IBclass B, .... , IZclass Z){ ... } The number I have to pass down goes well beyond 100. To ...
  • 19
1 vote
1 answer
3k 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 ...
  • 313
116 votes
5 answers
12k views

What's the difference between using dependency injection with a container and using a service locator?

I understand that directly instantiating dependencies inside a class is considered bad practise. This makes sense as doing so tightly couples everything which in turn makes testing very hard. Almost ...
52 votes
4 answers
17k 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 ...
  • 631
49 votes
3 answers
8k views

Using a "Pass-through (God) Service" is bad, right? [duplicate]

My team has developed a new service layer in our application. They created a bunch of services that implement their interfaces (E.g., ICustomerService, IUserService, etc). That's pretty good so far. ...
44 votes
11 answers
5k views

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

My colleagues like to say "logging/caching/etc. is a cross-cutting concern" and then proceed using the corresponding singleton everywhere. Yet they love IoC and DI. Is it really a valid excuse to ...
  • 4,799
40 votes
12 answers
6k views

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

TD;DR: There was some confusion as to what I was asking, so 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 ...
  • 8,059
31 votes
2 answers
8k 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 ...
  • 491
31 votes
4 answers
12k views

Is Functional Programming a viable alternative to dependency injection patterns?

I have recently been reading a book entitled Functional Programming in C# and it occurs to me that the immutable and stateless nature of functional programming accomplishes similar outcomes to ...
  • 3,305
25 votes
6 answers
3k 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 ...
  • 1,534
19 votes
5 answers
8k 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. ...
17 votes
3 answers
4k 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: ...
  • 8,073
13 votes
3 answers
866 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 ...
13 votes
2 answers
4k 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, so ...
13 votes
2 answers
12k 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 / ...
  • 1,956
10 votes
4 answers
1k 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 ...
  • 11.1k
10 votes
3 answers
2k views

Dependency Injection: Should I create a Car class to hold all of its parts?

I have many cars in my C++ application all contained withing a RaceTrack. Each car consists of hundreds of parts. Each part depends on some other part or two. I've read a lot of SO questions on DI ...
9 votes
3 answers
10k 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 ...
8 votes
2 answers
1k 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 ...
  • 19.3k
8 votes
1 answer
5k views

Should .NET Core class libraries register their own implementations?

Our team had recently a big struggle of deciding whether it is a good practice or not for the .NET Core class libraries to register their own implementations by the fact of providing a ...
8 votes
4 answers
721 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;...
7 votes
1 answer
16k views

Singleton with dependency injection ,is that a good practice in some cases?

It's a good idea to have a Singleton implementation with dependency injection? I have some classes that performs some heavy tasks on instantiation (related to database loading). I would like to use ...
  • 592
7 votes
4 answers
5k views

Are there any alternatives to dependency injection for stateless classes?

I am working on an application whereby I have designed classes to fit into several groups: Immutable: initialised through constructors, uses copy-and-swap idiom (inc. move), can deep copy (i.e. clone)...
6 votes
2 answers
6k views

Circular dependency in dependency injection

we're quite new to DI and we're refactoring our application to use Prism/Unity. We're almost there but got stuck on a circular dependency. I've been reading a lot, there are a lot of similar questions ...
  • 223
6 votes
3 answers
2k views

Dependency Injection with default Construction

Most of my production code has fixed types of dependencies, illustrated below: the House for example, at run time, always depends on the same Kitchen. Therefore, I am wondering what I gain by having ...