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
15
votes
3 answers

Ninject scoping - use same instance across entire graph being constructed

Let's say I have the following classes that I want to construct using Ninject, with the arrows showing dependencies. A > B > D A > C > D I want to configure Ninject such that A is transient scoped, i.e. every time you ask Ninject for an A, you get…
RationalGeek
  • 9,425
  • 11
  • 62
  • 90
15
votes
5 answers

Domain Driven Design and IoC/Dependency Injection

I'm trying to apply now what I learned about DDD and I'm a little bit confused about the flow of dependencies in the Domain model. My questions are: Should an Entity be aware of Factories, Repositories, Services in the domain? Should a Repository…
15
votes
4 answers

Do Extension Methods Hide Dependencies?

All, Wanted to get a few thoughts on this. Lately I am becoming more and more of a subscriber of "purist" DI/IOC principles when designing/developing. Part of this (a big part) involves making sure there is little coupling between my classes, and…
15
votes
2 answers

Faster alternative than Dictionary?

I'm creating a library which I'm performance testing. In it I generate a Dictionary once. The items are currently inserted in a random order. The dictionary remains unchanged during the application lifetime. It's then frequently used to…
jgauffin
  • 99,844
  • 45
  • 235
  • 372
15
votes
2 answers

Is it appropriate to use Property Injection in a base class when a dependency is only required in the base class?

Example: public abstract class BaseControler : Controller { public IUnitOfWork UnitOfWork { get; set; } } public class HomeController : BaseControler { readonly IUserRepository _userRepository; // :-) public…
Rookian
  • 19,841
  • 28
  • 110
  • 180
14
votes
4 answers

IoC container, check for errors at compile time

I have a simple question. Let's say I have a .Net solution, with different projects like some class libraries (bll, dal, etc) and a main project which can be a web application or a wpf application, it doesn't matter. Now let's say I want to use an…
14
votes
3 answers

What is the Best Way to Organize a ASP.Net MVC Solution Using Dependency Injection?

I am early in development on a new ASP.Net MVC project and I using this project to get into DI. I'm pretty sure that I am going to go with Structure Map, but that isn't what I am asking about. What I am trying to figure out is how best to organize…
14
votes
3 answers

UnityContainer.Resolve or ServiceLocator.GetInstance?

It could seem a stupid question because in my code everything is working, but I've registered a singleton this way with my Unity container _ambientContainer: _ambientContainer.RegisterType(new…
14
votes
3 answers

Autofac Scanning Assemblies for certain class type

I've started using Autofac and want to scan some DLL's and get Autofac to register some of the classes within them. The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registering them. Can…
Jon
  • 38,814
  • 81
  • 233
  • 382
14
votes
3 answers

How to avoid Service Locator Anti-Pattern?

I'm trying to remove a Service Locator from an abstract base class, but I'm not sure what to replace it with. Here is a psuedo-example of what I've got: public abstract class MyController : Controller { protected IKernel kernel; public…
Paul
  • 6,188
  • 1
  • 41
  • 63
14
votes
4 answers

Way to fill collection with Unity

I have two example classes class ClassToResolve { private List _coll; public ClassToResolve(List coll) { _coll = coll; } } class CollectionItem { //... } and I need to resolve…
14
votes
4 answers

Dependency inversion principle in JavaScript

Is anyone able to help illustrate Dependency inversion principle in JavaScript jQuery? Which would highlight and explain these 2 points: A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B.…
14
votes
2 answers

A .NET Unit Test without a parameterless constructor, to facilitate dependency injection

I'm trying to have the unit tests not rely on calling container.Resolve() for their dependencies. I'm currently using AutoFac 2.2.4, and tried xUnit.NET and NUnit, but both have this issue: No parameterless constructor defined for this…
14
votes
6 answers

Should I avoid using Dependency Injection and IoC?

In my mid-size project I used static classes for repositories, services etc. and it actually worked very well, even if the most of programmers will expect the opposite. My codebase was very compact, clean and easy to understand. Now I tried to…
14
votes
1 answer

How do I use Common Service Locator in Ninject 2

Changes in Ninject 2 say that Ninject support Common Service Locator, but how do I use it? I don't find any manual or sample.