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 8424485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:00:20+00:00 2026-06-10T04:00:20+00:00

Our Asp.net web application is using LINQ-to-SQL (Stored Procs are dragged on dropped on

  • 0

Our Asp.net web application is using LINQ-to-SQL (Stored Procs are dragged on dropped on dbml file to create classes) and 3 tier architecture is similar to the one below. I have just created rough methods to give reader proper idea so that he can answer well.

namespace MyDataLayer
{
    public class MyDataAccess
    {
        // global instance of datacontext
        MyDataModelDataContext myDB = new MyDataModelDataContext();     (#1)

        public void GetUserIDByUsername(string sUserName, ref int iUserID)
        {
            int? iUserIDout = 0;
            // this will make call to SP in SQL DB
            myDB.USP_RP_GETUSERIDBYUSERNAME(sUserName, "", ref iUserIDout);
            iUserID = (int)iUserIDout;
        }

        public List<USP_APP_USERDETAILSResult> GetUserDetails(string sUserIDs)
        {
            // this will make call to SP in SQL DB
            return myDB.USP_APP_USERDETAILS(sUserIDs).ToList();
        }
        ...
        ... // several CRUD methods
    }
}

namespace MyBusinessLayer
{
    public class SiteUser
    {
        // global DataAccess instance
        MyDataLayer.MyDataAccess myDA = new MyDataAccess();             (#2)
        public void GetUserIDByUsername(string sUserName, ref int iUserID)
        {
            myDA.GetUserIDByUsername(sUserName, ref iUserID);
        }
        public List<USP_APP_USERDETAILSResult> GetUserDetails(string sUserIDs)
        {
            // this will make call to SP in SQL DB
            return myDA.GetUserDetails(sUserIDs);
        }
        ...
        ... // several CRUD methods
    }
}

namespace MyWebApplication
{
    public class BaseWebPage : System.Web.UI.Page
    {
        // static business layer instance
        public static MyBusinessLayer.SiteUser UserBLInstance = new SiteUser();         (#3)                    
        ...             
    }
}
// Index.aspx.cs code fragment
namespace MyWebApplication
{
    public class Index : BaseWebPage
    {       
        public void PopulateUserDropDown()
        {
            // using static business layer instance declared in BaseWebPage
            List<USP_APP_USERDETAILSResult> listUsers = UserBLInstance.GetUserDetails("1,2,3");

            // do databinding and so on ...
        }
        ...             
    }
}

Questions

  • (Ref.#1) Is having global datacontext in DataAccess good approach? yes/no why?
  • If your suggestion is having datacontext per request what is the best practice for that
  • (Ref.#2) Is having global DataAccess instance in BusinessLayer good approach? yes/no why?
  • If your suggestion is having DataAccess instance per request what is the best practice for that
  • (Ref. #3) Is static business layer instance declared in BaseWebPage good approach? yes/no why?
  • Best approach to manage life time of BL instance and DL instance in general

We are facing periodic InvalidCastException on production server for a very simple method which works fine if I restart my application from IIS. When this problem is there we can access the same database from SQL Management Studio and can execute same SP

Our prime suspect about this issue is poor DataContext management and I have read many articles on net about managing life time of DataContext but I am now confused about various approach.
That’s why I have elaborated my questions so that many in same situation can get clear idea about problem/answer.

  • 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-10T04:00:22+00:00Added an answer on June 10, 2026 at 4:00 am

    (Ref.#1) Is having global datacontext in DataAccess good approach? yes/no why?

    Yes.

    However, creating it manually inside the dataaccess class means that you can’t control the lifetime of the datacontext. Instead, make it then a constructor parameter so that it is injected into the data access

    (Ref.#2) Is having global DataAccess instance in BusinessLayer good approach? yes/no why?

    Yes. But refer to 1. – make it injectable via the constructor.

    (Ref. #3) Is static business layer instance declared in BaseWebPage good approach? yes/no why?

    No. Avoid static for complex objects as usually such objects has non-trivial state. And this is when a lot of nasty issues can happen if you share such objects in a concurrent environment.

    To summarize.

    public class DataAccess {
       public DataAccess( DataContext context ) { ... }    
    }
    
    public class BusinessLayer {
       public BusinessLayer( DataAccess access ) { ... }    
    }
    
    public class MyPage : Page {
     ...
    
     var ctx = TheDataContext.Current;
     var bl = new BusinessLayer( new DataAccess( ctx ) );  
    }
    

    with data context shared in a request scope:

    public partial class TheDataContext {
    
      // Allow the datacontext to be shared in a request-scope
      public static TheDataContext Current {
         get {
             if ( HttpContext.Current.Items["context"] == null )
                HttpContext.Current.Items.Add( "context", new TheDataContext() );
    
             return (TheDataContext)HttpContext.Current.Items["context"];
         } 
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an ASP.NET application using WIF. Our web.config file has a section like
We are considering using Cassette in our asp.net web application which might be accessed
We are using Oracle 11g as backend in our asp.net Web application. In signup
I'm using watin testing tool for our asp.net mvc 3 web application. The same
We have a wonderful ASP.NET MVC 2 web application using MS SQL 2008 and
We have been using log4net for logging our asp.net web forms application. Our logging
We have an ASMX web service which we invoke from our ASP.NET application using
We have written Web application using ASP.NET MVC, which will be hosted in our
I have a location using our ASP.NET 4.0 web application which was running fine
We're using ELMAH for error logging in our ASP.NET application. We use the SQL

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.