Questions tagged [ioc-containers]
50 questions
121
votes
5 answers
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 all the frameworks I've come across seem to favour…

tom6025222
- 984
- 2
- 7
- 10
117
votes
7 answers
Why is Inversion of Control named that way?
The words invert or control are not used at all to define Inversion of Control in the definitions that I've seen.
Definitions
Wikipedia
inversion of control (IoC) is a programming technique, expressed here
in terms of object-oriented programming,…

Korey Hinton
- 2,656
- 3
- 20
- 30
25
votes
6 answers
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 how simple or short-lived, be "registered" within…

CraigTP
- 1,554
- 1
- 17
- 17
18
votes
2 answers
Is there evidence that the use of dependency injection improves outcomes in software engineering?
Notwithstanding its popularity, is there any empirical evidence that shows that Dependency Injection (and/or using a DI container) helps with, say, reducing bug counts, improving maintainability, or increasing development velocity on real-life…

NMrt
- 479
- 3
- 9
18
votes
1 answer
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 there any established practices for IoC container…

Dave Hillier
- 3,940
- 1
- 25
- 37
17
votes
3 answers
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 d_;
public:
UnitUnderTest(
std::auto_ptr d…

Billy ONeal
- 8,073
- 6
- 43
- 57
15
votes
3 answers
I get dependency injection, but can someone help me understand the need for an IoC container?
I apologize if this seems like yet another repeat of the question, but every time I find an article regarding the topic, it mostly just talks about what DI is. So, I get DI, but I'm trying to understand the need for an IoC container, which everyone…

Sinaesthetic
- 302
- 2
- 12
13
votes
6 answers
Questioning one of the arguments for dependency injection frameworks: Why is creating an object graph hard?
Dependency injection frameworks like Google Guice give the following motivation for their usage (source):
To construct an object, you first build its dependencies. But to build each dependency, you need its dependencies, and so on. So when you…

oberlies
- 466
- 4
- 16
10
votes
4 answers
How could dependency injection be integrated into the language?
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 used many dependency injection frameworks so there…

Homde
- 11,104
- 3
- 40
- 68
9
votes
1 answer
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 IServiceCollection extension methods like AddMyServices().
My…

Radek Strugalski
- 199
- 1
- 2
9
votes
3 answers
DI/IoC container vs factories: Where do I configure my application and why?
I am trying to figure out when to use the DIC/IoC registry for configuring my software and when to use factories, along with the reasoning behind either approach.
I am using StructureMap as my DI container (DIC), which is easy to configure using…

packoman
- 291
- 2
- 5
8
votes
4 answers
Acceptable placement of the composition root using dependency injection (DI) and inversion of control (IoC) 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 application.
In the .NET world, these applications…

Jeff
- 215
- 3
- 8
8
votes
3 answers
IOC and stateless services. Short-lived or single-instance?
Given a garbage-collected framework, when using an IOC container to inject purely stateless services, is it generally better to use the container's single-instance lifespan or to recreate the object each time it is used and throw it, and all its…

pdr
- 53,387
- 14
- 137
- 224
6
votes
4 answers
How to pass data between objects when an IoC container is being used?
I'm working with a project that our architect has decided to use dependency injection for almost everything. We use an IoC container. One of the main issues that I keep coming across when using this pattern is "How do I pass this bit of data here…

Jacob Brown
- 178
- 6
6
votes
3 answers
How do I support per-method-call instantiation without referencing the IoC container outside the composition root?
I read with interest this article which states:
A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container.
This means that all the application code relies solely on Constructor…

John Wu
- 26,032
- 10
- 63
- 84