Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Use OpenAccess in Web Applications
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > How to: Use OpenAccess in Web Applications

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

This topic describes the most preferable ways of working with Telerik OpenAccess ORM when developing a web application. One of the most important things regarding Telerik OpenAccess ORM based applications is the handling of the ObjectScope inside a web application. The most important thing that needs to be taken 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. In that way an object scope can be declared as a protected member of the Master Page and then accessed in all the pages' code-behind methods.
  • Creating a custom ASP Http Module for the developed web application and dealing with the creation and disposal of the scope in it.

Using MasterPage

  1. Create a Master Page for all the pages that will use the object scope.
  2. In the code-behind file of that page, create a field for the object scope and then create a public property, so the scope could be accessed from the pages that have this page as a Master.
    C# Copy Code
    private IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
    public IObjectScope Scope
    {
     get {
    return scope; }
     set { scope = value; }
    }  
  3. The disposal of the scope is placed in the Dispose method of the Master Page.
    C# Copy Code
    public override void Dispose()
    {
      scope.Dispose();
      
    base.Dispose();
    }  
  4. Fields that expose the object scope and the master page that contains it are declared for every page that will use the object scope. They get initialized in the Page_Init method.
    C# Copy Code
    private ScopeHolder myScopeHolder;
    private IObjectScope cashedScope;      
    protected void Page_Init(object sender, EventArgs e)
    {
       myScopeHolder = (ScopeHolder)Master;
       cachedScope = myScopeHolder.Scope;
    }
  5. After these steps are performed the object scope can be used in every page. You must notice that the same scope instance should be passed as a parameter to the methods in the BLL and DAL.

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, the user should 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).
    C# Copy Code
    public void Init(HttpApplication context)
    {
      context.PreRequestHandlerExecute +=
    new EventHandler(context_PreRequestHandlerExecute);
      context.PostRequestHandlerExecute +=
    new EventHandler(context_PostRequestHandlerExecute);
    }  
  3. In the PreRequestHandlerExecute event handler, a field in the Session requires to be defined where an initialized IObjectScope is placed. A transaction is also started.
    C# Copy Code
    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
       
    if (HttpContext.Current.Session != null)
       {
           HttpContext.Current.Session[SCOPE_KEY] = ObjectScopeProvider1.GetNewObjectScope();
       }
       CreateTransaction();
    }  
  4. In the PostRequestHandlerExecute event handler, the started transaction is committed and the scope is disposed as well as the clearance of the field of the Session that was used for the scope.
    C# Copy Code
    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:
    C# Copy Code
    protected void CreateTransaction()
    {
      IObjectScope scope = (IObjectScope)HttpContext.Current.Session[SCOPE_KEY];
      scope.Transaction.Begin();
    }

    protected void CommitTransaction()
    {
      IObjectScope scope = (IObjectScope)HttpContext.Current.Session[SCOPE_KEY];
      scope.Transaction.Commit();
    }

    protected void DisposeScope()
    {
      IObjectScope scope = (IObjectScope)HttpContext.Current.Session[SCOPE_KEY];
      scope.Dispose();
    }

    protected void ClearSession()
    {
      HttpContext.Current.Session.Remove(SCOPE_KEY);
    }  
  6. After the module is ready, it should be registered in the HttpModules section of the Web.config file.
    XML Copy Code
    <httpModules>
         
    <add name="ScopeModule" type="BestPracticesHttpModule.ScopeModule, BestPracticesHttpModule"/>
    </httpModules>  
  7. An IObjectScope field is defined in every page that will need an object scope with the already created object scope (initialized by the module) in the Page_Init method for the page.
    C# Copy Code
    public partial class _Default : System.Web.UI.Page
    {
       
    private IObjectScope cachedScope;

       
    protected void Page_Init(object sender, EventArgs e)
       {
           
    if (this.Session["objectScope"] != null)
           {
               cachedScope = (IObjectScope)
    this.Session["objectScope"];
           }
       }
    }
  8. That is all that needs to be done so that an HttpModule could handle the scope during a page lifecycle.