Contact Sales: +1-888-365-2779
Community & Support
Home / Community & Support / Knowledge Base / Telerik OpenAccess ORM / General / Best Practices in web development with OpenAccess

Best Practices in web development with OpenAccess

Article Info

Rating: 3

Article information

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.
private BestPracticesEntityDiagrams context = new BestPracticesEntityDiagrams();
 
public BestPracticesEntityDiagrams ModelContext
{
    get { return context; }
    set { context = value; }
}

3.    We add the disposing of the context in the Dispose() method of the Master Page
public override void Dispose()
{
    context.Dispose();
    base.Dispose();
}

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.
public partial class _Default : System.Web.UI.Page
{
    private ScopeHolder myScopeHolder;
    private BestPracticesEntityDiagrams context;
     
    protected void Page_Init(object sender, EventArgs e)
    {
        myScopeHolder = (ScopeHolder)Master;
        context = myScopeHolder.ModelContext;
    }

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).
public void Init(HttpApplication context)
{
        context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
        context.PostRequestHandlerExecute += context_PostRequestHandlerExecute;
}

3.    In the PreRequestHandlerExecute event handler we define a field in the Session where an initialized Context is placed. A transaction is also started.

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
    if (HttpContext.Current.Session != null)
    {
        HttpContext.Current.Session[SCOPE_KEY] = new BestPracticesEntityDiagrams();
        CreateTransaction();
     }
}

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.
void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
    CommitTransaction();
    DisposeScope();
    ClearSession();
}

5.    The methods that were written to make the code more clear for this example look like this:
protected void CreateTransaction()
{
    BestPracticesEntityDiagrams ctx = (BestPracticesEntityDiagrams)HttpContext.Current.Session[SCOPE_KEY];
}
 
protected void CommitTransaction()
{
    BestPracticesEntityDiagrams ctx = (BestPracticesEntityDiagrams)HttpContext.Current.Session[SCOPE_KEY];
    ctx.SaveChanges();
}
protected void DisposeScope()
{
    BestPracticesEntityDiagrams ctx = (BestPracticesEntityDiagrams)HttpContext.Current.Session[SCOPE_KEY];
    ctx.Dispose();
}
protected void ClearSession()
{
    HttpContext.Current.Session.Remove(SCOPE_KEY);
}

6.    Now that the module is finished we should register it in the HttpModules section of the web.config:
<httpModules>
      <add name="ContextModule" type="BestPracticesHttpModule.ContextModule, BestPracticesHttpModule" />
</httpModules>

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:
public partial class MicrosoftsDefault : System.Web.UI.Page
{
    private BestPracticesEntityDiagrams context;
 
    protected void Page_Init(object sender, EventArgs e)
    {
        if (this.Session["objectScope"] != null)
        {
            context = (BestPracticesEntityDiagrams)this.Session["objectScope"];
        }
        else
        {
            throw new NullReferenceException();
        }
    }



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.

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.