title: Am i using Unity and UnitOfWork in right way?
body: <p>Am I correct doing this? Can there be problems with memory usage or performance?</p>
  
  <p>UPD:Bug with disposed context is fixed.</p>
  
  <p>I'm using Unity with the Unit of Work pattern. When I run my application for the first time, everything works as expected, but once I refresh the page, the app crashes with the following exception:</p>
  
  <blockquote>
    <blockquote>
      <p>The operation cannot be completed because the DbContext has been disposed.</p>
    </blockquote>
  </blockquote>
  
  <p>My DI configuration looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private static IUnityContainer BuildUnityContainer()
  {
      var container = new UnityContainer();
  
      container.RegisterType&lt;DataContext&gt;();
  
      container.RegisterType&lt;IUnitOfWorkFactory, UnitOfWorkFactory&gt;();
  
      container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();
  
      container.RegisterType&lt;IProductRepository, ProductRepository&gt;();
  
      return container;
  }
  </code></pre>
  
  <p>My <code>UnitOfWork</code> and <code>UnitOfWorkFactory</code> look like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class UnitOfWork : IUnitOfWork
  {
      private readonly IUnityContainer _container;
      private bool _disposed;
  
      public UnitOfWork(IUnityContainer container)
      {
          _container = container;
          Context = _container.Resolve&lt;DataContext&gt;();
      }
  
      internal DataContext Context { get; set; }
  
      public TRepository GetRepository&lt;TRepository&gt;() 
          where TRepository : class
      {
          return _container.Resolve&lt;TRepository&gt;();
      }
  
      public void Commit()
      {
          Context.SaveChanges();
      }
  
      public void Dispose()
      {
          if (!_disposed)
          {
              _disposed = true;
              Context.Dispose();
          }
      }
  }
  
  public class UnitOfWorkFactory : IUnitOfWorkFactory
  {
      private readonly IUnityContainer _container;
  
      public UnitOfWorkFactory(IUnityContainer container)
      {
          _container = container;
      }
  
      public IUnitOfWork CreateUnitOfWork()
      {
          return _container.Resolve&lt;UnitOfWork&gt;();
      }
  }
  </code></pre>
  
  <p>The <code>UnitOfWork</code> contains a <code>GetRepository</code> method that allows returning any repository. Here is an example of such a repository:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ProductRepository : BaseRepository&lt;Product&gt;,
      IProductRepository
  {
      public ProductRepository(UnitOfWork unitOfWork)
          :base(unitOfWork)
      {
      }
  
      public Product Create(Product entity)
      {
          return DataBase.Add(entity);
      }
  
      public Product Remove(Product entity)
      {
          return DataBase.Remove(entity);
      }
  
      public Product Get(Int32 entityId)
      {
          return DataBase.FirstOrDefault(x =&gt; x.Id == entityId);
      }
  }
  
  public class BaseRepository&lt;TEntity&gt; 
      where TEntity : class
  {
      internal BaseRepository(UnitOfWork unitOfWork)
      {
          UnitOfWork = unitOfWork;
  
      }
  
      internal UnitOfWork UnitOfWork { get; private set; }
  
      internal virtual IDbSet&lt;TEntity&gt; DataBase
      {
          get { return UnitOfWork.Context.Set&lt;TEntity&gt;(); }
      }
  }
  </code></pre>
  
  <p>My MVC controllers depend on <code>IUnitOfWorkFactory</code>. Here is the <code>AccountController</code> for example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class AccountController : Controller
  {
      private readonly IUnitOfWorkFactory _unitOfWorkFactory;
  
      public AccountController(IUnitOfWorkFactory unitOfWorkFactory)
      {
          _unitOfWorkFactory = unitOfWorkFactory;
      }
  
      [AllowAnonymous]
      public ActionResult Login(string returnUrl)
      {
          ViewBag.ReturnUrl = returnUrl;
  
          using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
          {
              var rep = uow.GetRepository&lt;IProductRepository&gt;();
  
              var prod = rep.GetById(1);
          }
  
          return View();
      }
  }
  </code></pre>