• 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 MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • TDD
  • Testing
  • Unit testing
  • Unity
  • User tip

Five most recent posts

  • Prime Factors Kata in C#
  • iPhone developer
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Isolate your code from ASP.NET with Moles Isolation Framework
  • Moles Isolation Framework from Microsoft to be compared with TypeMock Isolator

Tag cloud

  • agile
  • ajax
  • asp.net
  • asp.net 3.5
  • asp.net mvc 2
  • bdd
  • blog
  • blogengine.net
  • c#
  • cocoa touch
  • code kata
  • correction
  • css
  • dbc
  • dependency injection
  • design by contract
  • dom
  • douglas crockford
  • fakes
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • httpcontext
  • humble dialog box
  • inversion of control
  • ioc container
  • iphone
  • iphone os
  • iso 8601
  • isolation
  • isolation framework
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • klarsynt
  • live template
  • metaweblog api
  • microsoft research
  • mocks
  • model-view-presenter
  • moles
  • mvp
  • ninetech
  • objective-c
  • patterns & practices
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • roy osherove
  • stubs
  • syntax
  • syntax highlighter
  • tdd
  • tdd masterclass
  • test coverage
  • testing
  • typemock
  • types
  • unit test
  • unity
  • unity 2.0
  • update
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

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:  
Share | Comments (1) | |
 
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2008-2010 rickardnilsson.net
Creative Commons-licens