Castle Windsor 2.0 in ASP.Net MVC 1.0

2009 October 23
by r.claypool

Consider yourself warned:  I’m just getting started with ASP.Net MVC, Dependency Injection (DI) and Inversion of Control (IoC), so this post is more for my benefit than yours.

warning-castle-windsor

ASP.Net MVC 1.0

ASP.Net MVC 1.0 was released in March 2009. Read this tutorial to start learning it. Note that ASP.Net MVC is bin deployable which is very nice if you don’t want to install anything on the production web server.

Inversion of Control and Castle Windsor

Castle Windsor 2.0 was just released this Summer after a very long development period. To start using it, go get the dlls and reference them in your project.

If you are completely new to the IoC pattern, go read James Kovacs’ article and watch these videos.  They will get a typical .Net programmer up to speed pretty fast. You should also go to the canonical source for IoC, but frankly I learned more from the other links and I think you will too.

To learn how to configure the Windsor container in your project, read the project’s documentation and continue with this article for an example in ASP.Net MVC.

Putting them Together

castle-plus-aspnet-mvc

Getting Windsor to work in an ASP.Net MVC application is easy if you know what to do. Most things are that way, huh?

For a bare bones integration, just follow these steps:

  1. Install ASP.NET MVC 1.0.  The Web Platform Installer is great for this and a ton of other projects.  Try it.  You’ll be glad you did.
  2. Open VS 2008,  go to File->New->Project… and select the “ASP.NET MVC Web Application” project template.
  3. Select “No” on the proceeding dialog. Do not create a unit test project.
  4. Download Castle Windsor 2.0
  5. Copy the dlls (Castle.DynamicProxy2, Castle.MicroKernel, Castle.Windsor and Castle.Core) to your solution’s folder or a sub folder.
  6. Add them as references to the web project. (Right-click the “References” folder, select “Add Reference…”, select the “Browse” tab and navigate to the dlls to add them.)
  7. Add a new XML file to the web project.  Name it “windsor.config” and enter the following:
  8. <?xml version="1.0" encoding="utf-8" ?>
    <!--Note: Since this is a web application, all components must have a lifesytle of 'transient' or 'preWebRequest' -->
    <configuration>
      <components>
        <!--This is where we could declaratively add components to the Windsor container if we wanted to.-->
      </components>
    </configuration>
  9. Create an interface IMessageHolder and a class MessageHolder.  (We will use this code to illustrate injecting an instance of MessageHolder into the HomeController.)
  10. public interface IMessageHolder
    {
    	string GetMessage();
    }
     
    public class MessageHolder : IMessageHolder
    {
    	#region IMessageHolder Members
     
    	public string GetMessage()
    	{
    		return "Welcome to ASP.NET MVC using Castle Windsor 2.0!";
    	}
     
    	#endregion
    }
  11. Create a controller factory for Windsor:
  12. public class WindsorControllerFactory : DefaultControllerFactory
    {
    	private readonly IWindsorContainer _container;
     
    	public WindsorControllerFactory(IWindsorContainer container)
    	{
    		_container = container;
    	}
     
    	protected override IController GetControllerInstance(Type controllerType)
    	{
    		if (controllerType == null)
    		{
    			// Force an exception message from the base class.
    			// This will be more informative than what we would get otherwise,
    			// see: http://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp/721313#721313
    			return base.GetControllerInstance(controllerType);
    		}
    		// Exception here ????
    		// Check the windsor configuration.
    		// Make sure that Windsor can match the component to load.
    		return _container.Resolve(controllerType) as IController;
    	}
     
    	public override void ReleaseController(IController controller)
    	{
    		_container.Release(controller);
    	}
    }
  13. Open Global.asax.cs and replace it with the following:
  14. public class MvcApplication : HttpApplication
    {
    	/// <remarks>
    	/// This has to be static, because more than one instance
    	/// of the HttpApplication can potentially be created.
    	/// </remarks>
    	private static IWindsorContainer _container;
     
    	public static void RegisterRoutes(RouteCollection routes)
    	{
    		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
    		routes.MapRoute(
    			"Default", // Route name
    			"{controller}/{action}/{id}", // URL with parameters
    			new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    			);
    	}
     
    	protected void Application_Start()
    	{
    		RegisterRoutes(RouteTable.Routes);
    		InitializeWindsor();
     
    		// Setup the MVC framework to use our Windsor controller factory.
    		ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
    	}
     
    	protected virtual void InitializeWindsor()
    	{
    		if (_container == null)
    		{
    			_container = Build("windsor.config");
    		}
    	}
     
    	public static IWindsorContainer Build(string configPath)
    	{
    		var resource = new FileResource(configPath);
    		var interperter = new XmlInterpreter(resource);
    		var container = new WindsorContainer(interperter);
     
    		// Register controllers
    		container.Register(AllTypes
    					   .Of<Controller>()
    					   .FromAssembly(Assembly.GetExecutingAssembly())
    		/* Modify the name with which the component is registered
    		See: http://blog.andreloker.de/post/2009/03/28/ASPNET-MVC-with-Windsor-programmatic-controller-registration.aspx */
    					   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));
     
    		// Register all other types.
    		container.Register(
    			Component.For<IMessageHolder>().ImplementedBy<MessageHolder>().LifeStyle.Transient);
     
    		return container;
    	}
    }
  15. Add a constructor to the HomeController and give it an IMessageHolder parameter.
  16.     [HandleError]
        public class HomeController : Controller
        {
            private readonly IMessageHolder _holder;
     
            public HomeController(IMessageHolder holder)
            {
                _holder = holder;
            }
     
            public ActionResult Index()
            {
                ViewData["Message"] = _holder.GetMessage();
     
                return View();
            }
     
            public ActionResult About()
            {
                return View();
            }
        }

    Since IMessageHolder is registered with Windsor, the container will resolve IMessageHolder to an instance of MessageHolder at run-time. This is called dependency injection because the HomeController dependency is injected through the constructor. Of course, you could do this without an IoC container such as Windsor, but in larger (real world) applications the container will greatly simplify the process of managing dependencies, which means you have a better chance of keeping things loosely coupled and falling into the pit of success.

    Notice also that we display the message in Index() to verify that the Windsor container is giving us a valid instance of the type registered for IMessageHolder. This is just a simple way to see the result of what occurred.

  17. Run the application and you should see the message on the home page. Click here to download the sample project.
  18. aspnet-and-windsor-integration

Project Source Code for this post.

I hope that this walk-through helps someone get started with Windsor. It really is an incredible project to learn and if used correctly it will make your programs much better. Have fun with it and Happy Programming!

2 Responses leave one →
  1. 2009 November 12
    BillB permalink

    Thanks, much for his post. I’ll check back to see if you’ve added anything more of what you’ve learned about Windsor. I’m using it in an MVC project but it’s still a bit of a black box – it’s on my todo list to learn more. Hope you beat me to it. :)

  2. 2010 February 10

    This project (sutekishop) is an awesome place to see MVC and Windsor working together in an enterprise level app. I learned a ton just by stepping through the code.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Creative Commons Attribution 3.0 United States
Creative Commons Attribution 3.0 United States