当前位置:编程学习 > asp >>

网上购物系统(Task006)——用户界面层公共函数集WebUtility

频繁的数据库操作,需要一个公共的数据库操作函数集(DBUtility中的SQLHelper.cs);频繁的用户界面操作,也需要一个公共函数集WebUtility.cs。因为频繁,这个类及类中的函数,也做成了静态的。

一、App_Code中添加类WebUtility.cs,并在类中添加函数GetCategoryName()

[csharp] using System; 
using System.Configuration; 
using System.Web; 
using System.Web.Caching; 
 
using WestGarden.DAL; 
 
namespace WestGarden.Web 

    public static class WebUtility 
    { 
        private const string CATEGORY_NAME_KEY = "category_name_{0}"; 
        private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]); 
 
        public static string GetCategoryName(string categoryId) 
        { 
            Category category = new Category(); 
            if (!enableCaching) 
                return category.GetCategory(categoryId).Name; 
 
            string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId); 
 
            string data = (string)HttpRuntime.Cache[cacheKey]; 
            if (data == null) 
            { 
                int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]); 
 
                data = category.GetCategory(categoryId).Name; 
 
                HttpRuntime.Cache.Add(cacheKey, data, null, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null); 
            } 
            return data; 
        } 
    } 

using System;
using System.Configuration;
using System.Web;
using System.Web.Caching;

using WestGarden.DAL;

namespace WestGarden.Web
{
    public static class WebUtility
    {
        private const string CATEGORY_NAME_KEY = "category_name_{0}";
        private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);

        public static string GetCategoryName(string categoryId)
        {
            Category category = new Category();
            if (!enableCaching)
                return category.GetCategory(categoryId).Name;

            string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);

            string data = (string)HttpRuntime.Cache[cacheKey];
            if (data == null)
            {
                int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);

                data = category.GetCategory(categoryId).Name;

                HttpRuntime.Cache.Add(cacheKey, data, null, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return data;
        }
    }
}
 

1、这个函数功能是获取类别名称,获取类别需要进行一下判断,如果允许Cache缓存,就从Cache中获取;如果不允许,就从数据库中查询。因些,使用这个函数需要在Web.config中添加两个设置,是否允许Cache以及Cache的生命期。

[csharp] <add key="EnableCaching" value="true"/> 
 
<add key="CategoryCacheDuration" value="12"/> 
<add key="EnableCaching" value="true"/>

<add key="CategoryCacheDuration" value="12"/>

 

 

2、这个函数如果从数据库进行查询,需要调用DAL中的GetCategory()函数,为此,需要在Category.cs中添加函数GetCategory()

[csharp] public CategoryInfo GetCategory(string categoryId) 

    CategoryInfo category = null; 
 
    SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 20); 
    parm.Value = categoryId; 
 
    using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, parm)) 
    { 
        if (rdr.Read()) 
            category = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2)); 
        else 
            category = new CategoryInfo(); 
    } 
    return category; 

        public CategoryInfo GetCategory(string categoryId)
        {
            CategoryInfo category = null;

            SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 20);
            parm.Value = categoryId;

            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, parm))
            {
                if (rdr.Read())
                    category = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetStrin

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,