Article relates to
Telerik OpenAccess ORM
April 26, 2010
Last modified by
This article discusses one of the most-common concerns of ORM users – the management of the DataContext objects in a real-case web scenario. Here we will add to the Master-Page and HttpModule approaches observed in the first part of this series. The final goal of this article is to successfully manage the scope lifetime by taking advantage of the Items collection of the HttpContext.
This way we are obtaining the context on a per-request basis. This approach of managing the context lifecycle is especially powerful in web projects where web pages are built from multiple web user controls. That means, a context will be obtained once on every request on a web-page and then shared among all web user controls composing the page as well as the page itself. That way we have consistency between the data that multiple parts of the web page use with different logic. Another advantage is the ability to share data between different user controls because the data all controls operate come from the same Context instance. To be able to obtain the context in such generic manner we need a class that we will use as a factory for our object scope instances.
Bellow is a basic implementation for such class:
NorthwindEntityDiagrams GetContextPerRequest(HttpContext context)
key = context.GetHashCode().ToString(
) + System.Threading.Thread.CurrentContext.ContextID.ToString();
NorthwindEntityDiagrams ctx =
ctx = (NorthwindEntityDiagrams)context.Items[key];
context.Items[key] = ctx;
The ContextFactory class has only one method - GetContextPerRequest. Then we create a key based on the ContextID of the context of the current thread where the request is processed. That key is a unique for every HttpRequest on a page. Then there is a sample logic that returns the context from the Items collection of the HttpContext and if it is not yet in that collection we just initialize it and place it in the collection. Example:
We have a sample web page containing of two web user control. The first one is called ComboBoxUserControl.ascxthat has just a RadComboBox on it and the other one is GridUserControl.ascx that holds a RadGrid. Default.aspx:
sender, EventArgs e)
var ctx = ContextFactory.GetContextPerRequest(
var result = from cust
RadComboBox1.DataSource = result;
source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
NorthwindEntityDiagrams ctx = ContextFactory.GetContextPerRequest(
var result = from ord
RadGrid1.DataSource = result;
o, RadComboBoxSelectedIndexChangedEventArgs e)
selectedId = e.Value;
Customer customerFromCombo = ctx.Customers.Where(cust => cust.CustomerID.Equals(selectedId)).First();
where ord.Customer == customerFromCombo
gridFromUserControl.DataSource = result;
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.