Using Castle Windsor with Sitecore MVC for Dependency Injection

September 9, 2012

The preview release of Sitecore 6.6 comes with first class support for ASP.NET MVC. If you want to get serious about MVC programming the first thing you’re going to want to do is to add support for your favourite dependency injection container. In this post I will show you how to add support for Castle Windsor.

First you need to create a new controller factory for ASP.NET MVC that can resolve controller types via Castle Windsor.

If this was standard ASP.NET MVC, you could configure this controller in the Global.asax and be done with it. However, that won’t work with Sitecore MVC as it overrides the controller in the loader pipeline processor, so you will need to override the relevant pipeline processor.

Here’s the code that adds the Castle Windsor controller factory to the Sitecore loader pipeline: