小整的一個通用的BLL層及DAL層基類,可能寫得很差,作爲代碼收集,多多指教sql
DAL 的接口ide
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); }
BLL的接口spa
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); }
DAL,通用基類code
/// <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); }
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); } }
調用代碼以下:blog
var bll = new BaseBLL<Cfg_Codes_DAL, Cfg_Codes_Entity>(dbHelp);