工厂 利用映射读取数据层方法并缓存映射数据
1、创建缓存方法(ProjectName是项目名称)
namespace ProjectName.Factory
{
/// <summary>
/// DataCache 的摘要说明。
/// </summary>
public class DataCache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
}
}
2、创建读取数据层的共有方法
导入命名空间 using System.Reflection;
namespace ProjectName.Factory
{
/// <summary>
/// DataAccess 的摘要说明。
/// </summary>
public sealed class DataAccess
{
public static readonly string path=ProjectName+".Factory";
/// <summary>
/// 创建对象或从缓存获取
/// </summary>
/// <param name="path">要映射的命名空间</param>
/// <param name="CacheKey">要映射的命名空间下的类</param>
public static object CreateObject(string path,string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);//从缓存读取
if (objType == null)
{
try
{
objType = Assembly.Load(path).CreateInstance(CacheKey);//反射创建
DataCache.SetCache(CacheKey, objType);// 写入缓存
}
catch
{}
}
return objType;
}
}
}
3、创建Model层 UserInfor、Roles 把所有创建的类都放在 ProjectName.Model的命名空间下
4、创建IDAL层 IUserInfor、IRoles 把所有创建的类都放在 ProjectName.IDAL的命名空间下
5、创建SQLDAL层 UserInfor、Roles 把所有创建的类都放在 ProjectName.SQLDAL的命名空间下
6、创建Factory层 UserInfor、Roles 把所有创建的类都放在 ProjectName.Factory的命名空间下
namespace ProjectName.Factory
{
public class UserInfor
{
public static ProjectName.IDAL.IUserInfor Create()
{
string CacheKey = DataAccess.path+".UserInfor";
object objType=DataAccess.CreateObject(DataAccess.path,CacheKey);
return (ProjectName.IDAL.IUserInfor)objType;
}
}
}
namespace ProjectName.Factory
{
public class Roles
{
public static IDAL.IRoles Create()
{
string CacheKey = DataAccess.path+".Roles";
object objType=DataAccess.CreateObject(DataAccess.path,CacheKey);
return (ProjectName.IDAL.IRoles)objType;
}
}
}
7、创建BLL 层 UserInfor、Roles 把所有创建的类都放在 项目名.BLL的命名空间下
namespace ProjectName.BLL
{
public class UserInfor
{
private static readonly ProjectName.IDAL.IUserInfor dal = ProjectName.Factory.UserInfor.Create();
}
}
namespace ProjectName.BLL
{
public class Roles
{
private static readonly ProjectName.IDAL.IRoles dal = ProjectName.Factory.Roles.Create();
}
}
namespace ProjectName.Factory
{
/// <summary>
/// DataCache 的摘要说明。
/// </summary>
public class DataCache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
}
}
2、创建读取数据层的共有方法
导入命名空间 using System.Reflection;
namespace ProjectName.Factory
{
/// <summary>
/// DataAccess 的摘要说明。
/// </summary>
public sealed class DataAccess
{
public static readonly string path=ProjectName+".Factory";
/// <summary>
/// 创建对象或从缓存获取
/// </summary>
/// <param name="path">要映射的命名空间</param>
/// <param name="CacheKey">要映射的命名空间下的类</param>
public static object CreateObject(string path,string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);//从缓存读取
if (objType == null)
{
try
{
objType = Assembly.Load(path).CreateInstance(CacheKey);//反射创建
DataCache.SetCache(CacheKey, objType);// 写入缓存
}
catch
{}
}
return objType;
}
}
}
3、创建Model层 UserInfor、Roles 把所有创建的类都放在 ProjectName.Model的命名空间下
4、创建IDAL层 IUserInfor、IRoles 把所有创建的类都放在 ProjectName.IDAL的命名空间下
5、创建SQLDAL层 UserInfor、Roles 把所有创建的类都放在 ProjectName.SQLDAL的命名空间下
6、创建Factory层 UserInfor、Roles 把所有创建的类都放在 ProjectName.Factory的命名空间下
namespace ProjectName.Factory
{
public class UserInfor
{
public static ProjectName.IDAL.IUserInfor Create()
{
string CacheKey = DataAccess.path+".UserInfor";
object objType=DataAccess.CreateObject(DataAccess.path,CacheKey);
return (ProjectName.IDAL.IUserInfor)objType;
}
}
}
namespace ProjectName.Factory
{
public class Roles
{
public static IDAL.IRoles Create()
{
string CacheKey = DataAccess.path+".Roles";
object objType=DataAccess.CreateObject(DataAccess.path,CacheKey);
return (ProjectName.IDAL.IRoles)objType;
}
}
}
7、创建BLL 层 UserInfor、Roles 把所有创建的类都放在 项目名.BLL的命名空间下
namespace ProjectName.BLL
{
public class UserInfor
{
private static readonly ProjectName.IDAL.IUserInfor dal = ProjectName.Factory.UserInfor.Create();
}
}
namespace ProjectName.BLL
{
public class Roles
{
private static readonly ProjectName.IDAL.IRoles dal = ProjectName.Factory.Roles.Create();
}
}