自己在项目中的学习总结:利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口
2015-08-10 10:18
1126 查看
工作一年多,自己小小的心得,方便自己以后查找。
首先上架构截图:
且看截图,其中DALFactory为工厂类库,IDAL为接口类库,SQLServerDAL则为实际的数据层实现类库。
1、数据层实现。这个不多说,起始就是编写相关数据操作的方法。
public partial class ActivityInfo:IActivityInfo
{
public int Add(ActivityInfo model)
{
reutrn 1;
}
}
2、IDAL接口
public interface IActivityInfo
{
int Add(ActivityInfo model);
}
3、重点:DALFactory为工厂类库。工厂方法 反射 缓存
直接上代码,简单易懂:
类库中的基类:
/// <summary>
/// Abstract Factory base class
/// </summary>
public class DataAccessBase
{
protected static readonly string AssemblyPath = ConfigHelper.GetConfigString("DAL");
#region CreateObject
//不使用缓存
protected static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
{
try
{
object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
return objType;
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 记录错误日志
return null;
}
}
//使用缓存
protected static object CreateObject(string AssemblyPath, string classNamespace)
{
object objType = DataCache.GetCache(classNamespace);
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
DataCache.SetCache(classNamespace, objType);// 写入缓存
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 记录错误日志
}
}
return objType;
}
#endregion
}
映射实现:
public sealed class DAJLT : DataAccessBase
{
/// <summary>
/// 创建UserAttendance数据层接口。考勤信息表
/// </summary>
public static IUserAttendance CreateUserAttendance()
{
string ClassNamespace = AssemblyPath + ".JLT.UserAttendance";
object objType = CreateObject(AssemblyPath, ClassNamespace);
return (IUserAttendance)objType;
}
}
4、BLL层的数据层方法调用:
public partial class Supplier
{
private readonly ISupplierAD dal = DAShopSupplier.CreateSupplierAD();
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(int AdvertisementId)
{
return dal.Exists(AdvertisementId);
}
}
此后如果更换数据库,新增一个数据层类库,只是替换webconfig中配置项就可以即可。
首先上架构截图:
且看截图,其中DALFactory为工厂类库,IDAL为接口类库,SQLServerDAL则为实际的数据层实现类库。
1、数据层实现。这个不多说,起始就是编写相关数据操作的方法。
public partial class ActivityInfo:IActivityInfo
{
public int Add(ActivityInfo model)
{
reutrn 1;
}
}
2、IDAL接口
public interface IActivityInfo
{
int Add(ActivityInfo model);
}
3、重点:DALFactory为工厂类库。工厂方法 反射 缓存
直接上代码,简单易懂:
类库中的基类:
/// <summary>
/// Abstract Factory base class
/// </summary>
public class DataAccessBase
{
protected static readonly string AssemblyPath = ConfigHelper.GetConfigString("DAL");
#region CreateObject
//不使用缓存
protected static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
{
try
{
object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
return objType;
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 记录错误日志
return null;
}
}
//使用缓存
protected static object CreateObject(string AssemblyPath, string classNamespace)
{
object objType = DataCache.GetCache(classNamespace);
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
DataCache.SetCache(classNamespace, objType);// 写入缓存
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 记录错误日志
}
}
return objType;
}
#endregion
}
映射实现:
public sealed class DAJLT : DataAccessBase
{
/// <summary>
/// 创建UserAttendance数据层接口。考勤信息表
/// </summary>
public static IUserAttendance CreateUserAttendance()
{
string ClassNamespace = AssemblyPath + ".JLT.UserAttendance";
object objType = CreateObject(AssemblyPath, ClassNamespace);
return (IUserAttendance)objType;
}
}
4、BLL层的数据层方法调用:
public partial class Supplier
{
private readonly ISupplierAD dal = DAShopSupplier.CreateSupplierAD();
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(int AdvertisementId)
{
return dal.Exists(AdvertisementId);
}
}
此后如果更换数据库,新增一个数据层类库,只是替换webconfig中配置项就可以即可。
相关文章推荐
- 4.2 标准库--日期和事件
- Java连接oracle数据库的两种常用方法
- jquery 动态创建元素,在元素山添加事件
- 内存&&光驱常见问题处理
- unity3d 圆形遮罩
- 抽开奖
- 使用JDeveloper 11g 发布WebService
- 日经社説 20150810 コメ先物存続は農協参加で
- 层次打印二叉树
- Ubuntu14.04 DNS Server配置
- CentOS安装Firefox的Flash播放器插件
- LA_4329_HDU_2492_PingPong
- POJ1679 The Unique MST(kru)
- 8月初.wang域名总量TOP14:35互联耐思尼克竞争激烈
- 如何实现鼠标放在图片上出现文字说明效果?
- 让crond以秒为单位执行任务的方法
- IE页面关闭时事件 Onunload与Onbeforeunload
- UVA 10723 Cyborg Genes
- 计蒜客 难题题库 218 配对元素
- 安全应急之网络异常排查win&linux-20150805