title: Repository as static or non-static class?
body: <p>Recently I had a discussion with a colleague on whether a repository in a web application (in this case a layer on top of Entity Framework) should be implemented as static or non-static classes.</p>
  
  <p>In this question I'm not interested if one or the other implementation is a better (OOP) design, because that would make the answers subjective.</p>
  
  <p>What I am interested in: Is instantiating and disposing the Entity Framework <code>DbContext</code> handled in a different way in the CLR when you compare the static with the non-static class? I am specifically interested in multi-threading issues and memory usage, because this code runs in an MVC web application.</p>
  
  <p><strong>Edit:</strong> Just to clarify: I'm not interested in the disposal or garbage collection of the repository class, just what happens with the local <code>context</code> variable. Is it correct to say that it is carbage collected (or at least marked to be garbage collected) when the method returns?</p>
  
  <p><strong>Static repository</strong></p>
  
  <pre><code>public static class AccountRepository
  {
      public static AccountModel GetAccountById(int accountId)
      {
          using (var context = new EntitiesContext())
          {
              var account = context.Accounts.FirstOrDefault(a =&gt; a.Id == accountId);
              if (account == null)
              {
                  return null;
              }
  
              return new AccountModel
              {
                  Id = account.Id,
                  Username = account.Username,
                  // etc...
              };
          }
      }
  }
  </code></pre>
  
  <p><strong>Non-static repository</strong></p>
  
  <pre><code>public class AccountRepository
  {
      public AccountModel GetAccountById(int accountId)
      {
          using (var context = new EntitiesContext())
          {
              var account = context.Accounts.FirstOrDefault(a =&gt; a.Id == accountId);
              if (account == null)
              {
                  return null;
              }
  
              return new AccountModel
              {
                  Id = account.Id,
                  Username = account.Username,
                  // etc...
              };
          }
      }
  }
  </code></pre>