Rickard Nilsson

  • Blog
  • Archive
  • About
  • Contact

Welcome to rickardnilsson.net

Rickard Nilsson is a software architect, developer, craftsman, agile enthusiast, and father of three... More

Rickard blogs about crafting software using .NET tooling and solid, development practices.

Follow @rickardn

Top Posts

  • Applying stylesheets dynamically with jQuery
  • My customized Son of Obsidian Visual Studio color scheme for ReSharper
  • .NET Development with Visual Studio on MacBook Pro
  • Code Kata Cast
  • ReSharper User tip #2: Refactor rename namespace
  • Combining and minifying JavaScript and CSS files with Ajax Minifier
  • Dependency injection in ASP.NET MVC with Unity IoC Container
  • C# REPL and Interactive interpreter

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
  • C# 4.0
  • Continuous Integration
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • Open source
  • Personal
  • Review
  • Social media
  • Software development
  • TDD
  • Testing
  • Umbraco
  • Unit testing
  • Unity
  • User tip
  • Web development

Five most recent posts

  • How to unit test your database code when using ServiceStack OrmLite
  • Extract class - ReSharper Ninja tricks
  • ASP.NET MVC 3 Template with built in JavaScript and CSS merging and minification
  • ReSharper Ninja tricks - Generate code from usage
  • Unit testing continuously

Tag cloud

  • agile
  • blogengine.net
  • c#
  • code kata
  • codegarden11
  • continuous integration
  • css
  • dependency injection
  • fakes
  • iso 8601
  • javascript
  • jquery
  • refactoring
  • resharper
  • resharper usertip
  • tdd
  • testing
  • umbraco
  • unit test
  • unit testing
  • visual studio

Recent comments

<< Isolate your code from ASP.NET with Moles Isolation Framework | iPhone developer >>

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

Sunday, 6 June 2010 23:30 by Rickard Nilsson

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)

Related posts

Dependency injection in ASP.NET MVC with Unity IoC ContainerEnabling truly easy testability with ASP.NET MVC Controllers requires a Dependency Injection contain...ASP.NET MVC 3 Template with built in JavaScript and CSS merging and minificationIn a previous post I described the importance of optimizing your web site resources and how you can ...Isolate your code from ASP.NET with Moles Isolation FrameworkI will show how easy it is to isolate your client code from ASP.NET code, using the Moles Isolation ...

Comments

August 8. 2010 13:02

trackback

Dependency injection in ASP.NET MVC with Unity IoC Container

Dependency injection in ASP.NET MVC with Unity IoC Container

Rickard Nilsson

 
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