*****************************************************************************************

Future development of Unity.Mvc has moved to GitHub:

https://github.com/feedbackhound/Unity.Mvc5

The project also has a new owner. You can read about the announcement on the FeedbackHound Blog.

If you are using MVC5 or greater, we recommend that you move to the updated library on GitHub (also available on Nuget as Unity.Mvc5). For MVC3 and MVC4, you can continue to use this version.

*****************************************************************************************

What does Unity.MVC3 do?

Unity.Mvc3 is a small library that allows simple Integration of Microsoft's Unity IoC container with ASP.NET MVC 3.

Where do I get Unity.MVC3 binaries from?

You have three options. Firstly, you can simply download the Unity.Mvc3 dll from the Downloads tab and manually reference it in your project. Secondly, you can download the source code (which includes an example project) from the Source Code tab and build the binaries yourself. Finally, you can reference the NuGet package from within Visual Studio. Just right-click on references in your MVC3 project and select Add Library Package Reference. Then Search for Unity.Mvc3 and click install. NuGet will download the necessary files and automatically add a reference.

How do I use Unity.MVC3 (NuGet)

If you download the latest NuGet Unity.Mvc3 package, you will find a Bootstrapper.cs file at the root of your application which contains code to register the DependencyResolver and set up the Unity container. All you need to do to get it up and running is to make a call to the bootstrapper's Initialise method from the Application_Start method in the Global.asax and register your components in the BuildUnityContainer method of the bootstrapper. It is important to note that any types that you want to be disposed at the end of the request must be given a lifetime of HierarchicalLifetimeManager.

Global.asax:
        protected void Application_Start()
        {
            ...
            Bootstrapper.Initialise();
        }

Bootstrapper.cs
        private static IUnityContainer BuildUnityContainer()
        {
            ...
            container.RegisterType<IUpperCaseService, UpperCaseService>();
            container.RegisterType<ILowerCaseService, LowerCaseService>();
            container.RegisterType<IExampleContext, ExampleContext>(new HierarchicalLifetimeManager());
            ...
        }

In this example, we have two services that use the same ExampleContext. The services themselves do not implement IDisposable, but the ExampleContext does, so we register it using the HierarchicalLifetimeManager. On every request, one UpperCaseService, one LowerCaseService and one ExampleContext are instantiated by DependencyResolver via Unity. At the end of the request, the ExampleContext is automatically disposed. You can download the source for more information.


How do I use Unity.MVC3 (dll)

If you are not using the NuGet package, you will need to register the DependencyResolver and set up the Unity container yourself. Developers generally do this in the Global.asax Application_Start method. Set up your Unity container as you would normally do, but any types that you want to be disposed at the end of the request must be given a lifetime of HierarchicalLifetimeManager.

RegisterControllers is a simple extension method that registers all controllers within the calling assembly and is included in the package.

It is not necessary to register your controllers and the extension method has been removed in 1.2.

Global.asax:
protected void Application_Start()
{
    ...
    var container = new UnityContainer();
    container.RegisterType<IUpperCaseService, UpperCaseService>();
    container.RegisterType<ILowerCaseService, LowerCaseService>();
    container.RegisterType<IExampleContext, ExampleContext>(
        new HierarchicalLifetimeManager());    
     
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

In this example, we have two services that use the same ExampleContext. The services themselves do not implement IDisposable, but the ExampleContext does, so we register it using the HierarchicalLifetimeManager. On every request, one UpperCaseService, one LowerCaseService and one ExampleContext are instantiated by DependencyResolver via Unity. At the end of the request, the ExampleContext is automatically disposed. You can download the source for more information.

Where can I find more information about Unity.MVC3

You can find further information about this project at the DevTrends Blog.

A complete guide to integrating Unity.Mvc3 1.1 into an MVC3 application including various DI scenarios
http://devtrends.co.uk/blog/integrating-the-unity.mvc3-1.1-nuget-package-from-scratch

The original 1.0 release post explaining more about how Unity.Mvc3 works.
http://devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable

Last edited Jan 25 at 4:02 PM by DevTrends, version 15