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
39
votes
2 answers

Are primitive constructor parameters a bad idea when using an IoC Container?

Standard newbie disclaimer: I'm new to IoC and am getting mixed signals. I'm looking for some guidance on the following situation please. Suppose I have the following interface and implementation: public interface IImageFileGenerator { void…
Jason Down
  • 21,731
  • 12
  • 83
  • 117
39
votes
4 answers

When would you use the Common Service Locator?

I've been looking at the Common Service Locator as a way of abstracting my IoC container but I've been noticing that some people are strongly against this type of this. Do people recommend never using it? Always using it? or sometimes using it? If…
39
votes
1 answer

What is a DI Container?

I am watching this course video about dependency injection and the instructor talked about di-container but did not explain in detail, now I read some articles and I want to confirm that now I am getting this right. Below is simple program and my…
Abdurakhmon
  • 2,813
  • 5
  • 20
  • 41
39
votes
2 answers

Using Dagger 2 to inject into service

I have an app which is basically a service that runs all the time and alarms the user when something happens. When the service creates the alarm, it needs to give it his context so that the alarm can do callbacks to the service when something…
39
votes
9 answers

Which Dependency Injection Tool Should I Use?

I am thinking about using Microsoft Unity for my Dependency Injection tool in our User Interface. Our Middle Tier already uses Castle Windsor, but I am thinking I should stick with Microsoft. Does anyone have any thoughts about what the best…
39
votes
1 answer

Where should I do Injection with Ninject 2+ (and how do I arrange my Modules?)

I have a solution with two relevant (to this question) projects, and a few others; Class library with functionality used by several other projects. ASP.NET MVC application. My question is basically where I should do IoC with Ninject 2,…
Rune Jacobsen
  • 9,907
  • 11
  • 58
  • 75
38
votes
6 answers

Spring IoC and Generic Interface Type

I'm trying to use Spring IoC with an interface like this: public interface ISimpleService { void someOp(T t); T otherOp(); } Can Spring provide IoC based on the generic type argument T? I mean, something like this: public class…
Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
37
votes
8 answers

Is Dependency Injection possible with a WPF application?

I want to start using dependency injection in my WPF application, largely for better unit testability. My app is mostly constructed along the M-V-VM pattern. I'm looking at Autofac for my IoC container, but I don't think that matters too much for…
37
votes
4 answers

Autofac with multiple implementations of the same interface

I'm using Autofac and would like to have multiple implementations of an interface. How can I configure Autofac so to resolve dependencies based on the current type? More specifically, I have one interface and multiple implementations that should be…
Peter
  • 13,733
  • 11
  • 75
  • 122
37
votes
1 answer

Proper Hub dependency lifetime management for SignalR and Castle Windsor

I have some SignalR hubs which may need to access some transient and singleton dependencies. Hooking the creation of the Hub is easy and works just fine however SignalR does its own Dispose() call on the created Hub rather than notifying the…
Ian Yates
  • 1,324
  • 13
  • 24
37
votes
3 answers

Base controller constructor injection in ASP.NET MVC with Unity

I have a base controller in my MVC 5 project which implements some shared functionality. This functionality requires some dependencies. I am using Unity 3 to inject these implementations into my controllers, a pattern which has worked fine until I…
36
votes
5 answers

How to deal with run-time parameters when using lifetime scoping?

Warning, long post ahead. I've been thinking a lot about this lately and I'm struggling to find a satisfying solution here. I will be using C# and autofac for the examples. The problem IoC is great for constructing large trees of stateless services.…
Kugel
  • 19,354
  • 16
  • 71
  • 103
36
votes
4 answers

Android and Dependency Injection

I've been looking around, in vain, for some information on using a dependency injection container in Android development. Specifically, how to override the creation of an Activity in a way that will also work when coming back from being killed (for…
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
36
votes
8 answers

When not to use IoC and DI?

I see lots of articles saying how great IoC and DI are and none about why it isn't so great because it can make code more complex. I see also that IoC shouldn't be in the core part of your code but more for libraries and plug-ins. Articles usually…
35
votes
7 answers

Castle Windsor are there any downsides?

I have been looking into the Castle project and specifically Windsor. I have been so impressed with what is possible with this technology and the benefits of having a such a loosely coupled system are definitely apparent. The only thing I am unsure…