All Things Related to Enterprise Architecture

Enterprise Architecture

Subscribe to Enterprise Architecture: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Enterprise Architecture: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Enterprise Architect Authors: Dana Gardner, Jason Bloomberg, Bob Gourley, Plutora Blog, Adrian Grigoriu

Related Topics: Enterprise Architecture, Azure Cloud on Ulitzer, Microsoft Developer

Blog Feed Post

Upgrading the default ASP.NET MVC project with IoC and the Unity Controller Factory

The ASP.NET MVC framework is one of the coolest things to happen to ASP.NET since its creation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcContrib.Unity;
using Microsoft.Practices.Unity;
using UlyssesAgenda.Controllers;
using System.Web.Security;

namespace UlyssesAgenda
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    // visit

    public class UlyssesApplication : HttpApplication, IUnityContainerAccessor
        public static void RegisterRoutes(RouteCollection routes)

            routes.IgnoreRoute("{filename}.ico"); // IE8 requests this and it tries to hit a controller for it.

                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults


        protected void Application_Start()

        protected virtual void InitializeContainer()
            if (_container == null)
                _container = new UnityContainer();             
                _container.RegisterType(typeof(HomeController), typeof(HomeController));
                _container.RegisterInstance(typeof(AccountController), new AccountController());               
        #region IUnityContainerAccessor Members

        private static UnityContainer _container;

        public static IUnityContainer Container
            get { return _container; }

        IUnityContainer IUnityContainerAccessor.Container
            get { return Container; }

You'll notice that I mapped the HomeController type and allowed Unity to manage the instantiation of it. However, with AccountController I created my own instance. The main reason for this is that account controller relies directly upon a service which relies upon a SqlMembershipProvider instance. If I attempt to constructor-inject the AccountController, I get stuck in a quagmire where I have to register my own instances of SqlMembershipProvider and SqlProfileProvider and much more - and that removes the flexibility I get from defining that stuff in the Web.config file (and potentially breaks other ASP.NET functionality by not having it in that file).. So (just this once!) I am allowing the AccountController to be a manually created instance that is injected by Unity.

Next, I'll be posting about how to convert the default Unit Tests for the AccountController into tests that use Moq rather than hand-mocked POCOs.

Read the original blog entry...

More Stories By Kevin Hoffman

Kevin Hoffman, editor-in-chief of SYS-CON's iPhone Developer's Journal, has been programming since he was 10 and has written everything from DOS shareware to n-tier, enterprise web applications in VB, C++, Delphi, and C. Hoffman is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. He authors The .NET Addict's Blog at .NET Developer's Journal.