Fork me on GitHub

 

工作一年多,自己小小的心得,方便自己以后查找。
首先上架构截图:

                    

且看截图,其中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中配置项就可以即可。

    

 

posted on 2015-08-10 10:18  乱花渐欲迷人眼  阅读(326)  评论(0编辑  收藏  举报