ASP.NET MVC Framework and Unity Dependency Injection Container Part II
by David Hayden, Microsoft MVP C#
Update 3/7/2008: Added Unity IoC and ASP.NET MVC Framework - Dependency Injection of Controllers
Update 2/19/2008: Added Unity IoC Dependency Injection and ASP.NET Model-View-Presenter
Update 2/22/2008: Added Unity Dependency Injection IoC Screencast
I received a few emails on showing more detail on how I am using the ASP.NET MVC Framework with the Unity Dependency Injection Container. I am still deciding the best way to integrate the various IoC Containers with the ASP.NET MVC Framework, but this time I used the same approach as the Windsor Example in MVCContrib.
The HttpApplication Class is responsible for holding the Unity Container and providing access to it using an IContainerAccessor Interface:
public class Global : HttpApplication, IContainerAccessor
{
private static UnityContainer _container;
public static IUnityContainer Container
{
get { return _container; }
}
IUnityContainer IContainerAccessor.Container
{
get { return Container; }
}
protected void Application_Start(object sender, EventArgs e)
{
InitializeContainer();
ControllerBuilder.Current.
SetDefaultControllerFactory(
typeof(CustomControllerFactory));
}
private void InitializeContainer()
{
if (_container == null)
_container = new UnityContainer();
// For Sample Only...
_container.RegisterType<INewsService, NewsService>();
}
}
As mentioned in the previous post, Unity Dependency Injection Container and ASP.NET MVC Framework, I am not adding any Controller Classes to the Unity Container. You don't need to with Unity. It will create classes even if they are not in the container.
I am specifying my custom IControllerFactory, CustomControllerFactory, and I am registering the NewsService that will be injected into my HomeController.
As mentioned in the previous post, my CustomControllerFactory is as follows:
public class CustomControllerFactory : IControllerFactory
{
public IController CreateController
(RequestContext context, Type controllerType)
{
IContainerAccessor containerAccessor =
context.HttpContext.ApplicationInstance
as IContainerAccessor;
return containerAccessor.Container.Resolve(controllerType)
as IController;
}
}
It may look a little confusing looking at it like this, because I am accessing the UnityContainer through a IContainerAccessor Interface and the Container Property on that interface. All I am doing is using the following method on the UnityContainer API:
UnityContainer.Resolve(Type t)
that will create the HomeController Class and add any dependencies to it.
Using the Property Setter Injection Mechanism mentioned in the previous post, since the HomeController has a [Dependency] Attribute on the NewsService Property:
public class HomeController : Controller
{
[Dependency]
public INewsService NewsService { get; set; }
[ControllerAction]
public void Index()
{
List<News> currentNews = NewsService.GetNews();
RenderView("Index", currentNews);
}
}
Unity will inject the NewsService we registered before into the HomeController. As mentioned above, we registered the NewsService in the InitializeContainer Method just for this sample -
private void InitializeContainer()
{
if (_container == null)
_container = new UnityContainer();
// For Sample Only...
_container.RegisterType<INewsService, NewsService>();
}
Note that if you wanted to register the NewsService as a Singleton, you could register it as follows:
_container
.RegisterType<INewsService, NewsService>
(new ContainerControlledLifetimeManager());
I hope that clarifies how I used Unity with the ASP.NET MVC Framework. This is just one way that I modeled after the Windsor Example in the MVCContrib.
Recent ASP.NET MVC Tutorials:
Author: David Hayden, Microsoft MVP C#
Site: http://www.davidhayden.com/