Cache Object DataSource : Introduction « Cache « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CacheObjectDataSource" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Cached object data source</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="_peopleGridView" runat="server" 
            DataSourceID="_personObjectDataSource"
            EnableViewState="False" />
        <asp:ObjectDataSource ID="_personObjectDataSource" runat="server" 
            CacheDuration="120"
            EnableCaching="True" SelectMethod="GetPeople" DataObjectTypeName="Person"
            TypeName="SampleData" />    
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CacheObjectDataSource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Output.Write("objds.GetHashCode() = {0}<br />", _personObjectDataSource.GetType().GetHashCode());
        Response.Output.Write("objds.CacheDuration = {0}<br />", _personObjectDataSource.CacheDuration);
        Response.Output.Write("objds.CacheExpirationPolicy = {0}<br />", _personObjectDataSource.CacheExpirationPolicy);
        Response.Output.Write("objds.SqlCacheDependency = {0}<br />", _personObjectDataSource.SqlCacheDependency);
        Response.Output.Write("objds.Typename = {0}<br />", _personObjectDataSource.TypeName);
        Response.Output.Write("objds.SelctMethod = {0}<br />", _personObjectDataSource.SelectMethod);
    }
}

File: Person.cs

using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class Person
{
    private int _age;
    private string _name;
    private bool _isMarried;
    private DateTime _birthDay;

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public bool IsMarried
    {
        get { return _isMarried; }
        set { _isMarried = value; }
    }

    public DateTime BirthDay
    {
        get { return _birthDay; }
        set { _birthDay = value; }
    }

    public Person() { }
    public Person(int age, string name, bool isMarried, DateTime birthDay)
    {
        _age = age;
        _name = name;
        _isMarried = isMarried;
        _birthDay = birthDay;
    }
}

public static class SampleData
{
    public static ICollection<Person> GetPeople()
    {
        List<Person> ret = new List<Person>();

        for (int i = 0; i < 10; i++)
            ret.Add(new Person(i + 20, "Person " + i.ToString(), (i % 2) == 0, 
                               DateTime.Now.AddYears(i-40)));
        
        return ret;
    }
}








13.1.Introduction
13.1.1.Overview of Caching
13.1.2.Basic operations executed on the ASP.NET Cache object
13.1.3.Programmatic Fragment Caching
13.1.4.Cache Object DataSource
13.1.5.Convert data in Cache to integer