服裝管理軟件開發(fā)注意事項(三)
在提交數(shù)據(jù)的時候,我們需要每次進(jìn)行權(quán)力檢查,那么緩存應(yīng)用是個不錯的解決方案之一:
具體實現(xiàn)方式如下:
using System;
保存和取緩存 using System;
using System.Web;
using System.Web.Caching;
namespace NGuestBook.Utility
{
/// <summary>
/// 用于緩存操作
/// </summary>
public sealed class CacheAccess
{
/// <summary>
/// 將對象加入到緩存中
/// </summary>
/// <param name="cacheKey">緩存鍵</param>
/// <param name="cacheObject">緩存對象</param>
/// <param name="dependency">緩存依賴項</param>
public static void SaveToCache(string cacheKey, object cacheObject, CacheDependency dependency)
{
Cache cache = HttpRuntime.Cache;
cache.Insert(cacheKey, cacheObject, dependency);
}
/// <summary>
/// 從緩存中取得對象,不存在則返回null
/// </summary>
/// <param name="cacheKey">緩存鍵</param>
/// <returns>獲取的緩存對象</returns>
public static object GetFromCache(string cacheKey)
{
Cache cache = HttpRuntime.Cache;
return cache[cacheKey];
}
}
}
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Web.Caching;
using NGuestBook.Utility;
namespace NGuestBook.Factory
{
/// <summary>
/// 依賴注入提供者
/// 使用反射機制實現(xiàn)
/// </summary>
public sealed class DependencyInjector
{
/// <summary>
/// 取得數(shù)據(jù)訪問層對象
/// 首先檢查緩存中是否存在,如果不存在,則利用反射機制返回對象
/// </summary>
/// <param name="className">數(shù)據(jù)訪問類名稱</param>
/// <returns>數(shù)據(jù)訪問層對象</returns>
public static object GetDALObject(string className)
{
/// <summary>
/// 取得數(shù)據(jù)訪問層名稱,首先檢查緩存,不存在則到配置文件中讀取
/// 緩存依賴項為Web.Config文件
/// </summary>
object dal = CacheAccess.GetFromCache("DAL");
if (dal == null)
{
CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
dal = ConfigurationManager.AppSettings["DAL"];
CacheAccess.SaveToCache("DAL", dal, fileDependency);
}
/// <summary>
/// 取得數(shù)據(jù)訪問層對象
/// </summary>
string dalName = (string)dal;
string fullClassName = dalName + "." + className;
object dalObject = CacheAccess.GetFromCache(className);
if (dalObject == null)
{
CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
dalObject = Assembly.Load(dalName).CreateInstance(fullClassName);
CacheAccess.SaveToCache(className, dalObject, fileDependency);
}
return dalObject;
}
/// <summary>
/// 取得業(yè)務(wù)邏輯層對象
/// 首先檢查緩存中是否存在,如果不存在,則利用反射機制返回對象
/// </summary>
/// <param name="className">業(yè)務(wù)邏輯類名稱</param>
/// <returns>業(yè)務(wù)邏輯層對象</returns>
public static object GetBLLObject(string className)
{
/// <summary>
/// 取得業(yè)務(wù)邏輯層名稱,首先檢查緩存,不存在則到配置文件中讀取
/// 緩存依賴項為Web.Config文件
/// </summary>
object bll = CacheAccess.GetFromCache("BLL");
if (bll == null)
{
CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
bll = ConfigurationManager.AppSettings["BLL"];
CacheAccess.SaveToCache("BLL", bll, fileDependency);
}
/// <summary>
/// 取得業(yè)務(wù)邏輯層對象
/// </summary>
string bllName = (string)bll;
string fullClassName = bllName + "." + className;
object bllObject = CacheAccess.GetFromCache(className);
if (bllObject == null)
{
CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
bllObject = Assembly.Load(bllName).CreateInstance(fullClassName);
CacheAccess.SaveToCache(className, bllObject, fileDependency);
}
return bllObject;
}
}
}