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
19
votes
4 answers

Why not pass your IoC container around?

On this AutoFac "Best Practices" page (http://code.google.com/p/autofac/wiki/BestPractices), they say: Don't Pass the Container Around Giving components access to the container, or storing it in a public static property, or making functions like…
Wil Bloodworth
  • 205
  • 2
  • 6
18
votes
1 answer

Why does everyone say dependency injection in ASP.NET webforms is hard when PageHandlerFactory and IHttpHandlerFactory exist?

So I have a legacy webforms site and am working on making it easier to maintain. Chucking it away and rewriting it isn't an option. IoC is obviously one of the first things it got, but this leaves me with the service-locator pattern and a bad…
18
votes
4 answers

What does this mean in Prism/Unity: Container.Resolve()

(from the StockTraderRIBootstrapper.cs file in the Prism V2 StockTrader example app) What is the difference between this: ShellPresenter presenter = new ShellPresenter(); and this: ShellPresenter presenter = Container.Resolve(); I…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
18
votes
5 answers

Mocking 'System.Console' behaviour

Is there a standard way of making a C# console application unit-testable by programming against an interface, rather than System.Console? For example, using an IConsole interface? Have you done this, and what kind of methods did you use? Did you…
Jonathan
  • 32,202
  • 38
  • 137
  • 208
18
votes
4 answers

What should be the strategy of unit testing when using IoC?

After all what I have read about Dependency Injection and IoC I have decided to try to use Windsor Container within our application (it's a 50K LOC multi-layer web app, so I hope it's not an overkill there). I have used a simple static class for…
17
votes
3 answers

Spring autowire and prototype scope

I have a class named Bar with the following annotation: @Configurable(autowire = Autowire.BY_TYPE) On a private member I have the following annotation: @Autowired(required = true) private Foo foo; In the spring configuration I have a bean of class…
Avner Levy
  • 6,601
  • 9
  • 53
  • 92
17
votes
6 answers

How to overwrite a component with castle windsor?

I want to redefine an (default) implementation in a given windsor-container. Is that what OverWrite is for? Doesn't work, though. container.Register( Component.For() …
17
votes
3 answers

How to use Autofac in a class library project?

I have the following implementation: private INewsRepository newsRepository; public NewsService(INewsRepository newsRepository) { this.newsRepository = newsRepository; } This service is in a separate project than that of my web project. …
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
17
votes
5 answers

Remove Dependency on IoC Container

After reading more and more about IoC containers, I read this post about not having IoC.Resolve() etc in your code. I'm really curious to know then, how can I remove the dependency on the container? I'll want to write code like the following: public…
TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116
17
votes
4 answers

structuremap - two implementations of same interface

I have a service class with the following ctor: public class (IMessageService emailService, IMessageService smsService) { ... } and two implementations of IMessageService (email and sms). How do I configure the container to resolve this…
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
17
votes
1 answer

How to configure unity container to provide string constructor value?

This is my dad class public class Dad { public string Name { get;set; } public Dad(string name) { Name = name; } } This is my test method public void TestDad() …
17
votes
4 answers

Is MEF a replacement for System.Addin?

Possible Duplicate: Choosing between MEF and MAF (System.AddIn) Is the Managed Extensibility Framework a replacement for System.Addin? Or are they complementary?
Chris Sutton
  • 2,771
  • 5
  • 26
  • 32
17
votes
7 answers

IoC - Multiple implementations support for a single interface

I am wondering why .Net IoC containers do not easily support multiple implementations for a single interface! May be I am wrong, but as far I have seen, frameworks like Ninject partially supports this feature using annotations (how?). I do not…
Bala
  • 1,193
  • 2
  • 12
  • 34
17
votes
6 answers

Create instance of a class with dependencies using Autofac

Problem: Assume the class: public class MyAwesomeClass { private IDependCls _dependCls; public MyAwesomeClass(IDependCls dependCls) { _dependCls = dependCls; } } And somewhere else I need to get an instance of that class, like…
17
votes
3 answers

Where to place and configure IoC container in a WPF application?

I am working on a middle sized WPF application (MVVM) that should be extensible and maintainable in the future. Thus I decided to use an IoC container (Unity in this case) to keep things flexible. However I am not sure where to place and configure…
matori82
  • 3,669
  • 9
  • 42
  • 64