Questions tagged [inversion-of-control]

Inversion of control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

In traditional programming the flow of the business logic is controlled by a central piece of code, which calls reusable subroutines that perform specific functions. Using Inversion of Control this "central control" design principle is abandoned. The caller's code deals with the program's execution order, but the business knowledge is encapsulated by the called subroutines. In practice, Inversion of Control is a style of software construction where reusable generic code controls the execution of problem-specific code. It carries the strong connotation that the reusable code and the problem-specific code are developed independently, which often results in a single integrated application. Inversion of Control as a design guideline serves the following purposes:

  • There is a decoupling of the execution of a certain task from implementation.
  • Every system can focus on what it is designed for.
  • The systems make no assumptions about what other systems do or should do.
  • Replacing systems will have no side effect on other systems.

Dependency injection and Inversion of Control are closely related. The difference between them is discussed in this question.

Wikipedia: Inversion of Control

Related Patterns

4379 questions
152
votes
5 answers

Is there a pattern for initializing objects created via a DI container

I am trying to get Unity to manage the creation of my objects and I want to have some initialization parameters that are not known until run-time: At the moment the only way I could think of the way to do it is to have an Init method on the…
146
votes
21 answers

Must Dependency Injection come at the expense of Encapsulation?

If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle…
urig
  • 16,016
  • 26
  • 115
  • 184
145
votes
4 answers

Ioc/DI - Why do I have to reference all layers/assemblies in application's entry point?

(Related to this question, EF4: Why does proxy creation have to be enabled when lazy loading is enabled?). I'm new to DI, so bear with me. I understand that the container is in charge of instantiating all of my registered types but in order to do so…
diegohb
  • 1,857
  • 2
  • 16
  • 34
137
votes
8 answers

Enterprise Library Unity vs Other IoC Containers

What's pros and cons of using Enterprise Library Unity vs other IoC containers (Windsor, Spring.Net, Autofac ..)?
Yoann. B
  • 11,075
  • 19
  • 69
  • 111
97
votes
4 answers

Using IoC for Unit Testing

How can a IoC Container be used for unit testing? Is it useful to manage mocks in a huge solution (50+ projects) using IoC? Any experiences? Any C# libraries that work well for using it in unit tests?
crauscher
  • 6,528
  • 14
  • 59
  • 85
87
votes
2 answers

What is a composition root in the context of dependency injection?

I am exploring dependency injection and the term composition root is used all over the place. So what is it?
Thomas
  • 2,137
  • 1
  • 17
  • 38
86
votes
2 answers

Why not use an IoC container to resolve dependencies for entities/business objects?

I understand the concept behind DI, but I'm just learning what different IoC containers can do. It seems that most people advocate using IoC containers to wire up stateless services, but what about using them for stateful objects like…
81
votes
7 answers

What do programmers mean when they say, "Code against an interface, not an object."?

I've started the very long and arduous quest to learn and apply TDD to my workflow. I'm under the impression that TDD fits in very well with IoC principles. After browsing some of TDD tagged questions here in SO, I read it's a good idea to program…
delete
78
votes
6 answers

Asp.Net Core: register implementation with multiple interfaces and lifestyle Singleton

Considering the following interface and class definitions: public interface IInterface1 { } public interface IInterface2 { } public class MyClass : IInterface1, IInterface2 { } is there any way to register one instance of MyClass with multiple…
Maxim
  • 1,413
  • 2
  • 10
  • 15
77
votes
1 answer

Resolve IContainer

What is the suggested method of getting the Autofac container from inside a class in the application? Does Autofac provide for resolving an IContainer property on a class or do I need to store the container globally once I've build it?
stimms
  • 42,945
  • 30
  • 96
  • 149
77
votes
1 answer

Application architecture/composition in F#

I have been doing SOLID in C# to a pretty extreme level in recent times and at some point realized I'm essentially not doing much else than composing functions nowadays. And after I recently started looking at F# again, I figured that it would…
TeaDrivenDev
  • 6,591
  • 33
  • 50
75
votes
4 answers

With Unity how do I inject a named dependency into a constructor?

I have the IRespository registered twice (with names) in the following code: // Setup the Client Repository IOC.Container.RegisterType(new InjectionConstructor()); IOC.Container.RegisterType
Vaccano
  • 78,325
  • 149
  • 468
  • 850
71
votes
4 answers

MEF vs. any IoC

Looking at Microsoft's Managed Extensibility Framework (MEF) and various IoC containers (such as Unity), I am failing to see when to use one type of solution over the other. More specifically, it seems like MEF handles most IoC type patterns and…
PureCognition
  • 1,253
  • 2
  • 10
  • 14
64
votes
6 answers

Factory method with DI and IoC

I am familiar with these patterns but still don't know how to handle following situation: public class CarFactory { public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6) { } public ICar CreateCar(type) { …
MistyK
  • 6,055
  • 2
  • 42
  • 76
64
votes
1 answer

Understanding IoC Containers and Dependency Injection

My understanding: A dependency is when an instance of ClassA requires an instance of ClassB to instantiate a new instance of ClassA. A dependency injection is when ClassA is passed an instance of ClassB, either through a parameter in ClassA's…
echochamber
  • 1,835
  • 1
  • 15
  • 18