title: Castle Windsor Dependency Resolver for MVC 3
body: <p>Since the IoC/DI implementation in MVC 3 is most likely in its final form in the RC,   I'm looking for an updated implementation of the DependencyResolver, IControllerActivator and IViewPageActivator using Caste Windsor.  Are there any examples out there that have been updated for MVC 3 RC?</p>
  
  <p><strong>EDIT #1</strong>
  Implementing a Windsor dependency resolver is indeed trivial, but there's still something missing.  Contrary to Jeff Putz's Ninject example (below), it appears that it's not as simple as that with Windsor. After setting the dependency resolver like so,</p>
  
  <pre><code>DependencyResolver.SetResolver(new WindsorDependencyResolver(container)); 
  </code></pre>
  
  <p>Windsor throws ComponentNotFoundException. I need to provide implementations for IControllerFactory and IControllerActivator.  Since the DefaultControllerFactory is DependencyResolver aware, this can be solved as follows:</p>
  
  <pre><code>Component.For&lt;IControllerFactory &gt;().ImplementedBy&lt;DefaultControllerFactory&gt;()
  Component.For&lt;IControllerActivator &gt;().ImplementedBy&lt;WindsorControllerActivator&gt;(),
  </code></pre>
  
  <p>WindsorControllerActivator is trivial as well. However, this leads to another ComponentNotFoundException for IViewPageActivator.</p>
  
  <p>This leads me to believe that I'm missing something. There is no way that this should be more complicated than an implementing a controller factory and calling ControllerBuilder.Current.SetControllerFactory MVC 2.0-style.</p>
  
  <p><strong>EDIT #2</strong>
  I missed the subtle but important detail that the Dependency resolver needs to return null when a service cannot be found.  The implementation is as follows:</p>
  
  <pre><code>public class WindsorDependencyResolver : IDependencyResolver
  {
      private readonly IWindsorContainer container;
  
      public WindsorDependencyResolver(IWindsorContainer container)
      {
          this.container = container;
      }
  
      public object GetService(Type serviceType)
      {
          return container.Kernel.HasComponent(serviceType) ? container.Resolve(serviceType) : null;
      }
  
    public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return container.Kernel.HasComponent(serviceType) ? container.ResolveAll(serviceType).Cast&lt;object&gt;() : new object[]{};
      }
  }
  </code></pre>
  
  <p><strong>EDIT #3</strong></p>
  
  <p>Responding to a question in the comments.  If you do find that you need your own IControllerActivator, here a simple implementation for Windsor:</p>
  
  <pre><code>public class WindsorControllerActivator : IControllerActivator
  {
      private readonly IWindsorContainer container;
  
      public WindsorControllerActivator(IWindsorContainer container)
      {
          this.container = container;
      }
  
      public IController Create(RequestContext requestContext, Type controllerType)
      {
          return (IController)container.GetService(controllerType);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Again, this is <strong>NOT</strong> necessary to get basic DI working with Windsor and the MVC3 dependency resolver.</p>
  
  <p><strong>EDIT #4</strong>
  Based on some further research and feedback, it seems that a traditional controller factory implementation is the best approach for Windsor and MVC3.  The concern is that the IDependencyResolver interface lacks a release method, which could cause memory leaks with Windsor not disposing its components.  This is probably not going to be an issue if all of your dependencies are resolved with the PerWebRequest lifecycle, but it's still better not to take the chance.  Here's a basic implementation of a Windsor controller factory for MVC3.  </p>
  
  <pre><code>public class WindsorControllerFactory : DefaultControllerFactory
  {
      private readonly IWindsorContainer container;
  
      public WindsorControllerFactory(IWindsorContainer container)
      {
          this.container = container;
      }
  
      public override void ReleaseController(IController controller)
      {
          container.Kernel.ReleaseComponent(controller);
      }
  
      public override IController CreateController(RequestContext requestContext, string controllerName)
      {
          var controllerComponentName = controllerName + "Controller";
          return container.Kernel.Resolve&lt;IController&gt;(controllerComponentName);
      }
  }
  </code></pre>
  
  <p><strong>EDIT #5</strong>
  If you're using MVC areas, the above implementation will not work for you.  You will need to register each controller based on its full name, and override GetControllerInstance instead of CreateController:</p>
  
  <pre><code> protected override IController GetControllerInstance(RequestContext context, Type controllerType)
      {
          if (controllerType != null)
          {
              return (IController)container.Kernel.Resolve(controllerType);
          }
          return null;
      }
  </code></pre>