title: ASP.NET MVC, Unity and IDisposable
body: <p>I am using ASP.Net MVC 4, EF and Unity for DI. Also uses UnitOfWork pattern. Trying to figure out the best way to implement this. I have the code like below.
  The problem I am having is the Dispose() in Business and Repository layer never get called now, only the Destructor in both get called, so the objects seem to be never disposed.
  Please answer the following</p>
  
  <ol>
  <li><p>Do I really need the IDisposable implementation in Business and Repository layer (if Unity is already taking care of it)</p></li>
  <li><p>What should I do to get the Dispose() called(should I add it to the Controller too and Dispose all other objects or use some specific LifeTime manager)</p></li>
  <li><p>Whether I should use the Singleton Instance of each or dispose it in each request as it is in the web environment.</p></li>
  </ol>
  
  <p>Global.asax.cs:</p>
  
  <pre><code>private static IUnityContainer _unityContainer;
  
  protected void Application_Start()
  {
     _unityContainer = UnityBootstrapper.SetupUnity();
     _unityContainer.RegisterType&lt;IController, ProductController&gt;("Product");  
     DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(_unityContainer));
  }  
  </code></pre>
  
  <p>UnityBootstrapper.cs:</p>
  
  <pre><code>public class UnityBootstrapper
  {
      public static IUnityContainer SetupUnity()
      {
          UnityContainer container = new UnityContainer();
          container.RegisterType&lt;IProductDbContext, ProductDbContext&gt;()
                   .RegisterType&lt;IUnitOfWork, UnitofWork&gt;(new InjectionConstructor(new ResolvedParameter(typeof(IProductDbContext))))
                   .RegisterType&lt;IProductRepository, ProductRepository&gt;()
                   .RegisterType&lt;IProductBusiness, ProductBusiness&gt;();
      }
  }
  </code></pre>
  
  <p>ProductController.cs:</p>
  
  <pre><code>public class ProductController : ControllerBase
  {
      private readonly IProductBusiness _productBusiness;        
  
      public ProductController(IProductBusiness productBusiness)
      {
          _productBusiness = productBusiness;
      }
  
      //No Dispose for this
  }
  </code></pre>
  
  <p>ProductBusiness.cs:</p>
  
  <pre><code>public class ProductBusiness : IProductBusiness, IDisposable
  {
      private readonly IUnitOfWork _unitOfWork;
      private readonly IProductRepository _productRepository;
      public ProductBusiness(IUnitOfWork unitOfWork)
      {
          _unitOfWork = unitOfWork;
          _productRepository = _unitOfWork.ProductRepository;
      }
  
      public override void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected override void Dispose(bool disposing)
      {
          if (!_isDisposed)
          {
              if (disposing)
              {
                  if (_productRepository != null) _productRepository.Dispose();
                  if (_unitOfWork != null) _unitOfWork.Dispose();
              }
  
              _isDisposed = true;
           }
      }
  
      ~ProductBusiness()
      {
           Dispose(false);
      }
  }
  </code></pre>
  
  <p>ProductRepository.cs:</p>
  
  <pre><code>public class ProductRepository : IProductRepository, IDisposable
  {
      private readonly IProductDbContext _context;
  
      public ProductRepository(IProductDbContext context)
      {
          if (context == null)
              throw new ArgumentNullException("context");
  
          _context = context;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (!_isDisposed)
          {
              if (disposing)
              {
                  if (_context != null) _context.Dispose();
              }
  
              _isDisposed = true;
          }
      }
  
      ~ProductRepository()
      {
          Dispose(false);
      }
  }   
  </code></pre>