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.
- public class HomeController : Controller
- {
- public string WelcomeMessage { get; set; }
- public ActionResult Index()
- {
- ViewBag.Message = WelcomeMessage;
- return View();
- }
- public ActionResult About()
- {
- return View();
- }
- }
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:
- protected void Application_Start()
- {
- DependencyResolver.SetResolver(new SimpleDependencyResolver());
- AreaRegistration.RegisterAllAreas();
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
- }
The Class Definition is as below:
- public class SimpleDependencyResolver:IDependencyResolver
- {
- #region IDependencyResolver Members
- public object GetService(Type serviceType)
- {
- if (serviceType == typeof(HomeController))
- {
- var hc = new HomeController();
- hc.WelcomeMessage = "Welcome 2 Dependency Injection Sample";
- return hc;
- }
- if (serviceType.IsInterface)
- {
- if (serviceType == typeof(IControllerFactory)) return new DefaultControllerFactory();
- if (serviceType == typeof(IControllerActivator)) return null;
- if (serviceType == typeof(IFilterProvider)) return GlobalFilters.Filters;
- if (serviceType == typeof(IViewEngine)) return new RazorViewEngine();
- if (serviceType == typeof(IViewPageActivator)) return null;
- }
- return Activator.CreateInstance(serviceType);
- }
- public IEnumerable<object> GetServices(Type serviceType)
- {
- return new object[] {GetService(serviceType)};
- }
- #endregion
- }
Now run the code and see the DI in action in its simplest form.
Comments
Anonymous
September 11, 2011
Interesting subject but stop reading it when saw how unreadable code sections are. Use some code formatting tool/extensionAnonymous
September 12, 2011
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.