title: Creating a simple unit of work with Entity Framework and no repository
body: <p>I've been trying to implement a .net MVC Unit of Work API (rather than creating a separate repository), but it doesn't feel right. Am I going about this the correct way?</p>
  
  <p>BaseController</p>
  
  <pre><code>public class BaseController : ApiController
      {
          protected DBEntities _dbEntities;
  
          protected override void OnActionExecuting(ActionExecutingContext filterContext)
          {
              _dbEntities = new DBEntities();
          }
  
          protected override void OnActionExecuted(ActionExecutedContext filterContext)
          {
              _dbEntities.SaveChanges();
          }
      }
  </code></pre>
  
  <p>MyController</p>
  
  <pre><code>public class MyController : BaseController
  {
      public HttpResponseMessage PutMyObject(int id, int id2)
      {
          if (id != 0)
          {
              var myObject = _dbEntities.MyObjects.Where(x =&gt; x.id == id);
              if (myObject.Count() &gt; 0)
              {
                  MyObject temp = myObject.SingleOrDefault();
                  temp.Processed = true;
                  return Request.CreateResponse(HttpStatusCode.OK);
              }
              else
              {
                  return Request.CreateErrorResponse(HttpStatusCode.NotFound);
              }
          }
          else
          {
              /* do some other stuff */
          }
      }             
  
  }
  </code></pre>
  
  <p>My thinking around this is that the controller action is a single Unit of Work. The database connection is opened when the controller action starts, and SaveChanges is called once the response is returned.</p>
  
  <p>Am I going about this in the correct manner? do i need to dispose of _dbentities after savechanges is called within the BaseController? (or is this a better question for code review exchange)</p>