基于CodeSmith代码生成之业务逻辑层

      本业务逻辑是的生成也是基于单表的业务逻辑,使用了泛性的方法来对业务逻辑进行了抽象,大简化了重复代码的开发,并且如果有特殊的要求,还可以自定义业务逻辑类,从现有的类进行继承实现更复杂的操作。

      首先在每个系统中,都会存在有日志系统和异常的处理和记录,首先来定义一个日志异常接口,如下:

public interface ILogException
    {
        #region 异常处理
        void WriteException(EntityBase e);
        /// <summary>
        /// WriteException
        /// </summary>
        /// <param name="Ex">Exception</param>
        void WriteException(Exception Ex);

        /// <summary>
        /// WriteException
        /// </summary>
        /// <param name="modelName">模块名称</param>
        /// <param name="Ex">Exception</param>
        void WriteException(string modelName, Exception Ex);
        /// <summary>
        /// WriteException
        /// </summary>
        /// <param name="modelName">模块名称</param>
        /// <param name="message">Exception</param>
        void WriteException(string modelName, string message);

        /// <summary>
        /// 写操作日志
        /// </summary>
        /// <param name="modelName">模块名称</param>
        /// <param name="OperaType">操作类型</param>
        /// <param name="OperaDesc">描述</param>
        /// <param name="OpResult"></param>
        void WriteOperationLogs(string modelName, string OperaType, string OperaDesc, bool OpResult);
        /// <summary>
        /// 调试信息接口
        /// </summary>
        ILog Log { get; }
        #endregion
    }
public interface ILog
{
    void InfoEnter(string message);
    void InfoExit(string message);
    void InfoFormatEnter(string FormatMessage, params object[] messages);
    void InfoFormatExit(string FormatMessage, params object[] messages);

    void Info(string message);
    void InfoFormat(string FormatMessage, params object[] messages);

    void Warn(string message);
    void WarnFromat(string FormatMessage, params object[] messages);
    void Error(string message);
    void ErrorFromat(string FormatMessage, params object[] messages);
    void Fatal(string message);
    void FatalFromat(string FormatMessage, params object[] messages);

    void Warn(string message, Exception e);
    void WarnFromat(string FormatMessage, Exception e, params object[] messages);
    void Error(string message, Exception e);
    void ErrorFromat(string FormatMessage, Exception e, params object[] messages);
    void Fatal(string message, Exception e);
    void FatalFromat(string FormatMessage, Exception e, params object[] messages);
}
所有业务逻辑层都要继承此接口,这样在业务逻辑的操作中,就很容易的记录异常和日志信息,下面就给出通用业务逻辑接口:
隐藏行号 复制代码 业务接口
  1. public interface IBLL<T> : IBLLBase
    
  2.      where T : EntityBase
    
  3.     {
    
  4.         /// <summary>
    
  5.         /// 新增记录
    
  6.         /// </summary>
    
  7.         /// <param name="e">实体</param>
    
  8.         /// <param name="LogInfo">日志信息</param>
    
  9.         /// <returns>成功返回true</returns>
    
  10.         void Add(T e, EntityBase LogInfo);
    
  11.         /// <summary>
    
  12.         /// 删除记录
    
  13.         /// </summary>
    
  14.         /// <param name="e">实体</param>
    
  15.         /// <param name="LogInfo">日志信息</param>
    
  16.         /// <returns>成功返回true</returns>
    
  17.         void Delete(T e, EntityBase LogInfo);
    
  18.         /// <summary>
    
  19.         /// 删除记录,根据条件删除指定表的记录
    
  20.         /// 此方法有注入的可能,请预防
    
  21.         /// </summary>
    
  22.         /// <param name="e">实体</param>
    
  23.         /// <param name="WhereCondition">条件</param>
    
  24.         /// <param name="LogInfo">日志信息</param>
    
  25.         /// <returns>成功返回true</returns>
    
  26.         void Delete(T e, string WhereCondition, EntityBase LogInfo);
    
  27.         /// <summary>
    
  28.         /// 删除记录
    
  29.         /// </summary>
    
  30.         /// <param name="e">实体</param>
    
  31.         /// <param name="LogInfo">日志信息</param>
    
  32.         /// <returns>成功返回true</returns>
    
  33.         void Deletes(T e, EntityBase LogInfo);
    
  34.         /// <summary>
    
  35.         /// 查询外键实体
    
  36.         /// </summary>
    
  37.         /// <param name="e">实体</param>
    
  38.         /// <param name="ForeignKeyName">外键名</param>
    
  39.         /// <param name="LogInfo">日志信息</param>
    
  40.         /// <returns>返回实体</returns>
    
  41.         EntityBase GetEntityByForeignKey(T e, string ForeignKeyName, EntityBase LogInfo);
    
  42.         /// <summary>
    
  43.         /// 分页查询列表
    
  44.         /// </summary>
    
  45.         /// <param name="TableName">表名</param>
    
  46.         /// <param name="FieldNames">字段列表</param>
    
  47.         /// <param name="OrderName">排序名</param>
    
  48.         /// <param name="PageSize">分页大小</param>
    
  49.         /// <param name="PageIndex">页索引</param>
    
  50.         /// <param name="IsReCount">返回记录总数</param>
    
  51.         /// <param name="OrderType">排序类型</param>
    
  52.         /// <param name="strWhere"></param>
    
  53.         /// <param name="LogInfo">日志信息</param>
    
  54.         /// <returns>返回数据集</returns>
    
  55.         DataSet GetList(string TableName, string FieldNames, string OrderName, int PageSize, int PageIndex, bool IsReCount, bool OrderType, string strWhere, EntityBase LogInfo);
    
  56.         /// <summary>
    
  57.         /// 查询列表
    
  58.         /// </summary>
    
  59.         /// <param name="e">实体</param>
    
  60.         /// <param name="WhereCondition">条件</param>
    
  61.         /// <param name="OrderByExpression">排序</param>
    
  62.         /// <param name="LogInfo">日志信息</param>
    
  63.         /// <returns>返回数据集</returns>
    
  64.         DataSet GetList(T e, string WhereCondition, string OrderByExpression, EntityBase LogInfo);
    
  65.         /// <summary>
    
  66.         /// 查询记录
    
  67.         /// </summary>
    
  68.         /// <param name="e"></param>
    
  69.         /// <param name="LogInfo">日志信息</param>
    
  70.         /// <returns>成功返回true</returns>
    
  71.         void Select(T e, EntityBase LogInfo);
    
  72.         /// <summary>
    
  73.         /// 查询外键列表
    
  74.         /// </summary>
    
  75.         /// <param name="e">实体</param>
    
  76.         /// <param name="ForeignKeyName"></param>
    
  77.         /// <param name="LogInfo">日志信息</param>
    
  78.         /// <returns></returns>
    
  79.         DataSet SelectByForeignKey(T e, string ForeignKeyName, EntityBase LogInfo);
    
  80.         /// <summary>
    
  81.         /// 查询外键列表
    
  82.         /// </summary>
    
  83.         /// <param name="e">实体</param>
    
  84.         /// <param name="ForeignKeyName">外键名称</param>
    
  85.         /// <param name="LogInfo">日志信息</param>
    
  86.         /// <returns>返回列表</returns>
    
  87.         List<EntityBase> GetEntityListByForeignKey(T e, string ForeignKeyName, EntityBase LogInfo);
    
  88.         /// <summary>
    
  89.         /// 查询外键列表
    
  90.         /// </summary>
    
  91.         /// <param name="e">实体</param>
    
  92.         /// <param name="ForeignKeyName">外键名称</param>
    
  93.         /// <returns>返回列表</returns>
    
  94.         List<EntityBase> GetEntityListByForeignKey(T e, string ForeignKeyName);
    
  95.         /// <summary>
    
  96.         /// 更新表
    
  97.         /// </summary>
    
  98.         /// <param name="e">实体</param>
    
  99.         /// <param name="WhereCondition">更新条件(SQL)</param>
    
  100.         /// <param name="LogInfo">日志信息</param>
    
  101.         /// <returns>成功返回true</returns>
    
  102.         void Update(T e, string WhereCondition, EntityBase LogInfo);
    
  103.         /// <summary>
    
  104.         /// 更新记录
    
  105.         /// </summary>
    
  106.         /// <param name="e">实体</param>
    
  107.         /// <param name="LogInfo">日志信息</param>
    
  108.         /// <returns>成功返回true</returns>
    
  109.         void Update(T e, EntityBase LogInfo);
    
  110.         /// <summary>
    
  111.         /// 更新记录
    
  112.         /// </summary>
    
  113.         /// <param name="e">实体</param>
    
  114.         /// <param name="updateStatement">更新语句(SQL SET)</param>
    
  115.         /// <param name="WhereCondition">更新条件(where)</param>
    
  116.         /// <param name="LogInfo">日志信息</param>
    
  117.         /// <returns>成功返回true</returns>
    
  118.         void Update(T e, string updateStatement, string WhereCondition, EntityBase LogInfo);
    
  119.         /// <summary>
    
  120.         /// 新增记录
    
  121.         /// </summary>
    
  122.         /// <param name="e">实体</param>
    
  123.         /// <returns>成功返回true</returns>
    
  124.         void Add(T e);
    
  125.         /// <summary>
    
  126.         /// 删除记录
    
  127.         /// </summary>
    
  128.         /// <param name="e">实体</param>
    
  129.         /// <returns>成功返回true</returns>
    
  130.         void Delete(T e);
    
  131.         /// <summary>
    
  132.         /// 根据条件,删除记录
    
  133.         /// </summary>
    
  134.         /// <param name="e">实体</param>
    
  135.         /// <param name="WhereCondition">条件,SQL</param>
    
  136.         /// <returns>成功返回true</returns>
    
  137.         void Delete(T e, string WhereCondition);
    
  138.         /// <summary>
    
  139.         /// 删除记录
    
  140.         /// </summary>
    
  141.         /// <param name="e">实体</param>
    
  142.         /// <returns>成功返回true</returns>
    
  143.         void Deletes(T e);
    
  144.         /// <summary>
    
  145.         /// 查询外键实体
    
  146.         /// </summary>
    
  147.         /// <param name="e">实体</param>
    
  148.         /// <param name="ForeignKeyName">外键名</param>
    
  149.         /// <returns></returns>
    
  150.         EntityBase GetEntityByForeignKey(T e, string ForeignKeyName);
    
  151.         /// <summary>
    
  152.         /// 分页查询列表
    
  153.         /// </summary>
    
  154.         /// <param name="TableName">表名</param>
    
  155.         /// <param name="FieldNames">字段列表</param>
    
  156.         /// <param name="OrderName">排序名</param>
    
  157.         /// <param name="PageSize">分页大小</param>
    
  158.         /// <param name="PageIndex">页索引</param>
    
  159.         /// <param name="IsReCount">返回记录总数</param>
    
  160.         /// <param name="OrderType">排序类型</param>
    
  161.         /// <param name="strWhere">查询条件</param>
    
  162.         /// <returns>返回数据集</returns>
    
  163.         DataSet GetList(string TableName, string FieldNames, string OrderName, int PageSize, int PageIndex, bool IsReCount, bool OrderType, string strWhere);
    
  164.         /// <summary>
    
  165.         /// 查询列表
    
  166.         /// </summary>
    
  167.         /// <param name="e">实体</param>
    
  168.         /// <param name="WhereCondition">条件</param>
    
  169.         /// <param name="OrderByExpression">排序条件</param>
    
  170.         /// <returns></returns>
    
  171.         DataSet GetList(T e, string WhereCondition, string OrderByExpression);
    
  172.         /// <summary>
    
  173.         /// 查询记录
    
  174.         /// </summary>
    
  175.         /// <param name="e">实体</param>
    
  176.         /// <returns>成功返回true</returns>
    
  177.         void Select(T e);
    
  178.         /// <summary>
    
  179.         /// 查询外键实体
    
  180.         /// </summary>
    
  181.         /// <param name="e">实体</param>
    
  182.         /// <param name="ForeignKeyName">外键名</param>
    
  183.         /// <returns></returns>
    
  184.         DataSet SelectByForeignKey(T e, string ForeignKeyName);
    
  185.         /// <summary>
    
  186.         /// 根据条件更新记录
    
  187.         /// </summary>
    
  188.         /// <param name="e">实体</param>
    
  189.         /// <param name="WhereCondition">条件</param>
    
  190.         /// <returns>成功返回tru</returns>
    
  191.         void Update(T e, string WhereCondition);
    
  192.         /// <summary>
    
  193.         /// 更新记录
    
  194.         /// </summary>
    
  195.         /// <param name="e">实体</param>
    
  196.         /// <returns>成功返回true</returns>
    
  197.         void Update(T e);
    
  198.         /// <summary>
    
  199.         /// 根据更新语句和条件更新记录
    
  200.         /// </summary>
    
  201.         /// <param name="e">实体</param>
    
  202.         /// <param name="updateStatement"></param>
    
  203.         /// <param name="WhereCondition"></param>
    
  204.         /// <returns>成功返回true</returns>
    
  205.         void Update(T e, string updateStatement, string WhereCondition);
    
  206. 
    
  207.         #region 通用存储过程执行方法,为子类准备
    
  208.         /// <summary>
    
  209.         /// 通用存储过程执行方法
    
  210.         /// </summary>
    
  211.         /// <param name="StroreName">存储过程名</param>
    
  212.         /// <param name="Parame">参数</param>
    
  213.         /// <returns>返回out参数</returns>
    
  214.         IDictionary<string, object> ExceStore(string StoreName, IList<Parameter> Parame);
    
  215.         /// <summary>
    
  216.         /// 通用执行方法,返回数据集
    
  217.         /// 不支持out参数
    
  218.         /// </summary>
    
  219.         /// <param name="StroreName">存储过程名</param>
    
  220.         /// <param name="Parame">参数</param>
    
  221.         /// <returns>返回数据集</returns>
    
  222.         DataSet ExceStoreGetDataSet(string StoreName, IList<Parameter> Parame);
    
  223.         /// <summary>
    
  224.         /// 存储过程名
    
  225.         /// </summary>
    
  226.         /// <param name="StoreName">存储过程名</param>
    
  227.         /// <param name="Parame">参数</param>
    
  228.         /// <returns>返回单个对象</returns>
    
  229.         object ExceStoreGetObject(string StoreName, IList<Parameter> Parame);
    
  230.         /// <summary>
    
  231.         /// 通用存储过程执行方法
    
  232.         /// </summary>
    
  233.         /// <param name="StroreName">存储过程名</param>
    
  234.         /// <param name="Parame">参数</param>
    
  235.         /// <param name="LogInfo">日志</param>
    
  236.         /// <returns>返回out参数</returns>
    
  237.         IDictionary<string, object> ExceStore(string StoreName, IList<Parameter> Parame, EntityBase LogInfo);
    
  238.         /// <summary>
    
  239.         /// 通用执行方法,返回数据集
    
  240.         /// 不支持out参数
    
  241.         /// </summary>
    
  242.         /// <param name="StroreName">存储过程名</param>
    
  243.         /// <param name="Parame">参数</param>
    
  244.         /// <param name="LogInfo">日志</param>
    
  245.         /// <returns>返回数据集</returns>
    
  246.         DataSet ExceStoreGetDataSet(string StoreName, IList<Parameter> Parame, EntityBase LogInfo);
    
  247.         /// <summary>
    
  248.         /// 通用执行存储过程方法
    
  249.         /// 
    
  250.         /// </summary>
    
  251.         /// <param name="StoreName">存储过程名</param>
    
  252.         /// <param name="Parame">参数</param>
    
  253.         /// <param name="LogInfo">日志信息</param>
    
  254.         /// <returns>返回单个对象</returns>
    
  255.         object ExceStoreGetObject(string StoreName, IList<Parameter> Parame, EntityBase LogInfo);
    
  256.         #endregion
    
  257. 
    
  258.         /// <summary>
    
  259.         /// 批量插入列表
    
  260.         /// </summary>
    
  261.         /// <param name="EntityList">实体列表</param>
    
  262.         /// <param name="LogInfo">日志信息</param>
    
  263.         /// <returns></returns>
    
  264.         void Add(IList<EntityBase> EntityList, EntityBase LogInfo);
    
  265.         /// <summary>
    
  266.         /// 批量插入列表
    
  267.         /// </summary>
    
  268.         /// <param name="EntityList">实体列表</param>
    
  269.         /// <returns></returns>
    
  270.         void Add(IList<EntityBase> EntityList);
    
  271. 
    
  272.         /// <summary>
    
  273.         /// 查询列表
    
  274.         /// </summary>
    
  275.         /// <param name="e">实体</param>
    
  276.         /// <param name="WhereCondition">条件</param>
    
  277.         /// <param name="OrderByExpression">排序条件,支持多排序字段</param>
    
  278.         /// <returns>返回强类型列表</returns>
    
  279.         List<T> GetEntityList(T e, string WhereCondition, string OrderByExpression);
    
  280.         /// <summary>
    
  281.         /// 查询视图或表,返回强类型的对象
    
  282.         /// </summary>
    
  283.         /// <param name="e">实体</param>
    
  284.         /// <param name="OrderName">排序字段</param>
    
  285.         /// <param name="PageSize">单页大小</param>
    
  286.         /// <param name="PageIndex">页面编号</param>
    
  287.         /// <param name="IsReCount">是否返回记录总数</param>
    
  288.         /// <param name="OrderType">排序类型</param>
    
  289.         /// <param name="strWhere">过滤条件</param>
    
  290.         /// <param name="TotalCount">返回记录总数</param>
    
  291.         /// <returns>返回查询到的记录</returns>
    
  292.         List<T> GetEntityList(T e, string OrderName, int PageSize, int PageIndex, bool IsReCount, bool OrderType, string strWhere,ref int TotalCount);
    
  293.         /// <summary>
    
  294.         /// 查询列表
    
  295.         /// </summary>
    
  296.         /// <param name="e">实体</param>
    
  297.         /// <param name="WhereCondition">条件</param>
    
  298.         /// <param name="OrderByExpression">排序条件,支持多排序字段</param>
    
  299.         /// <param name="LogInfo">日志信息</param>
    
  300.         /// <returns>返回强类型列表</returns>
    
  301.         List<T> GetEntityList(T e, string WhereCondition, string OrderByExpression, EntityBase LogInfo);
    
  302.         /// <summary>
    
  303.         /// 查询视图或表,返回强类型的对象
    
  304.         /// </summary>
    
  305.         /// <param name="e">实体</param>
    
  306.         /// <param name="OrderName">排序字段</param>
    
  307.         /// <param name="PageSize">单页大小</param>
    
  308.         /// <param name="PageIndex">页面编号</param>
    
  309.         /// <param name="IsReCount">是否返回记录总数</param>
    
  310.         /// <param name="OrderType">排序类型</param>
    
  311.         /// <param name="strWhere">过滤条件</param>
    
  312.         /// <param name="TotalCount">返回记录总数</param>
    
  313.         /// <param name="LogInfo">日志信息</param>
    
  314.         /// <returns>返回查询到的记录</returns>
    
  315.         List<T> GetEntityList(T e, string OrderName, int PageSize, int PageIndex, bool IsReCount, bool OrderType, string strWhere, ref int TotalCount, EntityBase LogInfo);
    
  316. 
    
  317.         /// <summary>
    
  318.         /// 批量更新成员,可以更新多个表
    
  319.         /// </summary>
    
  320.         /// <param name="EntityList">实体列表</param>
    
  321.         /// <param name="LogInfo">操作日志信息</param>
    
  322.         /// <returns></returns>
    
  323.         void Update(IList<EntityBase> EntityList, EntityBase LogInfo);
    
  324.         /// <summary>
    
  325.         /// 批量更新成员,可以更新多个表
    
  326.         /// </summary>
    
  327.         /// <param name="EntityList">实体列表</param>
    
  328.         /// <returns></returns>
    
  329.         void Update(IList<EntityBase> EntityList);
    
  330.         /// <summary>
    
  331.         /// 删除表记录
    
  332.         /// </summary>
    
  333.         /// <param name="EntityList">记录列表,可以是多个表的数据,在事务中执行</param>
    
  334.         /// <param name="LogInfo">日志信息</param>
    
  335.         /// <returns>成功返true</returns>
    
  336.         void Delete(IList<EntityBase> EntityList, EntityBase LogInfo);
    
  337.         /// <summary>
    
  338.         /// 删除表记录
    
  339.         /// </summary>
    
  340.         /// <param name="EntityList">记录列表,可以是多个表的数据,在事务中执行</param>
    
  341.         /// <returns>成功返true</returns>
    
  342.         void Delete(IList<EntityBase> EntityList);
    
  343.     }
    

上面接口了出现了EntityBase类型,这个是我上一篇中定义的一个抽象实体类,通过这种抽象,就可以实现一个业务类操作所有实体(不同表)。

为实现业务层对数据访问层的访问,我定义了一个工具类:

隐藏行号 复制代码 数据访问层工具类
  1. public static class DAHelper
    
  2.     {
    
  3.         /// <summary>
    
  4.         /// 默认的数据库连接字符串
    
  5.         /// </summary>
    
  6.         public static readonly string ConnectionStringKey = "ConnectionStringKey";
    
  7.         /// <summary>
    
  8.         /// 返回事务型的数据访问层接口
    
  9.         /// </summary>
    
  10.         /// <returns>数据访问层接口</returns>
    
  11.         public static IDABase GetIDABase()
    
  12.         {
    
  13.             return GetIDABase(true);
    
  14.         }
    
  15. 
    
  16.         /// <summary>
    
  17.         /// 返回数据访问层接口对象
    
  18.         /// </summary>
    
  19.         /// <param name="IsTran">是启用事务</param>
    
  20.         /// <returns>数据层访问接口</returns>
    
  21.         public static IDABase GetIDABase(bool IsTran)
    
  22.         {
    
  23.             return GetIDABase(IsTran, ConnectionStringKey);
    
  24.         }
    
  25.         /// <summary>
    
  26.         /// 返回数据访问层接口对象
    
  27.         /// </summary>
    
  28.         /// <param name="IsTran">事务隔离级别</param>
    
  29.         /// <param name="ConnectionName">链接的名称</param>
    
  30.         /// <returns>数据层访问接口</returns>
    
  31.         public static IDABase GetIDABase(IsolationLevel IsolationLevel, string ConnectionName)
    
  32.         {
    
  33.             return new Dal.EnterpriseLibraryDABase(IsolationLevel, ConnectionName);
    
  34.         }
    
  35. 
    
  36.         /// <summary>
    
  37.         /// 返回数据访问层接口对象
    
  38.         /// </summary>
    
  39.         /// <param name="IsTran">事务隔离级别</param>
    
  40.         /// <returns>数据层访问接口</returns>
    
  41.         public static IDABase GetIDABase(IsolationLevel IsolationLevel)
    
  42.         {
    
  43.             return GetIDABase(IsolationLevel, ConnectionStringKey);
    
  44.         }
    
  45.         /// <summary>
    
  46.         /// 返回数据访问层接口对象
    
  47.         /// </summary>
    
  48.         /// <param name="IsTran">是启用事务</param>
    
  49.         /// <param name="ConnectionName">链接的名称</param>
    
  50.         /// <returns>数据层访问接口</returns>
    
  51.         public static IDABase GetIDABase(bool IsTran, string ConnectionName)
    
  52.         {
    
  53.             return new Dal.EnterpriseLibraryDABase(IsTran, ConnectionName);
    
  54.         }
    
  55.     }
    

这样就可以很方便的开起一个事务,同时也对开人员对事务的了解可以简化。

 

下面来看一下,业务逻辑的具体实现(查询实体):
 

显示行号 复制代码
  1. public void Select(T e, EntityBase LogInfo)
    
  2.         {
    
  3.             IDABase dalbase = null;
    
  4.             try
    
  5.             {
    
  6.                 dalbase = GetIDABase();
    
  7.                 if (!dalbase.Select(e))
    
  8.                 {
    
  9.                     throw new Exception("查询出错。");
    
  10.                 }
    
  11.                 else
    
  12.                 {
    
  13.                     if (LogInfo != null) dalbase.Insert(LogInfo);
    
  14.                     dalbase.Commit();
    
  15.                 }
    
  16.             }
    
  17.             catch (Exception ee)
    
  18.             {
    
  19.                 if (dalbase != null)
    
  20.                     dalbase.Rollback();
    
  21.                 WriteException(ee);
    
  22.                 throw new KMToolException(ee.Message, ee);
    
  23.             }
    
  24.             finally
    
  25.             {
    
  26.                 if (dalbase != null)
    
  27.                     dalbase.Close();
    
  28.             }
    
  29.         }
    

这样所有的方法都会出现一种统一的结构,如下:

public void 方法名(参数)
        {
            IDABase dalbase = null;//定义数据库访问对象
            try
            {
                dalbase = GetIDABase();//获取数据库接口实例
//执行操作,同时,由于数据访问层有直接执行SQL语句的功能,这样就可以动态的改变当前的数据库,就可以进行多数据库的操作
                dalbase.Commit(); //提交操作
                
            }
            catch (Exception ee)
            {
                if (dalbase != null)
                    dalbase.Rollback();//异常时回滚操作
                WriteException(ee);
                throw new KMToolException(ee.Message, ee);
            }
            finally
            {
                if (dalbase != null)
                    dalbase.Close();//关闭链接
            }
        }
虽然数据访问层提供了对SQL语句执行的功能,但是不建议使用,另外从上面的获取数据访问层实例的是一个方法GetIDABase,包装一层的目的是为了便于单元测试进行mock,
实现如下:
显示行号 复制代码
  1. /// <summary>
    
  2.        /// 返回事务型的数据访问层接口
    
  3.        /// </summary>
    
  4.        /// <returns>数据访问层接口</returns>
    
  5.        protected IDABase GetIDABase()
    
  6.        {
    
  7.            return GetIDABase(true);
    
  8.        }
    
  9.        /// <summary>
    
  10.        /// 返回数据访问层接口对象
    
  11.        /// </summary>
    
  12.        /// <param name="IsTran">是启用事务</param>
    
  13.        /// <returns>数据层访问接口</returns>
    
  14.        protected IDABase GetIDABase(bool IsTran)
    
  15.        {
    
  16.            return GetIDABase(IsTran,null);
    
  17.        }
    
  18.        /// <summary>
    
  19.        /// 返回数据访问层接口对象
    
  20.        /// </summary>
    
  21.        /// <param name="IsTran">是启用事务</param>
    
  22.        /// <param name="ConnectionName">链接的名称</param>
    
  23.        /// <returns>数据层访问接口</returns>
    
  24.        protected IDABase GetIDABase(bool IsTran, string ConnectionName)
    
  25.        {
    
  26.            if (_dal != null)
    
  27.            {
    
  28.                try
    
  29.                {
    
  30.                    _dal.Close();
    
  31.                }
    
  32.                catch { }
    
  33.                
    
  34.                return _dal;
    
  35.            }
    
  36.            return KMTool.Common.Dal.DAHelper.GetIDABase(IsTran, ConnectionName);           
    
  37.        }
    

其实对单元测试进行mock的是一个方法:

private IDABase _dal = null;
       /// <summary>
       /// 测试注入专用方法
       /// </summary>
       /// <param name="dal">注入的数据库访问类</param>
       protected void InitDal(IDABase dal)
       {
           if (dal == null)
               throw new ArgumentNullException("dal","此函数的参数dal不能允许为空.");
           _dal = dal;
       }
它是一个保护型的,在单元测试时,我们可以调用此方法进行注入。
 
这一篇就写到这里,下一篇来说一下,WebUI的生成。
posted @ 2009-10-06 09:17  cdboy  阅读(1046)  评论(2编辑  收藏  举报