Creating controllers using Castle Windsor Project - Part 2

Published by on
Tags:

Further to my last post on using the Castle Windsor Project to inject dependencies into your controllers you will also need to update the web.config. To do this find the line:

<add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler,
 System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Replace this line with line that refers to the WindsorControllerFactory:

<add verb="*" path="*.mvc" validate="false" type="Custom.Web.Mvc.WindsorControllerFactory, Custom"/>

Creating controllers using Castle Windsor Project

Published by on
Tags:

Your controllers in the MVC pattern are going to need access to service classes, rather than instantiate a concrete version of the service class in a controller it would be better to inject the services we needed into the controller. Firstly we need to ensure that the constructor of the controller has the interface of the service we want to use:

The default controller factory System.Web.Mvc.DefaultControllerFactory requires all controllers to have a parameterless constructor, so we have to create another controller factory. This factory will get the type of controller we want and then use a Windsor container to create a concrete version of it:

By inheriting from from the DefaultControllerFactory we can get it to do all the work of actually working out which type of controller we wanted. We then have to add the new controller to the Windsor container so that it is aware of the controller (this saves us configuring each controller in the config file), next the type is passed to Windsor to create a concrete version.

 

All blogs tagged with 'castle'

Projects

Have you read?

Disable HTML Caching in Sitecore



Blogs by date