title: Simplest wat to integrate Unity with MVC3
body: <p>I have a console app with config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt;
      &lt;configSections&gt;
          &lt;section name="unity" type=
             "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
               Microsoft.Practices.Unity.Configuration" /&gt;
      &lt;/configSections&gt;
      &lt;unity xmlns="http://schemas.microsoft.com/practices/2010/unity"&gt;
          &lt;assembly name="UnityDi.Contracts" /&gt;
          &lt;assembly name="UnityDi.Domain" /&gt;
          &lt;assembly name="UnityDi.Services" /&gt;
          &lt;assembly name="UnityDi.Repositories" /&gt;
          &lt;namespace name="UnityDi.Contracts" /&gt;
          &lt;namespace name="UnityDi.Domain" /&gt;
          &lt;namespace name="UnityDi.Services" /&gt;
          &lt;namespace name="UnityDi.Repositories" /&gt;
          &lt;container&gt;
              &lt;register type="IUser" mapTo="User"&gt;&lt;/register&gt;
              &lt;register type="IUserService" mapTo="UserService"&gt;&lt;/register&gt;
              &lt;register type="IUserRepository" mapTo="UserRepository"&gt;&lt;/register&gt;
          &lt;/container&gt;
      &lt;/unity&gt; 
  &lt;/configuration&gt;
  </code></pre>
  
  <p>and Program.cs</p>
  
  <pre><code>namespace UnityDi.Console
  {
      using System;
      using Contracts;
      using Microsoft.Practices.Unity;
      using Microsoft.Practices.Unity.Configuration;
  
      public static class Program
      {
          private static readonly IUnityContainer Container = 
             new UnityContainer().LoadConfiguration();
  
          public static void Main()
          {
              var dummyUserOne = GetUserReference();
  
              dummyUserOne.UserId = 1;
              dummyUserOne.FullName = "Bilbo Baggins";
  
              var dummyUserTwo = GetUserReference();
  
              dummyUserTwo.UserId = 2;
              dummyUserTwo.FullName = "Frodo Baggins";
  
              var userService = GetUserServiceReference(); 
  
              userService.Add(dummyUserOne);
              userService.Add(dummyUserTwo);
  
              var users = userService.GetAllUsers();
  
              foreach (var user in users)
              {
                  Console.WriteLine(user.FullName);
              }
  
              Console.ReadLine();
          }
  
          private static IUser GetUserReference()
          {
              return Container.Resolve&lt;IUser&gt;();
          }
  
          private static IUserService GetUserServiceReference()
          {
              return Container.Resolve&lt;IUserService&gt;();
          }
      }
  }
  </code></pre>
  
  <p>That works really well.</p>
  
  <p>I've been doing a lot of searching around and find that solutions published integrating Unity with MVC3 seem to be very verbose in their approach.</p>
  
  <p>Within the context of the code above (and with a requirement to wire up Entity Framework code first), what's the SIMPLEST way for me to integrate Unity with MVC3 and work with my data like above?</p>
  
  <p>I want to use the exact same approach to creating my objects in MVC as above.</p>
  
  <p>Thanks!</p>
  
  <p>Richard</p>
  
  <p>P.s. The closest I found was <a href="http://unitymvc3.codeplex.com/" rel="nofollow">Unity.Mvc</a> but I can't get it to work like above. I'm probably being stupid though. This is new to me.</p>
  
  <p><strong>EDIT: Working with the proposed answer.</strong></p>
  
  <p>In Web.config for the MVC app (with Unity.Mvc package added)</p>
  
  <pre><code>[snip]
  &lt;container&gt;
      &lt;register type="IUser" mapTo="User"&gt;&lt;/register&gt;
      &lt;register type="IUserService" mapTo="UserService"&gt;&lt;/register&gt;
      &lt;register type="IUserRepository" mapTo="UserRepository"&gt;
          &lt;lifetime type="HierarchicalLifetimeManager" /&gt;
      &lt;/register&gt;
  &lt;/container&gt;
  </code></pre>
  
  <p>Then in Bootstrapper.cs</p>
  
  <pre><code>namespace Unity.Mvc.Resources
  {
      using System.Web.Mvc;
      using Microsoft.Practices.Unity;
      using Microsoft.Practices.Unity.Configuration;
      using Unity.Mvc3;
  
      public static class Bootstrapper
      {
          public static void Initialise()
          {
              var container = BuildUnityContainer();
  
              DependencyResolver.SetResolver(new UnityDependencyResolver(container));
          }
  
          private static IUnityContainer BuildUnityContainer()
          {
              var container = new UnityContainer().LoadConfiguration();
  
              container.RegisterControllers();
  
              return container;
          }
      }
  }
  </code></pre>
  
  <p>Then in the controller (bit of an a-ha moment when I realised the Interface can be any Unity knows about, including it's own):</p>
  
  <pre><code>private readonly IUnityContainer container;
  
  public HomeController(IUnityContainer container)
  {
      this.container = container;
  }
  
  public ActionResult Index()
  {
      var dummyUserOne = GetUserReference();
  
      dummyUserOne.UserId = 1;
      dummyUserOne.FullName = "Bilbo Baggins";
  
      var dummyUserTwo = GetUserReference();
  
      dummyUserTwo.UserId = 2;
      dummyUserTwo.FullName = "Frodo Baggins";
  
      var userService = GetUserServiceReference();
  
      userService.Add(dummyUserOne);
      userService.Add(dummyUserTwo);
  
      var users = userService.GetAllUsers();
  
      return View(users);
  }
  
  private IUser GetUserReference()
  {
      return container.Resolve&lt;IUser&gt;();
  }
  
  private IUserService GetUserServiceReference()
  {
      return container.Resolve&lt;IUserService&gt;();
  }
  </code></pre>
  
  <p>And again, it works :)</p>
  
  <p>That just leaves the Entity Framework aspect. Need to get DbContext wired up to work with my Domain objects without introducing additional dependencies... Any pointers? Really, very much appreciated.</p>