title: Testing a class library that is using different databases based on Session
body: <p>I have an ASP.NET website project that until recent had all code in App_Code folder. It uses Entity Framework 4 as ORM. Application is divided into three "sections" (let's say one for each customer). Each section has it's own database (but same schema). This is due to performance reasons, databases are over 10GB each with millions of rows.</p>
  
  <p>Each time a <code>context</code> object is created a Session variable which holds section ID is called and proprietary connection string is chosen for this context.</p>
  
  <p>It looks like this (following are members of static <code>Connection</code> class):</p>
  
  <pre><code>public static MyEntities GetEntityContext()
  {
      if (HttpContext.Current.Session["section"] == null)
      {
          HttpContext.Current.Response.Redirect("~/Login.aspx");
      }
      var context = new MyEntities(GetEntityConnectionStringForSection((int)HttpContext.Current.Session["section"]);
      return context;
  }
  
  private static string GetEntityConnectionStringForSection(int section)
  {
      switch (section)
      {
          case 1: return ConfigurationManager.ConnectionStrings["entity_1"].ConnectionString;
          case 2: return ConfigurationManager.ConnectionStrings["entity_2"].ConnectionString;
          case 3: return ConfigurationManager.ConnectionStrings["entity_3"].ConnectionString;
          default: return ConfigurationManager.ConnectionStrings["entity_1"].ConnectionString;
      }
  }
  </code></pre>
  
  <p>It works very good and also handles situation when session timed out everytime any data access is performed. </p>
  
  <p>Recently as I needed to share DB classes among two websites I moved all DB classes to separate class library and referenced <code>System.Web</code> library which I know is bad practice, but it's working.</p>
  
  <p>Now the next step is to include unit and module tests which as I read is very difficult or impossible when using <code>HttpContext</code> in library, so I want to get rid of <code>System.Web</code> references. What is the best practice for this situation?</p>
  
  <p>I think I can't just pass <code>HttpContext</code> to <code>GetEntityContext()</code> as it is also called from within my entity classes. Although this probably can be refactored. So maybe this is where I should go?</p>
  
  <p>I also wondered if is it possible to somehow pass current section ID to this whole library? It cannot be just static property because as far as I understand it would be common for all users using the application. This should be user-specific.</p>
  
  <p>Reassuming the objective is to make automated testing possible without loosing transparent Connection String choosing and session timeouts handling.</p>
  
  <p>If I do something fundamentally wrong at this stage please also let me know. I can look again at this question tomorrow morning (8.00 am UTC) so please don't be discouraged by my silence till then.</p>
  
  <p>EDIT:</p>
  
  <p>Example of usage of <code>Connection</code> class in the library:</p>
  
  <pre><code>public partial class Store
  {
      public static List&lt;Store&gt; GetSpecialStores()
      {
          using (var context = Connection.GetEntityContext())
          {
            return context.Stores.Where(qq =&gt; qq.Type &gt; 0).OrderBy(qq =&gt; qq.Code).ToList();
          }
      }
  }
  </code></pre>