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

Using a Master Page

This topic demonstrates how to manage the OpenAccessContext by declaring it in a MasterPage or in a BasePage.

Declaring the Context in a MasterPage

Suppose, you have an ASP.NET Web Application and a class library with a Telerik Data Access fluent model.

To create such a solution and to configure it, you can use the workflow suggested in the Creating a New Web Application article.

To declare your context in the MasterPage:

  1. Add a new master page to the web application project (SofiaCarRentalWebApp). Name it Site.Master.
  2. Open Default.aspx in Source view and add the master page in its definition:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
             Inherits="SofiaCarRentalWebApp.Default" MasterPageFile="~/Site.Master" %>
    
  3. Open the Master Page for your web application.

  4. In the code-behind file of that page, create a field for the OpenAccessContext and then create a public property, so the context could be accessed from the pages that have this page as a master.
  5. Override the OnInit method. Here you need to initialize the context, i.e. to create a new instance of the OpenAccessContext.
  6. The final step is to dispose the context. You need to override the Dispose method and dispose the context there.
using System;
namespace BestPracticesMasterPage
{
   public partial class SiteMaster : System.Web.UI.MasterPage
   {
       private FluentModel context;

       public FluentModel ModelContext
       {
           get { return this.context; }
           set { this.context = value; }
       }

       protected override void OnInit(EventArgs e)
       {
           base.OnInit(e);
           this.context = new FluentModel();
       }

       public override void Dispose()
       {
           //The disposal of the scope is made here, just before the Page is disposed
           //When using multiple pages the best way is to do this step in the Dispose()
           //method of the Master Page as well
           this.context.Dispose();
           base.Dispose();
       }
   }
}
Public Class Site
    Inherits System.Web.UI.MasterPage
    Private _context As FluentModel

    Public Property ModelContext() As FluentModel
        Get
            Return Me._context
        End Get
        Set(ByVal value As FluentModel)
            Me._context = value
        End Set
    End Property

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
        Me._context = New FluentModel()
    End Sub

    Public Overrides Sub Dispose()
        'The disposal of the scope is made here, just before the Page is disposed
        'When using multiple pages the best way is to do this step in the Dispose()
        'method of the Master Page as well
        Me._context.Dispose()
        MyBase.Dispose()
    End Sub
End Class

For every page that will use the OpenAccessContext, you should declare a field that exposes the context. You need to override the OnInit method and there you will initialize (retrieve) the context. Next in the Page_Load method you could load the required data by using the already retrieved context. The code-snippet below shows how the Default.aspx page could look like:

using System;
using System.Collections.Generic;
using System.Linq;

namespace BestPracticesMasterPage
{
   public partial class _Default : System.Web.UI.Page
   {
       private FluentModel dbContext;

       protected override void OnInit(EventArgs e)
       {
           base.OnInit(e);

           SiteMaster siteMaster = this.Master as SiteMaster;
           this.dbContext = siteMaster.ModelContext;
       }

       protected void Page_Load(object sender, EventArgs e)
       {
           // Load your data here.
           List<Category> categories = this.dbContext.Categories.ToList();
       }
   }
}
Public Class _Default
    Inherits System.Web.UI.Page

    Private dbContext As FluentModel

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)

        Dim _siteMaster As Site = TryCast(Me.Master, Site)
        Me.dbContext = _siteMaster.ModelContext
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Load your data here.
        Dim categories As List(Of Category) = Me.dbContext.Categories.ToList()
    End Sub
End Class

Declaring the Context in a BasePage

An alternative approach is to define the context in a base class that inherits System.Web.UI.Page. Then the other pages should derive from the base page and use the context. For example:

using System;

namespace BestPracticesMasterPage
{
   public class BaseUIPage : System.Web.UI.Page
   {
       protected FluentModel DbContext
       {
           get;
           private set;
       }

       protected override void OnInit( EventArgs e )
       {
           this.DbContext = new FluentModel();
           base.OnInit( e );
       }

       public override void Dispose()
       {
           if ( this.DbContext != null )
           {
               this.DbContext.Dispose();
           }
           base.Dispose();
       }
   }
}
Public Class BaseUIPage
    Inherits System.Web.UI.Page
    Private privateDbContext As FluentModel

    Protected Property DbContext() As FluentModel
        Get
            Return privateDbContext
        End Get
        Private Set(ByVal value As FluentModel)
            privateDbContext = value
        End Set
    End Property

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        Me.DbContext = New FluentModel()
        MyBase.OnInit(e)
    End Sub

    Public Overrides Sub Dispose()
        If Me.DbContext IsNot Nothing Then
            Me.DbContext.Dispose()
        End If
        MyBase.Dispose()
    End Sub
End Class

Then, for the concrete pages you will have:

public partial class _Default : BaseUIPage
{
   protected void Page_Load( object sender, EventArgs e )
   {
       // Load your data here.
       List<Category> categories = this.DbContext.Categories.ToList();
   }
}
Public Class _Default
    Inherits BaseUIPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim categories As List(Of Category) = Me.DbContext.Categories.ToList()
    End Sub
End Class