Data Access has been discontinued. Please refer to this page for more information.

Handling OpenAccessContext in Web Projects

One of the most important things regarding Telerik Data Access based applications is the handling of the OpenAccessContext inside a web application.

The most important thing you should take care of is that the OpenAccessContext class implements the IDisposable interface. It is traditionally difficult to handle those cases in a web application, because of the stateless nature of the web. However, there are three highly efficient ways of dealing with this issue:

  • Using a MasterPage - the MasterPage is common for all the pages in a web application. You can declare the OpenAccessContext as a member of the MasterPage and access it in all the pages code-behind methods. Read more
  • Using the HttpContext - basically, you need to create a class that is responsible for getting and setting the OpenAccessContext from/to the HttpContext. Read more
  • Creating a new instance of the context for each CRUD operation. Read more

In this tutorial you will use the second approach, i.e. handling the context using the HttpContext. Add a new class named ContextFactory in the CarRentWebSite project.

using System.Web;
using SofiaCarRental.Model;

namespace CarRentWebSite
{
    public class ContextFactory
    {
        private static readonly string contextKey = typeof( FluentModel ).FullName;

        public static FluentModel GetContextPerRequest()
        {
            HttpContext httpContext = HttpContext.Current;
            if ( httpContext == null )
            {
                return new FluentModel();
            }
            else
            {
                FluentModel context = httpContext.Items[contextKey] as FluentModel;

                if ( context == null )
                {
                    context = new FluentModel();
                    httpContext.Items[contextKey] = context;
                }

                return context;
            }
       }

        public static void Dispose()
        {
            HttpContext httpContext = HttpContext.Current;

            if ( httpContext != null )
            {
                FluentModel context = httpContext.Items[contextKey] as FluentModel;

                if ( context != null )
                {
                    context.Dispose();
                    httpContext.Items[contextKey] = null;
                }
            }
        }
    }
}
Imports SofiaCarRental.Model

Public Class ContextFactory
    Private Shared ReadOnly contextKey As String = GetType(FluentModel).FullName

    Public Shared Function GetContextPerRequest() As FluentModel
        Dim _httpContext As HttpContext = HttpContext.Current
        If _httpContext Is Nothing Then
            Return New FluentModel()
        Else
            Dim context As FluentModel = TryCast(_httpContext.Items(contextKey), FluentModel)

            If context Is Nothing Then
                context = New FluentModel()
                _httpContext.Items(contextKey) = context
            End If

            Return context
        End If
    End Function

    Public Shared Sub Dispose()
        Dim _httpContext As HttpContext = HttpContext.Current
        If _httpContext IsNot Nothing Then
            Dim context As FluentModel = TryCast(_httpContext.Items(contextKey), FluentModel)
            If context IsNot Nothing Then
                context.Dispose()
                _httpContext.Items(contextKey) = Nothing
            End If
        End If
    End Sub
End Class