DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Logging Best Practices Revisited [Video]
  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core

Trending

  • Logging Best Practices Revisited [Video]
  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Manage ObjectContext Per Request in ASP.NET

How to Manage ObjectContext Per Request in ASP.NET

Gil Fink user avatar by
Gil Fink
·
May. 19, 10 · News
Like (0)
Save
Tweet
Share
7.42K Views

Join the DZone community and get the full member experience.

Join For Free
We started a new project at work.  One of my guidelines was to manage the lifetime of the Entity Framework’s ObjectContext as context per request. Since I got some questions in the area I decided to explain how you can achieve that.

Why to Use an ObjectContext Per Request?

In a previous post I wrote about this subject. The main issue is that the context should be a short living object. Since this is the case but we want to gain all the advantages of using the context (which include lazy lading, change tracking and more), I prefer to use a context per request in web applications.

How to Manage ObjectContext Per Request in ASP.NET?

When we want to use an ObjectContext per request in web applications we need to find a centralized place which will hold the context during the request. For our rescue comes the HttpContext class. The HttpContext lives during the request and is dispose after it so its our perfect candidate. We can use the Items collection that it holds in order to keep the instance of our ObjectContext. One drawback of this is that we need a dependency on System.Web library in order to use this class.  So how can we do that? we will create an helper class that will retrieve the context when we need it. In the helper class we will use lazy loading in order to create the context only when we need it. Here is a simple example of how to do that:

public static class ContextHelper<T> where T : ObjectContext, new()

{

#region Consts



private const string ObjectContextKey = "ObjectContext";



#endregion



#region Methods



public static T GetCurrentContext()

{

HttpContext httpContext = HttpContext.Current;

if (httpContext != null)

{

string contextTypeKey = ObjectContextKey + typeof(T).Name;

if (httpContext.Items[contextTypeKey] == null)

{

httpContext.Items.Add(contextTypeKey, new T());

}

return httpContext.Items[contextTypeKey] as T;

}

throw new ApplicationException("There is no Http Context available");

}



#endregion

}

Summary

Lets wrap up, in web application my suggestion is to use an ObjectContext per request. This will help us to have a short living ObjectContext and to use its capabilities for change tracking and other mechanisms. There are other solutions such as a static context (don’t do that!) or context per business transaction but as I wrote I prefer context per request.


Requests ASP.NET

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Logging Best Practices Revisited [Video]
  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: