服装管理软件开发注意事项(三)

 在提交数据的时候,我们需要每次进行权力检查,那么缓存应用是个不错的解决方案之一:

具体实现方式如下:

 

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;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Web.Caching;
using NGuestBook.Utility;

namespace NGuestBook.Factory
{
    
/// <summary>
    
/// 依赖注入提供者
    
/// 使用反射机制实现
    
/// </summary>
    public sealed class DependencyInjector
    {
        
/// <summary>
        
/// 取得数据访问层对象
        
/// 首先检查缓存中是否存在,如果不存在,则利用反射机制返回对象
        
/// </summary>
        
/// <param name="className">数据访问类名称</param>
        
/// <returns>数据访问层对象</returns>
        public static object GetDALObject(string className)
        {
            
/// <summary>
            
/// 取得数据访问层名称,首先检查缓存,不存在则到配置文件中读取
            
/// 缓存依赖项为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>
            
/// 取得数据访问层对象
            
/// </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>
        
/// 取得业务逻辑层对象
        
/// 首先检查缓存中是否存在,如果不存在,则利用反射机制返回对象
        
/// </summary>
        
/// <param name="className">业务逻辑类名称</param>
        
/// <returns>业务逻辑层对象</returns>
        public static object GetBLLObject(string className)
        {
            
/// <summary>
            
/// 取得业务逻辑层名称,首先检查缓存,不存在则到配置文件中读取
            
/// 缓存依赖项为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>
            
/// 取得业务逻辑层对象
            
/// </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;
        }
    }
}