Questions tagged [ioc-container]

In object oriented languages, an inversion of control container (ioc-container) can be used for configuring and managing objects in an application.

In object oriented languages, an inversion of control container (ioc-container) can be used for configuring and managing objects in an application. The container is responsible for managing object lifecycles (creation, initialization, destruction) and configures objects by wiring them together.

Objects managed by the container obtain references to other objects (their dependencies):

  • by explicitly asking the container, using dependency lookup methods on the container
  • automatically, because the container injects the dependencies using one or more dependency injection techniques

Frameworks that provide an ioc-container support one of these methods and often both.

The following frameworks are discussed here at Stackoverflow

For .NET:

For Java:

2163 questions
52
votes
2 answers

Comparing Castle Windsor, Unity and StructureMap

In a follow up to Krzysztof’s statement that Windsor does a lot more than other IoC’s, I wanted to understand how these IoC’s stack up against each other and the benefits/additional facilities that castle Windsor provides. Are there any…
50
votes
9 answers

MVC, EF - DataContext singleton instance Per-Web-Request in Unity

I have a MVC 3 web application, where I am using the Entity Framework for the data access. Furthermore, I have made a simple use of the repository pattern, where e.g. all Product related stuff is handled in the "ProductRepository" and all User…
Nima
  • 937
  • 1
  • 13
  • 20
49
votes
4 answers

Is it better to create a singleton to access unity container or pass it through the application?

I am dipping my toe into using a IoC framework and I have choosen to use Unity. One of the things that I still don't fully understand is how to resolve objects deeper into the application. I suspect I just haven't had the light bulb on moment that…
btlog
  • 4,760
  • 2
  • 29
  • 38
48
votes
3 answers

NInject: Where do you keep your reference to the Kernel?

I'm using NInject on a new web application and there are two things that are unclear to me: Don't I need to keep a reference to the Kernel around (Session/App variable) to insure that GC doesn't collect all my instances? For example, if I specify…
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
48
votes
2 answers

Ninject + Bind generic repository

I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null? I have tried various things like: Bind(typeof(IRepository)).To(typeof(Repository));…
ebb
  • 9,297
  • 18
  • 72
  • 123
47
votes
2 answers

Why is MVC4 using the Service Locator Anti-Pattern?

After reading "Dependency Injection in .NET" by Mark Seemann I stay away from the Service Locator which is an anti-pattern. Upon reading the release notes on MVC 4 I see: Improved Inversion of Control (IoC) via DependencyResolver: Web API now…
47
votes
2 answers

Difference between MEF and IoC containers (like Unity, Autofac, SMap, Ninject, Windsor.Spring.net, etc.)

I have been searching about the dependency injection and read a few articles. But still I am unable to find out the difference between MEF and other IoC's. So, my question is this: In which situation should I prefer to use a MEF or IoC…
46
votes
8 answers

IoC.Resolve vs Constructor Injection

I heard a lot of people saying that it is a bad practice to use IoC.Resolve(), but I never heard a good reason why (if it's all about testing than you can just mock the container, and you're done). now the advantages of using Resolve instead of…
Omu
  • 69,856
  • 92
  • 277
  • 407
44
votes
9 answers

Why are IOC containers unnecessary with dynamic languages

Someone on the Herding Code podcast No. 68, http://herdingcode.com/herding-code-68-new-year-shenanigans/, stated that IOC containers had no place with Python or Javascript, or words to that effect. I'm assuming this is conventional wisdom and that…
mikemay
  • 3,707
  • 6
  • 34
  • 35
44
votes
4 answers

Specify constructor for the Unity IoC container to use

I'm using the Unity IoC container for resolving my objects. However, I've run into an issue. When I have more than one constructor - how does Unity know which one to use? It seems to use the one with parameters when I have one with and one without.…
stiank81
  • 25,418
  • 43
  • 131
  • 202
42
votes
3 answers

@PostConstruct annotation and spring lifecycle

I'm new to Spring, I would like to know: I have a java class annotated with @Component (spring) and inside I have a method annotated with @PostConstruct. The class is then referenced by @Autowired annotated field in another class. Can I assume that…
Daniele
  • 1,332
  • 1
  • 13
  • 24
42
votes
1 answer

Castle Windsor - IoC registration for open generic interfaces?

Does Castle Windsor permit registration of an open generic interface or do I need to register each possible typed instance separately? Example - the below with types T,Z fails upon compilation unless I separately specify T, Z with strong types. …
goldfinger
  • 1,105
  • 1
  • 20
  • 29
41
votes
9 answers

Cannot get rid of "physical connection is not usable" exception

I am about to shoot myself. Spent few weeks now trying to solve this issue. We have an ASP.NET MVC 4 web app that uses SQL Server 2012 and Entity Framework as ORM and Unity for IoC. Web app is hosted on Amazon EC2. I started getting "Physical…
40
votes
1 answer

Which IoC container is better with Prism.Forms

I am in the start of a new Prism.Forms project and I was wondering which of the various IoC containers (Autofac, Dryloc, Ninject or Unity) would be best to move forward with. I do not know if this is true, but I read somewhere that Unity is no…
Martin Robins
  • 6,033
  • 10
  • 58
  • 95
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