通用的BLL层及DAL层基类

小整的一个通用的BLL层及DAL层基类,可能写得不好,做为代码收集,多多指教

 DAL 的接口

public interface IDAL<TEntity>
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Add(TEntity item);

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Update(TEntity item);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Delete(TEntity item);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        bool Delete(object objId);

        /// <summary>
        /// 获取实体类
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        TEntity GetEntity(object objId);

        /// <summary>
        /// 获取所有实体集合
        /// </summary>
        /// <returns></returns>
        IList<TEntity> GetData();

        /// <summary>
        /// 根据条件子句获取实体集合
        /// </summary>
        /// <param name="sqlSubWhere"></param>
        /// <returns></returns>
        IList<TEntity> GetData(string sqlSubWhere);
    }
View Code

BLL的接口

public interface IBLL<TEntity> where TEntity:class,new()
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Add(TEntity item);

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Update(TEntity item);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Delete(TEntity item);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        bool Delete(object objId);

        /// <summary>
        /// 获取实体类
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        TEntity GetEntity(object objId);

        /// <summary>
        /// 获取所有实体集合
        /// </summary>
        /// <returns></returns>
        IList<TEntity> GetData();

        /// <summary>
        /// 根据条件子句获取实体集合
        /// </summary>
        /// <param name="sqlSubWhere"></param>
        /// <returns></returns>
        IList<TEntity> GetData(string sqlSubWhere);
    }
View Code

DAL,通用基类

    /// <summary>
    /// 数据访问层的基类
    /// </summary>
    /// <typeparam name="TEntity">实体例</typeparam>
    public abstract class BaseDAL<TEntity> : IDAL<TEntity>
    {
        public IDbHelp dbHelp { get; set; }

        /// <summary>
        /// 增加对象
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public abstract bool Add(TEntity item);

        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public abstract bool Update(TEntity item);
        
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public abstract bool Delete(TEntity item);


        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        public abstract bool Delete(object objId);

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        public abstract TEntity GetEntity(object objId);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns></returns>
        public abstract IList<TEntity> GetData();

        /// <summary>
        /// 根据条件获取实体
        /// </summary>
        /// <param name="sqlSubWhere"></param>
        /// <returns></returns>
        public abstract IList<TEntity> GetData(string sqlSubWhere);
    }
View Code

BLL,通用基类

    /// <summary>
    /// 业务逻辑的基类对象
    /// </summary>
    /// <typeparam name="TDAL">数据访问层</typeparam>
    /// <typeparam name="TEntity">实体类</typeparam>
    public class BaseBLL<TDAL, TEntity> : IBLL<TEntity> 
        where TDAL:BaseDAL<TEntity>,new()
        where TEntity:class,new()
    {
        protected IDbHelp db = null;
        protected TDAL dal;
        public BaseBLL(IDbHelp dbHelp)
        {
            db = dbHelp;
            dal = new TDAL();
            dal.dbHelp = dbHelp;
        }

        public bool Add(TEntity item)
        {
            return dal.Add(item);
        }
        public bool Update(TEntity item)
        {
            return dal.Update(item);
        }

        public bool Delete(TEntity item)
        {
            return dal.Delete(item);
        }

        public bool Delete(object objId)
        {
            return dal.Delete(objId);
        }

        public TEntity GetEntity(object objId)
        {
            return dal.GetEntity(objId);
        }

        public IList<TEntity> GetData()
        {
            return dal.GetData();
        }

        public IList<TEntity> GetData(string sqlSubWhere)
        {
            return dal.GetData(sqlSubWhere);
        }


       
    }
View Code

 

 

调用代码如下:

var bll = new BaseBLL<Cfg_Codes_DAL, Cfg_Codes_Entity>(dbHelp);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。