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
34
votes
1 answer

Ninject dependency injection with Decorator pattern

Say, I have such classes hierarchy: public interface IRepository { } public class SomeSimpleRepository : IRepository {} Now I want to "decorate" SomeSimpleRepository with additional functions public class MoreAdvancedRespository : IRepository { …
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
34
votes
6 answers

How to use Dependency Injection with ASP.NET Web Forms

I am trying to work out a way to use dependency injection with ASP.NET Web Forms controls. I have got lots of controls that create repositories directly, and use those to access and bind to data etc. I am looking for a pattern where I can pass…
Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113
34
votes
3 answers

Arguments against Inversion of Control containers

Seems like everyone is moving towards IoC containers. I've tried to "grok" it for a while, and as much as I don't want to be the one driver to go the wrong way on the highway, it still doesn't pass the test of common sense to me. Let me explain, and…
vivri
  • 885
  • 2
  • 12
  • 23
33
votes
3 answers

MVVM and IOC: Handling View Model's Class Invariants

This is an issue I've been struggling with since I started using MVVM, first in WPF and now in Silverlight. I use an IOC container to manage the resolution of Views and ViewModels. Views tend to be very basic, with a default constructor, but…
Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
33
votes
5 answers

How to use Castle Windsor with ASP.Net web forms?

I am trying to wire up dependency injection with Windsor to standard asp.net web forms. I think I have achieved this using a HttpModule and a CustomAttribute (code shown below), although the solution seems a little clunky and was wondering if there…
Xian
  • 76,121
  • 12
  • 43
  • 49
32
votes
5 answers

Dependency Injection - new instance required in several of a classes methods

I have some code that looks something like this: public MyService(IDependency dependency) { _dependency = dependency; } public Message Method1() { _dependency.DoSomething(); } public Message Method2() { _dependency.DoSomething(); …
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
32
votes
1 answer

How to create a child scope from the parent with default dependency injection in .NET Core?

I am building a console .NET Core application. It periodically runs a method that does some work. How do I make ServiceProvider behave in the same way it behaves in ASP.NET Core apps. I want it to resolve scoped services when the method begins it's…
x2bool
  • 2,766
  • 5
  • 26
  • 33
31
votes
12 answers

Code your own IOC Container

Has anyone out there written their own IOC Container in C#? Or do the vast majority of folks use the various frameworks such as Spring. What are the pro's and con's of each?
Peanut
  • 18,967
  • 20
  • 72
  • 78
31
votes
4 answers

IoC in class library. Where to bootstrap

I'm using a class library that can be reused by other components. In this class library I'm using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I'm uncertain about is…
Patrick
  • 2,730
  • 4
  • 33
  • 55
30
votes
4 answers

How to register a Controller into ASP.NET MVC when the controller class is in a different assembly?

My goal is to modify asp.net mvc's controller registery so that I can create controllers and views in a separate (child) assembly, and just copy the View files and the DLLs to the host MVC application and the new controllers are effectively "Plugged…
quakkels
  • 11,676
  • 24
  • 92
  • 149
30
votes
5 answers

how to implement IOC without a global static service (non-service locator solution)?

we want to use Unity for IOC. All i've seen is the implementation that there is one global static service (let's call it the the IOCService) which holds a reference to the Unity container, which registers all interface/class combinations and every…
Michel
  • 23,085
  • 46
  • 152
  • 242
30
votes
6 answers

How to use Property Injection with AutoFac?

In a Console application, I'm using Log4Net and in the Main method I'm getting the logger object. Now, I'd like to make this log object available in all my classes by letting all the classes inherit from a BaseClass which has a ILog property and is…
The Light
  • 26,341
  • 62
  • 176
  • 258
29
votes
1 answer

Dependency Injection and IDisposable

I'm a little bit confused about Dispose() methods in IDisposable implementations with Autofac usage Say I have a certain depth to my objects: Controller depends on IManager; Manager depends on IRepository; Repository depends on ISession; ISession…
Igorek
  • 15,716
  • 3
  • 54
  • 92
29
votes
7 answers

Spring 3.2 Autowire generic types

So I have a number of generics in Spring 3.2 and ideally my architecture would look something like this. class GenericDao{} class GenericService> { // FAILS @Autowired T_DAO; } @Component class…
29
votes
4 answers

What should I consider when choosing a dependency injection framework for .NET

see also Which C#/.NET Dependency Injection frameworks are worth looking into? There are now many dependency injection frameworks to choose from. You used to often be forced to use a given dependency injection framework due to a library you…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317