• Blog
  • Archive
  • About
  • Contact
Sign in

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Top Posts

  • Applying stylesheets dynamically with jQuery
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Code Kata Cast
  • Dependency injection in ASP.NET MVC with Unity IoC Container
  • Isolate your code from ASP.NET with Moles Isolation Framework

Sites I've visited recently

  • Ninetech - Affärsnytta med IT
  • JetBrains Team City
  • Vimeo

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET 4
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • Continuous Integration
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • Review
  • TDD
  • Testing
  • Unit testing
  • Unity
  • User tip

Five most recent posts

  • Combining and minifying JavaScript and CSS files with Ajax Minifier
  • Continuous Integration in .NET - Book review
  • How we practice Continuous Integration with Team City and ASP.NET 4
  • Slides from my talk on Continuous Integration
  • Prime Factors Kata in C#

Tag cloud

  • agile
  • blogengine.net
  • c#
  • code kata
  • continuous deployment
  • continuous integration
  • css
  • dependency injection
  • fakes
  • getweekofyear
  • iso 8601
  • isolation framework
  • javascript
  • jquery
  • microsoft research
  • mocks
  • refactor
  • refactoring
  • resharper
  • stubs
  • tdd
  • test coverage
  • testing
  • unit test
  • unit testing

ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container

Sunday, 6 June 2010 23:30 by Rickard

Update! Download sample project MvcWithUnity.VS2010.zip (727,52 kb)

This is an update to a previous post on MVC and Unity: Dependency injection in ASP.NET MVC with Unity IoC Container

After my previous post, new versions of both ASP.NET MVC and Unity has been released and some confusion about their compatibility has shown up on the Internet. The first hit on Google on the topic is this which is completely wrong and uninformed. BillKrat writes:

"MVC2 will pull the rug out from under these blogs because the IControllerFactory interface for CreateController no longer provides a "type" - it provides a "string" which will simply hold the controllerName; not as easy to work with."

In fact, this is not the case at all. The following snippet is fresh out of the code repository from the MVC 1.0 relase:

public interface IControllerFactory {
    IController CreateController(RequestContext requestContext, string controllerName);
    void ReleaseController(IController controller);
}

thus, the interface has not changed at all, and all samples on integrating ASP.NET MVC with Unity still apply!

What BillKrat has done is to mistake the interface for the base implementation in System.Web.Mvc.DefaultControllerFactory. If we subclass DefaultControllerFactorty all we need to do is this (as I’ve explained previously):

public class UnityControllerFactory : DefaultControllerFactory {
    private readonly IUnityContainer container;
    public UnityControllerFactory(IUnityContainer container) {
        this.container = container;
    }
    protected override IController GetControllerInstance(
                RequestContext requestContext, Type controllerType) { 
        return container.Resolve(controllerType) as IController; 
    }
}

thus, here we get our type which plays very nicely with Unity’s Resolve method.

Tags:   asp.net mvc 2, unity 2.0, update, correction, dependency injection
Categories:   ASP.NET MVC | Unity
Actions:   | Ping backs (1)
 
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2008-2011 rickardnilsson.net
Creative Commons-licens