|
Article relates to
|
Telerik OpenAccess ORM
|
|
Created by
|
Zoran Kostov
|
|
Last modified
|
April 26, 2010
|
|
Last modified by
|
Serge Ovanesyan
|
Best Practices in web development with OpenAccess:
This article describes the most preferable ways of working with OpenAccess when developing a web application.
One of the most important things regarding OpenAccess based applications is the handling of the context inside a web application.
For the purpose of this article we will use the
RadGridView component. We will present the information of some sample “TestPerson” objects in one of our pages . A GetList() method ( part of the “TestPersonManager” BLL class) is used to return a collection of “TestPerson” persistence objects.
The most important thing that we have to take care of with the
IObjectScope instance is its
disposal. It is traditionally difficult to handle those cases in a web application, because of the stateless nature of the web. However there are two highly efficient ways of dealing with this issue:
• Using a
Master Page - that is common for all the pages in a web application. That way we can declare the context as a protected member of the Master Page and then access it in all our pages code-behind methods.
• Creating a custom ASP
Http Module for our web application and dealing with the creation and disposal of the context in it.
The choice which approach is to be used is left to the programmer.
Dealing with object scope using Master Page:
1. Create a Master Page for all the pages that will use the context.
2.
In the code-behind file of that page create a field for our object
scope and then a protected property, so that the context could be
accessed from the pages that have this page as a Master.
3. We add the disposing of the context in the Dispose() method of the Master Page
4. Fields that expose the context and the master page containing it are declared for every page that will use the context. They get initialized in the Page_Init() method.
After these steps are taken we can use our object scope in every page depending on our needs. We must notice that this same context instance should be passed as a parameter to the methods in the BLL and DAL as it is shown in the code library project for this
topic.
Dealing with objects using custom HttpModule:
This approach is considered as more “elegant”. You can find more on the basics of taking advantage of HttpModules in this
article.
1. Create a custom HttpModule.
2. In the module’s
Init method we subscribe to the HttpApplication’s
PreRequestHandlerExecute and
PostRequestHandlerExecute events. These events are fired just before and after ASP .Net starts and finishes with the execution of a web Page (or custom HttpHandler).
3. In the
PreRequestHandlerExecute event handler we define a field in the
Session where an initialized
Context is placed. A transaction is also started.
4. In the
PostRequestHandlerExecute event handler we commit the started transaction and dispose the context as well as we clear the field of the Session that was used for the context.
5. The methods that were written to make the code more clear for this example look like this:
6. Now that the module is finished we should register it in the HttpModules section of the
web.config:
7. We define an Context field in every page that will need an object scope with our already created context (initialized by the module) in the
Page_Init() method for the page:
NOTE: For the simplicity of the example we only show the principles of
using HttpModule for context management. Using HttpModule in this form
might cause issues with AJAX components on the page. In order to use
this method of handling the Context, the HttpModule must go
through some additional development so it processes the AJAX requests
correctly.
It does not seem very easy at first but these are the steps that need to be done so that an HttpModule could handle the context during a page lifecycle. This approach is also presented in the sample project for the best practices that you can find in our
code library.