Dependency Injection with ASP.NET MVC3 – with custom IDependencyResolver


One of the new features of ASP.NET MVC3 is better support for dependency
injection. There are changes in how the framework interacts with your Inversion
of Control (IoC) Container.

As we all know the whole point of DI is about separating component behavior
from dependency resolution without object intervention. DI is a particular
implementation of Inversion of Control, where the consumer object receives his
dependencies inside constructor properties or arguments. DI requires a framework
component behind to deal with class constructor.

The web is full of blog posts on Dependency Injection and IoC. However – this
post is aimed at providing a very basic example of a custom DependencyResolver
with a Property Injection to HomeController.

I have setup the HomeController with a property WelcomeMessage.

  1. public class HomeController : Controller
  2. {
  3.     public string WelcomeMessage { get; set; }
  4.  
  5.     public ActionResult Index()
  6.     {
  7.         ViewBag.Message = WelcomeMessage;
  8.  
  9.         return View();
  10.     }
  11.  
  12.     public ActionResult About()
  13.     {
  14.  
  15.         return View();
  16.     }
  17.  
  18. }

In the Global.aspx, I have registered a SimpleDependencyResolver which essentially implements IDependencyResolver as depicted below. The point to note here is the default values I am giving as part of the GetService() method. This example is based on Maarten Balliauw's session on ASP.NET MVC3 and MEF @ MIX11

Register the DependencyResolver at Application_Start() as below:

  1.     protected void Application_Start()
  2.         {
  3.             DependencyResolver.SetResolver(new SimpleDependencyResolver());
  4.             AreaRegistration.RegisterAllAreas();
  5.             RegisterGlobalFilters(GlobalFilters.Filters);
  6.             RegisterRoutes(RouteTable.Routes);
  7.         }

  The Class Definition is as below:

  1. public class SimpleDependencyResolver:IDependencyResolver
  2.     {
  3.         #region IDependencyResolver Members
  4.  
  5.         public object GetService(Type serviceType)
  6.         {
  7.  
  8.             if (serviceType == typeof(HomeController))
  9.             {
  10.                 var hc = new HomeController();
  11.                 hc.WelcomeMessage = "Welcome 2 Dependency Injection Sample";
  12.                 return hc;
  13.             }
  14.  
  15.             if (serviceType.IsInterface)
  16.             {
  17.                 if (serviceType == typeof(IControllerFactory)) return new DefaultControllerFactory();
  18.                 if (serviceType == typeof(IControllerActivator)) return null;
  19.                 if (serviceType == typeof(IFilterProvider)) return GlobalFilters.Filters;
  20.                 if (serviceType == typeof(IViewEngine)) return new RazorViewEngine();
  21.                 if (serviceType == typeof(IViewPageActivator)) return null;
  22.             }
  23.             return Activator.CreateInstance(serviceType);
  24.         }
  25.  
  26.         public IEnumerable<object> GetServices(Type serviceType)
  27.         {
  28.             return new object[] {GetService(serviceType)};
  29.         }
  30.  
  31.         #endregion
  32.     }

Now run the code and see the DI in action in its simplest form.

Comments (2)

  1. Me says:

    Interesting subject but stop reading it when saw how unreadable code sections are. Use some code formatting tool/extension

  2. sajoshi says:

    Thanks for the comment. I have modified the code sections with QuickHighlighter which makes it more readable now. You own a real thanks since I got introduced to a new online tool which I can use for my blogs posts going forward.

Skip to main content