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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:46:45+00:00 2026-05-27T18:46:45+00:00

Following the directions from many articles, I’ve decided to implement the Unit of work

  • 0

Following the directions from many articles, I’ve decided to implement the Unit of work pattern to my Linq2SQL DataContexts in my ASP.Net WebForms Application, but I’m not sure if I’m on the right way.

Here’s what I’m accomplishing so far:

1 – On every Request, I catch the Application_AcquireRequestState event (which has access to Session data) in Global.asax and instantiate a new DataContext to bind it to the user’s Session:

void Application_AcquireRequestState(object sender, EventArgs e)
{
    // Check if the request is for a Page, Page Method or Handler
    if (new Regex(@"\.(aspx|ashx)(/.*)?$").IsMatch(HttpContext.Current.Request.Url.AbsolutePath))
    {
        MyCompany.MyDatabaseDataContext myDatabaseDataContext = new MyCompany.MyDatabaseDataContext();

        HttpContext.Current.Session["myDatabaseDataContext"] = myDatabaseDataContext;
    }
}

2 – Every Data Access Layer Object (DAO) inherits from a base DAO: GenericDAO:

public class GenericDAO
{
    private MyDatabaseDataContext _dbMyDatabase;

    protected MyDatabaseDataContext dbMyDatabase
    {
        get
        {
            if (_dbMyDatabase == null)
                _dbMyDatabase = HttpContext.Current.Session["myDatabaseDataContext"] as MyDatabaseDataContext;

            return _dbMyDatabase;
        }
    }
}

3 – So, in every operation, the DAO use the DataContext Property from its parent class:

public class MyTableDAO : GenericDAO
{
    public List<MyTable> getAll()
    {
        return dbMyDatabase.GetTable<MyTable>().ToList();
    }
}

Here’s my concerns…

  1. First of all, is it ok to store the DataContext in the user’s Session? What would be another option? My app has a lot of PageMethods calls, so I’m worried the DTX would be invalidated between their async requests if it is stored in the session.
  2. Do I need to capture the Application_ReleaseRequestState event to Dispose() of the DataContext and remove it from the session?
  3. If I don’t need to Dispose of it, in every Application_AcquireRequestState, would it be better to Remove DTX from Session - Create DTX - Store it or just Refresh it?
  4. Also, if I don’t need to Dispose of it, what about Connections? Would it handle them automatically or I would need to control them too?

I appreciate your time and help 🙂

— EDIT

Here’s the code I’ve reached, following @ivowiblo’s suggestion:

Global.asax

void Application_BeginRequest(object sender, EventArgs e)
{
    if (new Regex(@"\.(aspx|ashx)(/.*)?$").IsMatch(HttpContext.Current.Request.Url.AbsolutePath))
    {
        MyCompany.MyDatabaseDataContext myDatabaseDataContext = new MyCompany.MyDatabaseDataContext();

        HttpContext.Current.Items["myDatabaseDataContext"] = ceabsDataContext;
    }
}

void Application_EndRequest(object sender, EventArgs e)
{
    if (new Regex(@"\.(aspx|ashx)(/.*)?$").IsMatch(HttpContext.Current.Request.Url.AbsolutePath))
    {
        if (HttpContext.Current.Items["myDatabaseDataContext"] != null)
        {
            System.Data.Linq.DataContext myDbDtx = HttpContext.Current.Items["myDatabaseDataContext"] as System.Data.Linq.DataContext;

            if (myDbDtx != null)
                myDbDtx.Dispose();
        }
    }
}

GenericDAO

public class GenericDAO
{
    protected MyDatabaseDataContext dbMyDatabase
    {
        get
        {
            return HttpContext.Current.Items["myDatabaseDataContext"] as MyDatabaseDataContext;
        }
    }
}

Simple as that!

  • 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-05-27T18:46:46+00:00Added an answer on May 27, 2026 at 6:46 pm

    The best approach is to put it on HttpContext.Current.Items, creating the DataContext on RequestBegin and dispose it in RequestEnd. In msdn there’s an interesting article about the better management of the DataContext, where it’s suggested to have short-time DataContext instances.

    This pattern is called Open session in view and was created for using NHibernate in web environments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a custom print template closely following the directions in Chuck Ainslie's articles
I'm trying to run the following line: Directions.loadFromWaypoints((Waypoint[])waypoints.toArray(), opts); But I'm getting: 23:41:44.595 [ERROR]
I am trying to extract the content of a date element from many ill-formed
What i want to do is the following procedure: Get a frame from the
Trying to change two relationships in our entity framework from many-to-one to many-to-many relationships.
I am trying to return a varchar from stored procedure with the following code:
I've been trying many times to create an algorithm to extract stroke information from
I have the following C++ code which I got from google's sparsehash website: #include
I have integrated NHibernate.Search into my web app by following tutorials from the following
I have the following code to scroll images in vertical direction (uses jCarouselLite )

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.