Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:49:43+00:00 2026-06-07T23:49:43+00:00

I have an ASP.NET website project that until recent had all code in App_Code

  • 0

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.

Each time a context object is created a Session variable which holds section ID is called and proprietary connection string is chosen for this context.

It looks like this (following are members of static Connection class):

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;
    }
}

It works very good and also handles situation when session timed out everytime any data access is performed.

Recently as I needed to share DB classes among two websites I moved all DB classes to separate class library and referenced System.Web library which I know is bad practice, but it’s working.

Now the next step is to include unit and module tests which as I read is very difficult or impossible when using HttpContext in library, so I want to get rid of System.Web references. What is the best practice for this situation?

I think I can’t just pass HttpContext to GetEntityContext() as it is also called from within my entity classes. Although this probably can be refactored. So maybe this is where I should go?

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.

Reassuming the objective is to make automated testing possible without loosing transparent Connection String choosing and session timeouts handling.

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.

EDIT:

Example of usage of Connection class in the library:

public partial class Store
{
    public static List<Store> GetSpecialStores()
    {
        using (var context = Connection.GetEntityContext())
        {
          return context.Stores.Where(qq => qq.Type > 0).OrderBy(qq => qq.Code).ToList();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:49:45+00:00Added an answer on June 7, 2026 at 11:49 pm

    You can declare interface IContextProvider inside your library ans use it to retrieve context. Something like:

    public interface IContextProvider
    {
       MyEntities GetEntityContext();
    }
    

    This will make your library testable. In your web project you can inject IContextProvider implementation into your library.

    public class WebContextProvider : IContextProvider
    {
        public MyEntities GetEntityContext()
        {
            if (HttpContext.Current.Session["section"] == null)        
                HttpContext.Current.Response.Redirect("~/Login.aspx");
    
            int sectionId = (int)HttpContext.Current.Session["section"];
            string connectionString = GetEntityConnectionStringForSection(sectionId);
            var context = new MyEntities(connectionString);
            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;
           }
       }
    }
    

    Inject this interface to repositories or other data access classes.

    public partial class Store
    {
        private IContextProvider contextProvider;
    
        public Store(IContextProvider contextProvider)
        {
            this.contextProvider = contextProvider;
        }
    
        public List<Store> GetSpecialStores()
        {
            using (var context = contextProvider.GetEntityContext())
            {
              return context.Stores.Where(qq => qq.Type > 0).OrderBy(qq => qq.Code).ToList();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy ASP.NET/VB.NET WebSite. There several App_Code modules/code files that use types
We have a ASP.NET website project. In the past, we had been using asmx
I have created an asp.net website, with the accompanying app_code folder. In the same
I have the following: ASP.NET website using the 3.5 framework One folder that contains
Hello I have a page in an ASP.NET (website) project that writes a file
In a dynamically compiled ASP.NET Website project, can the assembly for the App_Code folder
I have a ASP.NET website that references another project with contains as resx file.
We have an ASP.NET website that we use internally to do some project tracking
I my asp.net website project with c# code, I have a table in which
I have a website project (C#/ASP.NET) opened in Visual Studio 11 (beta) which works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.