ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Custom Caching

In this sample we extend the ExtendedObjectDataSource to show data in a GridView. The data will be cached in the session thanks to the flexibility of the design of the CompatObjectDataSource and the ExtendedObjectDataSource.

The caching for the data source controls in the ExtendedObjectDataSource package is handled by a class that implements the IDataSourceCache interface, that has 3 main methods: GetData, SaveData and InvalidateData. In this sample we create a new data source control inheriting from the ExtendedObjectDataSource and caching the data in the session.

IdNameDescriptionPrice
1Product 1Full description for product number 12
2Product 2Full description for product number 24
3Product 3Full description for product number 36
4Product 4Full description for product number 48
123456789

Sample source code

The ASPX file for this sample is:


<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="CustomCaching.aspx.cs" Inherits="CustomCaching" Title="Custom Caching" %>

 

<%@ Register Assembly="ExtendedObjectDataSource" Namespace="Manu.Web.UI.WebControls" TagPrefix="manu" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="Server">

    <h1>Custom Caching</h1>

 

 

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

        AutoGenerateColumns="False" CssClass="gridview" BackColor="#335789"

        BorderColor="#133769" BorderStyle="Solid" BorderWidth="1px"

        DataSourceID="ObjectDataSource1" PageSize="4" DataKeyNames="Id">

        <Columns>

            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />

            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

            <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression
="Description" />

            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />

        </Columns>

        <RowStyle BackColor="White" ForeColor="#436799" />

        <HeaderStyle ForeColor="White" />

        <PagerStyle ForeColor="White" />

    </asp:GridView>

    <manu:MyCustomObjectDataSource ID="ObjectDataSource1" runat="server"
    OldValuesParameterFormatString
="original_{0}" SelectMethod="LoadAll"
    TypeName
="ProductsDAL" EnableCaching="True">

    </manu:MyCustomObjectDataSource>

</asp:Content>


The custom data source control for this sample is:


using System;

using Manu.Web.UI.WebControls;

using Manu.Web.UI.WebControls.Caching;

 

namespace Samples

{

    /// <summary>A custom data source that caches information in the session.</summary>

    public class MyCustomObjectDataSource : ExtendedObjectDataSource

    {

        public MyCustomObjectDataSource()

        {

        }

 

        protected override IDataSourceCache CreateCache()

        {

            return new SessionCache(View);

        }

    }

}


The custom cache for the custom data source control is:


using System;

using System.Collections.Specialized;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Manu.Web.UI.WebControls;

using Manu.Web.UI.WebControls.Caching;

 

/// <summary>A cache that saves the data in the session</summary>

public class SessionCache : BaseCache

{

    public SessionCache(CompatObjectDataSourceView view) : base(view)

    {

    }

 

    /// <summary>Gets the cached data.</summary>

    public override CachedResult GetData(DataSourceSelectArguments arguments, string 
                                       filterExpression, IOrderedDictionary allParameters)

    {

        // if the view supports custom paging, error

        if (View.EnablePaging) {

            throw new NotSupportedException(View.Owner.ID + ": Caching not supported with
                                                               paging"
);

        }

 

        // if the view supports custom sorting, error

        if (!String.IsNullOrEmpty(arguments.SortExpression) && 
           (View.SortParameterName.Length > 0)) {

            throw new NotSupportedException(View.Owner.ID + ": Caching not supported with
                                                               custom sorting"
);

        }

 

        // if the view supports custom filtering, error

        if(!String.IsNullOrEmpty(filterExpression)&&(View.FilterParameterName.Length>0)){

            throw new NotSupportedException(View.Owner.ID + ": Caching not supported with
                                                                custom filtering"
);

        }

 

        // gets the select parameters

        IOrderedDictionary parameters = CopyDictionary(View.SelectParameters.GetValues
                                                           (View.Context, View.Owner));

 

        // generate a key based on the select parameters

        string cacheKey = GenerateBaseCacheKey(parameters).ToString();

        return HttpContext.Current.Session[cacheKey] as CachedResult;

    }

 

    // saves data to the cache

    public override void SaveData(CachedResult data, DataSourceSelectArguments arguments,
                                string
filterExpression, IOrderedDictionary selectParams)

    {

        // gets the select parameters

        IOrderedDictionary parameters = CopyDictionary(View.SelectParameters.GetValues
                                                        (View.Context, View.Owner));

 

        // generate a key to save the data to the cache

        string cacheKey = GenerateBaseCacheKey(parameters).ToString();

        if (HttpContext.Current.Session[cacheKey] == null) {

            HttpContext.Current.Session[cacheKey] = data;

        }

    }

 

    // invalidates the data stored in the cache

    public override void InvalidateData()

    {

        // gets the select parameters

        IOrderedDictionary parameters = CopyDictionary(View.SelectParameters.GetValues
                                                         (View.Context, View.Owner));

 

        // remove the base key to remove dependent entries

        string cacheKey = GenerateBaseCacheKey(parameters).ToString();

        HttpContext.Current.Session.Remove(cacheKey);

    }

}