An updated code sample of using the Unity IoC Container with the ASP.NET MVC Framework based on the API Changes to Unity that occurred on the March 4th, 2008 Weekly Drop. Unity can be used in a CustomControllerFactory to create your MVC Controllers and inject dependencies. The sample code is modeled after the Castle Windsor Example in MVCContrib.
ASP.NET Model-View-Presenter and Unity IoC
You can also learn about using Unity IoC with ASP.NET Model-View-Presenter at:
IUnityContainer Accessed From HttpApplication Class
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)
{
Initialize();
}
private static void Initialize()
{
if (_container == null)
_container = new UnityContainer();
IControllerFactory controllerFactory =
new UnityControllerFactory(_container);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
// For Sample Only... Create a Singleton
_container.RegisterType<INewsService, NewsService>
(new ContainerControlledLifetimeManager());
}
}
The interface, IContainerAccessor, is just a custom interface I created to provide access to the IUnityContainer on the HttpApplication Class. This is completely optional, but sometimes you may need to get at the IUnityContainer from within the web application and it is nice to have it on the HttpApplication Class for later reference.
public interface IContainerAccessor
{
IUnityContainer Container { get; }
}
IControllerFactory - Custom Controller Factory for ASP.NET MVC Controllers
I am specifying my custom IControllerFactory, UnityControllerFactory, that will use Unity to create the Controller Classes and inject their dependencies.
public class UnityControllerFactory : DefaultControllerFactory
{
IUnityContainer _container;
public UnityControllerFactory(IUnityContainer container)
{
_container = container;
}
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null)
throw new ArgumentNullException("controllerType");
if (!typeof(IController).IsAssignableFrom(controllerType))
throw new ArgumentException(string.Format(
"Type requested is not a controller: {0}", controllerType.Name),
"controllerType");
return _container.Resolve(controllerType) as IController;
}
}
Inject Dependencies into Controller
Either use Constructor Injection or Property Injection to inject the INewsService into the HomeController to display news. In this case, I am specifying Property Injection by specifying the DependencyAttribute on the NewsService Property:
public class HomeController : Controller
{
[Dependency]
public INewsService NewsService { get; set; }
public void Index()
{
List<News> currentNews = NewsService.GetNews();
RenderView("Index", currentNews);
}
}
Conclusion
The Unity IoC Dependency Injection Tool from Microsoft Patterns & Practices can be used with the ASP.NET MVC Framework to create Controller Classes and inject their dependencies.