Software Mechanics
Why do we even have that lever?

Using Unity and the ASP.NET MVC Preview 2

March 13, 2008 07:40 by Chris

The recent release of the ASP.NET MVC Framework made a change to the IControllerFactory interface at the request of users of dependency injection containers. Instead of passing the controller factory the type of controller desired, it now gets the string from the route, and the factory can now resolve that type however it wishes.

This fits with most other DI containers, since they have to have everything pre-configured anyway. However, Unity is a little different, in that you don't have to register concrete types ahead of time. This change requires Unity users to register types ahead of time like everyone else, or do reflection to find controller types at runtime. Kind of annoying.

However, there's another answer. Recent (ahem) discussions on the net has shown an obsession with interfaces, to the point that even when there's a useful base class that will solve the problem, many will go straight to the interface and duplicate a lot of work.

In this case, there's a simple way to hook Unity up to ASP.NET MVC preview 2. Rather than implement IControllerFactory, inherit from DefaultControllerFactory instead. There's a method in there, GetControllerInstance, which is called after the name has already been resolved to a type. In other words, the DefaultControllerFactory already does the reflection for you.

Here's the code:

    public class UnityControllerFactory : DefaultControllerFactory
    {
        IUnityContainer container;

        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
        }

        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }
            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException("Type requested is not a controller", "controllerType");
            }

            return container.Resolve(controllerType) as IController;
 }

To hook this up, in global.asax.cs, do something like this:

            IUnityContainer container = new UnityContainer();

            // Configure container here

            IControllerFactory controllerFactory = new UnityControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);

Simple and easy! Hope this helps!

 


Currently rated 1.5 by 2 people

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: ,
Categories: .NET | Unity
Actions: E-mail | Permalink | Comments (5) | Comment RSSRSS comment feed

Related posts

Comments

March 13. 2008 09:33

David Hayden

Awesome, Chris! I was hoping you would post something like this.

David Hayden

March 14. 2008 07:34

pingback

Pingback from samgentile.com

New and Notable 227 - Sam Gentile

samgentile.com

March 15. 2008 14:10

pingback

Pingback from alvinashcraft.com

Dew Drop - March 15, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

March 17. 2008 20:47

pingback

Pingback from vdeohplus.com

New Unity IoC Screencast - Includes Unity Tutorials on Common Tasks

vdeohplus.com

April 10. 2008 01:03

pingback

Pingback from rbazinet.wordpress.com

Great Conversation with Chris Tavares about Microsoft Unity « Accidental Technologist

rbazinet.wordpress.com

Comments are closed