Usage of IoC Containers; specifically Windsor

99% of the cases it’s one container instance per app. Normally you initialize it in Application_Start (for a web app), like this.

After that, it’s really up to the consumer of the container. For example, some frameworks, like Monorail and ASP.NET MVC allow you to intercept the creation of the instances (the controllers in this case), so you just register the controllers and their dependencies in the container and that’s it, whenever you get a request the container takes care of injecting each controller with its dependencies. See for example this ASP.NET MVC controller.
In these frameworks, you hardly ever need to call or even reference the container in your classes, which is the recommended usage.

Other frameworks don’t let you get in the creation process easily (like Webforms) so you have to resort to hacks like this one, or pull the required dependencies (that is, explicitly calling the container). To pull dependencies, use a static gateway to the container like this one or the one described by maxnk. Note that by doing this, you’re actually using the container as a Service Locator, which doesn’t decouple things as well as inversion of control. (see difference here and here)

Hope this clears your doubts.

Leave a Comment