title: Entity Framework: Singletonish ObjectContext - Good, Bad, or Overthinking?
body: <p>The idea is to create a class that exposes a context but handles the storage of it in a web application.</p>
  
  <p>Currently this is what I have:</p>
  
  <pre><code>public class EntityContext
  {
  
      private static String MAIN_CONTEXT_KEY = "MainContext";
      private static TISQLEntities _context;
  
      public static void RemoveContext()
      {
          if (
              HttpContext.Current != null 
              &amp;&amp; 
              HttpContext.Current.Items[MAIN_CONTEXT_KEY] != null
             )
          {
              ((TISQLEntities)HttpContext.Current.Items[MAIN_CONTEXT_KEY]).Dispose();
              HttpContext.Current.Items[MAIN_CONTEXT_KEY] = null;
          }
  
          if (_context != null)
          {
              _context.Dispose();
              _context = null;
          }
      }
  
      public static TISQLEntities Context
      {
          get
          {
              if (HttpContext.Current == null)
              {
                  if (_context == null)
                  {
                      _context = new TISQLEntities();
                  }
  
                  return _context;
              }
  
              if (HttpContext.Current.Items[MAIN_CONTEXT_KEY] == null)
              {
                  HttpContext.Current.Items[MAIN_CONTEXT_KEY] = new TISQLEntities();
              }
  
              return (TISQLEntities)HttpContext.Current.Items[MAIN_CONTEXT_KEY];
          }
      }
  }
  </code></pre>
  
  <p>And then in the Global.asax file:</p>
  
  <pre><code>protected void Application_EndRequest(object sender, EventArgs e)
  {
      EntityContext.RemoveContext();
  }
  </code></pre>
  
  <p>The idea is that if this is being run with a web application, the context is created on first need (and saved to the current HttpContext) and torn down whenever the request is over.</p>
  
  <p>If this is a UnitTest situation it is against created on first need and removed in the TestCleanup (Not as important in this post but just wanted to clarify the _context object).</p>
  
  <p>Now the idea behind this is in the least to not have to do this:</p>
  
  <pre><code>using(TISQLEntities context = new TISQLEntities())
  {
    ....
  }
  </code></pre>
  
  <p>Everytime I want to query.  I realize this may be me being lazy, but I just think that it's easier and cleaner to have:</p>
  
  <pre><code>EntityContext.Context.User.Select(...)
  </code></pre>
  
  <p>And avoids "using" which I try to avoid for most cases.  On top of that, I'm not creating 9001 contexts per postback.</p>
  
  <p>Now what I am curious about is that am I over thinking this?  Should I just keep creating a context for every method that needs one?  Say on a post back I have to:</p>
  
  <ul>
  <li>Get the user from an ID</li>
  <li>Get a site from an id</li>
  <li>Add the Site to the User (user.Site = foundSite)</li>
  <li>Save the user</li>
  </ul>
  
  <p>That could entail at least 3 contexts.  Is entity framework smart enough that it's ok to just keep creating contexts whenever?</p>