|
Article relates to
|
Telerik OpenAccess ORM
|
|
Created by
|
Zoran Kostov
|
|
Last modified
|
April 26, 2010
|
|
Last modified by
|
Serge Ovanesyan
|
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:
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:
The code for the user controls is the following:
ComboBoxUserControl.ascx:
ComboBoxUserControl.cs:
GridUserControl.ascx:
GridUserControl.cs:
Both controls are doing similar operations. They get an context, fetch some result set from the database and bind to that result set. First the Page_Load of the combo-box control is called and there we initialize the context and place in the HttpContext.Items collection. The grid then uses the same context that was used by the combo-box from the other web user control and does a separate call to the database to load some data on its own.
In the code behind of the page there is additional logic that filters the grid with the selected value from the Combo-Box.
The final step that should never be overlooked is the
disposal of the context as well as un-wiring from the combo-box event in the Page_Unload method.
One of the greatest things about using the HttpContext way of handling the context is that with little modifications basically the same workflow can be followed with web parts in
Component Management Systems like
Sitefinity,
SharePoint and
DotNetNuke, expect more on that in the future.
The full code of this example can be found in the
Telerik OpenAccess code library.
Please
Sign In
to rate this article.