本系列目錄:ASP.NET MVC4入門到精通系列目錄彙總
html
在上一篇中,咱們已經把項目的基本框架搭起來了,這一篇咱們就來實現業務層和數據層的父接口及父類。sql
1、咱們先來定義一個業務層父接口IBaseBLL.cs數據庫
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace IBLL { /// <summary> /// 業務父 接口 /// </summary> /// <typeparam name="T"></typeparam> public interface IBaseBLL<T> where T:class,new() { //定義 增刪改查 方法 #region 1.0 新增 實體 +int Add(T model) /// <summary> /// 新增 實體 /// </summary> /// <param name="model"></param> /// <returns></returns> int Add(T model); #endregion #region 2.0 根據 id 刪除 +int Del(T model) /// <summary> /// 根據 id 刪除 /// </summary> /// <param name="model">包含要刪除id的對象</param> /// <returns></returns> int Del(T model); #endregion #region 3.0 根據條件刪除 +int DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 3.0 根據條件刪除 /// </summary> /// <param name="delWhere"></param> /// <returns></returns> int DelBy(Expression<Func<T, bool>> delWhere); #endregion #region 4.0 修改 +int Modify(T model, params string[] proNames) /// <summary> /// 4.0 修改,如: /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; /// this.Modify(u, "uLoginName"); /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> int Modify(T model, params string[] proNames); #endregion #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// <summary> /// 4.0 批量修改 /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="whereLambda">查詢條件</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames); #endregion #region 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// <summary> /// 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> List<T> GetListBy(Expression<Func<T, bool>> whereLambda); #endregion #region 5.1 根據條件 排序 和查詢 + List<T> GetListBy<TKey> /// <summary> /// 5.1 根據條件 排序 和查詢 /// </summary> /// <typeparam name="TKey">排序字段類型</typeparam> /// <param name="whereLambda">查詢條件 lambda表達式</param> /// <param name="orderLambda">排序條件 lambda表達式</param> /// <returns></returns> List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda); #endregion #region 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">頁碼</param> /// <param name="pageSize">頁容量</param> /// <param name="whereLambda">條件 lambda表達式</param> /// <param name="orderBy">排序 lambda表達式</param> /// <returns></returns> List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy); #endregion } }
2、定義一個數據層父接口IBaseDAL.cs數組
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace IDAL { /// <summary> /// 數據層父接口 /// </summary> public interface IBaseDAL<T> where T:class ,new() { //定義增刪改查方法 #region 1.0 新增 實體 +int Add(T model) /// <summary> /// 新增 實體 /// </summary> /// <param name="model"></param> /// <returns></returns> int Add(T model); #endregion #region 2.0 根據 id 刪除 +int Del(T model) /// <summary> /// 根據 id 刪除 /// </summary> /// <param name="model">包含要刪除id的對象</param> /// <returns></returns> int Del(T model); #endregion #region 3.0 根據條件刪除 +int DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 3.0 根據條件刪除 /// </summary> /// <param name="delWhere"></param> /// <returns></returns> int DelBy(Expression<Func<T, bool>> delWhere); #endregion #region 4.0 修改 +int Modify(T model, params string[] proNames) /// <summary> /// 4.0 修改,如: /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; /// this.Modify(u, "uLoginName"); /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> int Modify(T model, params string[] proNames); #endregion #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// <summary> /// 4.0 批量修改 /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="whereLambda">查詢條件</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames); #endregion #region 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// <summary> /// 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> List<T> GetListBy(Expression<Func<T, bool>> whereLambda); #endregion #region 5.1 根據條件 排序 和查詢 + List<T> GetListBy<TKey> /// <summary> /// 5.1 根據條件 排序 和查詢 /// </summary> /// <typeparam name="TKey">排序字段類型</typeparam> /// <param name="whereLambda">查詢條件 lambda表達式</param> /// <param name="orderLambda">排序條件 lambda表達式</param> /// <returns></returns> List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda); #endregion #region 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">頁碼</param> /// <param name="pageSize">頁容量</param> /// <param name="whereLambda">條件 lambda表達式</param> /// <param name="orderBy">排序 lambda表達式</param> /// <returns></returns> List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy); #endregion } }
三、咱們數據庫中有許多的表,一般每一張表都對應一個業務實體,若是咱們每個業務實體類都本身去手寫的話,太浪費時間了,咱們能夠使用T4模板,簡化這些枯燥的操做,提升咱們的開發效率。框架
新建一個文本模板ide
而後把如下代碼複製上去,post
<#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs"#> <# CodeGenerationTools code = new CodeGenerationTools(this); MetadataLoader loader = new MetadataLoader(this); CodeRegion region = new CodeRegion(this, 1); MetadataTools ef = new MetadataTools(this); string inputFile = @"..\MODEL\OA.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBLL { <# // Emit Entity Types foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs"); //BeginNamespace(namespaceName, code); #> public partial interface I<#=entity.Name#>BLL : IBaseBLL<Model.<#=entity.Name#>> { } <#}#> }
固然,你也能夠使用NuGet安裝tangibleT4EditorPlusModellingToolsVS2012,安裝完成後,按照以下的方式添加T4模板學習
按Ctrl+S自動生成代碼,自動生成的IBLL.cs以下:優化
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBLL { public partial interface IBill_LeaveBLL : IBaseBLL<Model.Bill_Leave> { } public partial interface IoldWF_AutoTransactNodeBLL : IBaseBLL<Model.oldWF_AutoTransactNode> { } public partial interface IoldWF_BillFlowNodeBLL : IBaseBLL<Model.oldWF_BillFlowNode> { } public partial interface IoldWF_BillFlowNodeRemarkBLL : IBaseBLL<Model.oldWF_BillFlowNodeRemark> { } public partial interface IoldWF_BillStateBLL : IBaseBLL<Model.oldWF_BillState> { } public partial interface IoldWF_NodeBLL : IBaseBLL<Model.oldWF_Node> { } public partial interface IoldWF_NodeStateBLL : IBaseBLL<Model.oldWF_NodeState> { } public partial interface IoldWF_WorkFlowBLL : IBaseBLL<Model.oldWF_WorkFlow> { } public partial interface IoldWF_WorkFlowNodeBLL : IBaseBLL<Model.oldWF_WorkFlowNode> { } public partial interface IOu_DepartmentBLL : IBaseBLL<Model.Ou_Department> { } public partial interface IOu_PermissionBLL : IBaseBLL<Model.Ou_Permission> { } public partial interface IOu_RoleBLL : IBaseBLL<Model.Ou_Role> { } public partial interface IOu_RolePermissionBLL : IBaseBLL<Model.Ou_RolePermission> { } public partial interface IOu_UserInfoBLL : IBaseBLL<Model.Ou_UserInfo> { } public partial interface IOu_UserRoleBLL : IBaseBLL<Model.Ou_UserRole> { } public partial interface IOu_UserVipPermissionBLL : IBaseBLL<Model.Ou_UserVipPermission> { } public partial interface IW_WorkFlowBLL : IBaseBLL<Model.W_WorkFlow> { } public partial interface IW_WorkFlowBranchBLL : IBaseBLL<Model.W_WorkFlowBranch> { } public partial interface IW_WorkFlowNodeBLL : IBaseBLL<Model.W_WorkFlowNode> { } public partial interface IW_WrokFlowRoleBLL : IBaseBLL<Model.W_WrokFlowRole> { } public partial interface IWR_WorkFlowApplyBLL : IBaseBLL<Model.WR_WorkFlowApply> { } public partial interface IWR_WrokFlowApplyDetailsBLL : IBaseBLL<Model.WR_WrokFlowApplyDetails> { } }
四、把IBLL.tt拷貝到IDAL項目下,而後修改命名空間爲IDAL,修改接口聲明模板this
namespace IDAL public partial interface I<#=entity.Name#>DAL : IBaseDAL<Model.<#=entity.Name#>>
按Ctrl+S自動生成代碼,自動生成的IDAL.cs以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IDAL { public partial interface IBill_LeaveDAL : IBaseDAL<Model.Bill_Leave> { } public partial interface IoldWF_AutoTransactNodeDAL : IBaseDAL<Model.oldWF_AutoTransactNode> { } public partial interface IoldWF_BillFlowNodeDAL : IBaseDAL<Model.oldWF_BillFlowNode> { } public partial interface IoldWF_BillFlowNodeRemarkDAL : IBaseDAL<Model.oldWF_BillFlowNodeRemark> { } public partial interface IoldWF_BillStateDAL : IBaseDAL<Model.oldWF_BillState> { } public partial interface IoldWF_NodeDAL : IBaseDAL<Model.oldWF_Node> { } public partial interface IoldWF_NodeStateDAL : IBaseDAL<Model.oldWF_NodeState> { } public partial interface IoldWF_WorkFlowDAL : IBaseDAL<Model.oldWF_WorkFlow> { } public partial interface IoldWF_WorkFlowNodeDAL : IBaseDAL<Model.oldWF_WorkFlowNode> { } public partial interface IOu_DepartmentDAL : IBaseDAL<Model.Ou_Department> { } public partial interface IOu_PermissionDAL : IBaseDAL<Model.Ou_Permission> { } public partial interface IOu_RoleDAL : IBaseDAL<Model.Ou_Role> { } public partial interface IOu_RolePermissionDAL : IBaseDAL<Model.Ou_RolePermission> { } public partial interface IOu_UserInfoDAL : IBaseDAL<Model.Ou_UserInfo> { } public partial interface IOu_UserRoleDAL : IBaseDAL<Model.Ou_UserRole> { } public partial interface IOu_UserVipPermissionDAL : IBaseDAL<Model.Ou_UserVipPermission> { } public partial interface IW_WorkFlowDAL : IBaseDAL<Model.W_WorkFlow> { } public partial interface IW_WorkFlowBranchDAL : IBaseDAL<Model.W_WorkFlowBranch> { } public partial interface IW_WorkFlowNodeDAL : IBaseDAL<Model.W_WorkFlowNode> { } public partial interface IW_WrokFlowRoleDAL : IBaseDAL<Model.W_WrokFlowRole> { } public partial interface IWR_WorkFlowApplyDAL : IBaseDAL<Model.WR_WorkFlowApply> { } public partial interface IWR_WrokFlowApplyDetailsDAL : IBaseDAL<Model.WR_WrokFlowApplyDetails> { } }
注意,我這裏生成的全部接口都是partial類型的,這樣我之後能夠很方便的進行擴展
關於T4模板的使用不在本篇所講的範疇,有興趣的朋友,能夠查找相關資料學習,我這裏只作簡要說明。
<#@ include file="EF.Utility.CS.ttinclude"#>:引入微軟的生成框架
<#@ output extension=".cs"#>:指定生成的文件類型爲.cs文件
string inputFile = @"..\MODEL\OA.edmx":這裏使用相對路徑指向咱們新建的ADO.NET實體數據模型文件。
五、添加數據層的父類BaseDAL.cs
前面數據層接口和業務層接口都已經添加好了,那麼如今來添加數據層類和業務層類
在DAL項目中添加EntityFramework.dll和System.Data.Entity.dll的引用
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using Model; namespace DAL { /// <summary> /// 數據層 父類 /// </summary> /// <typeparam name="T"></typeparam> public class BaseDAL<T> : IDAL.IBaseDAL<T> where T : class,new() { /// <summary> /// EF上下文對象 /// </summary> DbContext db = new OAEntities(); //new DBContextFactory().GetDbContext();// #region 1.0 新增 實體 +int Add(T model) /// <summary> /// 新增 實體 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Add(T model) { db.Set<T>().Add(model); return db.SaveChanges();//保存成功後,會將自增的id設置給 model的 主鍵屬性,並返回受影響行數 } #endregion #region 2.0 根據 id 刪除 +int Del(T model) /// <summary> /// 根據 id 刪除 /// </summary> /// <param name="model">包含要刪除id的對象</param> /// <returns></returns> public int Del(T model) { db.Set<T>().Attach(model); db.Set<T>().Remove(model); return db.SaveChanges(); } #endregion #region 3.0 根據條件刪除 +int DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 3.0 根據條件刪除 /// </summary> /// <param name="delWhere"></param> /// <returns></returns> public int DelBy(Expression<Func<T, bool>> delWhere) { //3.1查詢要刪除的數據 List<T> listDeleting = db.Set<T>().Where(delWhere).ToList(); //3.2將要刪除的數據 用刪除方法添加到 EF 容器中 listDeleting.ForEach(u => { db.Set<T>().Attach(u);//先附加到 EF容器 db.Set<T>().Remove(u);//標識爲 刪除 狀態 }); //3.3一次性 生成sql語句到數據庫執行刪除 return db.SaveChanges(); } #endregion #region 4.0 修改 +int Modify(T model, params string[] proNames) /// <summary> /// 4.0 修改,如: /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; /// this.Modify(u, "uLoginName"); /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> public int Modify(T model, params string[] proNames) { //4.1將 對象 添加到 EF中 DbEntityEntry entry = db.Entry<T>(model); //4.2先設置 對象的包裝 狀態爲 Unchanged entry.State = System.Data.EntityState.Unchanged; //4.3循環 被修改的屬性名 數組 foreach (string proName in proNames) { //4.4將每一個 被修改的屬性的狀態 設置爲已修改狀態;後面生成update語句時,就只爲已修改的屬性 更新 entry.Property(proName).IsModified = true; } //4.4一次性 生成sql語句到數據庫執行 return db.SaveChanges(); } #endregion #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// <summary> /// 4.0 批量修改 /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="whereLambda">查詢條件</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) { //4.1查詢要修改的數據 List<T> listModifing = db.Set<T>().Where(whereLambda).ToList(); //獲取 實體類 類型對象 Type t = typeof(T); // model.GetType(); //獲取 實體類 全部的 公有屬性 List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); //建立 實體屬性 字典集合 Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>(); //將 實體屬性 中要修改的屬性名 添加到 字典集合中 鍵:屬性名 值:屬性對象 proInfos.ForEach(p => { if (modifiedProNames.Contains(p.Name)) { dictPros.Add(p.Name, p); } }); //4.3循環 要修改的屬性名 foreach (string proName in modifiedProNames) { //判斷 要修改的屬性名是否在 實體類的屬性集合中存在 if (dictPros.ContainsKey(proName)) { //若是存在,則取出要修改的 屬性對象 PropertyInfo proInfo = dictPros[proName]; //取出 要修改的值 object newValue = proInfo.GetValue(model, null); //object newValue = model.uName; //4.4批量設置 要修改 對象的 屬性 foreach (T usrO in listModifing) { //爲 要修改的對象 的 要修改的屬性 設置新的值 proInfo.SetValue(usrO, newValue, null); //usrO.uName = newValue; } } } //4.4一次性 生成sql語句到數據庫執行 return db.SaveChanges(); } #endregion #region 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// <summary> /// 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public List<T> GetListBy(Expression<Func<T, bool>> whereLambda) { return db.Set<T>().Where(whereLambda).ToList(); } #endregion #region 5.1 根據條件 排序 和查詢 + List<T> GetListBy<TKey> /// <summary> /// 5.1 根據條件 排序 和查詢 /// </summary> /// <typeparam name="TKey">排序字段類型</typeparam> /// <param name="whereLambda">查詢條件 lambda表達式</param> /// <param name="orderLambda">排序條件 lambda表達式</param> /// <returns></returns> public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda) { return db.Set<T>().Where(whereLambda).OrderBy(orderLambda).ToList(); } #endregion #region 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">頁碼</param> /// <param name="pageSize">頁容量</param> /// <param name="whereLambda">條件 lambda表達式</param> /// <param name="orderBy">排序 lambda表達式</param> /// <returns></returns> public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy) { // 分頁 必定注意: Skip 以前必定要 OrderBy return db.Set<T>().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } #endregion } }
六、拷貝IDAL.tt到DAL項目中,更名爲DAL.tt,而後修改T4模板
按Ctrl+S自動生成代碼,自動生成的DAL.cs以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using IDAL; namespace DAL { public partial class Bill_LeaveDAL : BaseDAL<Model.Bill_Leave>,IBill_LeaveDAL { } public partial class oldWF_AutoTransactNodeDAL : BaseDAL<Model.oldWF_AutoTransactNode>,IoldWF_AutoTransactNodeDAL { } public partial class oldWF_BillFlowNodeDAL : BaseDAL<Model.oldWF_BillFlowNode>,IoldWF_BillFlowNodeDAL { } public partial class oldWF_BillFlowNodeRemarkDAL : BaseDAL<Model.oldWF_BillFlowNodeRemark>,IoldWF_BillFlowNodeRemarkDAL { } public partial class oldWF_BillStateDAL : BaseDAL<Model.oldWF_BillState>,IoldWF_BillStateDAL { } public partial class oldWF_NodeDAL : BaseDAL<Model.oldWF_Node>,IoldWF_NodeDAL { } public partial class oldWF_NodeStateDAL : BaseDAL<Model.oldWF_NodeState>,IoldWF_NodeStateDAL { } public partial class oldWF_WorkFlowDAL : BaseDAL<Model.oldWF_WorkFlow>,IoldWF_WorkFlowDAL { } public partial class oldWF_WorkFlowNodeDAL : BaseDAL<Model.oldWF_WorkFlowNode>,IoldWF_WorkFlowNodeDAL { } public partial class Ou_DepartmentDAL : BaseDAL<Model.Ou_Department>,IOu_DepartmentDAL { } public partial class Ou_PermissionDAL : BaseDAL<Model.Ou_Permission>,IOu_PermissionDAL { } public partial class Ou_RoleDAL : BaseDAL<Model.Ou_Role>,IOu_RoleDAL { } public partial class Ou_RolePermissionDAL : BaseDAL<Model.Ou_RolePermission>,IOu_RolePermissionDAL { } public partial class Ou_UserInfoDAL : BaseDAL<Model.Ou_UserInfo>,IOu_UserInfoDAL { } public partial class Ou_UserRoleDAL : BaseDAL<Model.Ou_UserRole>,IOu_UserRoleDAL { } public partial class Ou_UserVipPermissionDAL : BaseDAL<Model.Ou_UserVipPermission>,IOu_UserVipPermissionDAL { } public partial class W_WorkFlowDAL : BaseDAL<Model.W_WorkFlow>,IW_WorkFlowDAL { } public partial class W_WorkFlowBranchDAL : BaseDAL<Model.W_WorkFlowBranch>,IW_WorkFlowBranchDAL { } public partial class W_WorkFlowNodeDAL : BaseDAL<Model.W_WorkFlowNode>,IW_WorkFlowNodeDAL { } public partial class W_WrokFlowRoleDAL : BaseDAL<Model.W_WrokFlowRole>,IW_WrokFlowRoleDAL { } public partial class WR_WorkFlowApplyDAL : BaseDAL<Model.WR_WorkFlowApply>,IWR_WorkFlowApplyDAL { } public partial class WR_WrokFlowApplyDetailsDAL : BaseDAL<Model.WR_WrokFlowApplyDetails>,IWR_WrokFlowApplyDetailsDAL { } }
七、BLL項目中添加業務層的父類BaseBLL.cs,在業務父類中只能調用數據庫父類接口,因此能夠經過一個抽象方法,讓子類來實現數據庫對象的實例化操做。
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Reflection; namespace BLL { /// <summary> /// 業務層父類 /// </summary> /// <typeparam name="T"></typeparam> public abstract class BaseBLL<T> : IBLL.IBaseBLL<T> where T : class,new() { public BaseBLL() { SetDAL(); } //1.數據層接口 對象 - 等待 被實例化 protected IDAL.IBaseDAL<T> idal;// = new idal.BaseDAL(); /// <summary> /// 由子類實現,爲 業務父類 裏的 數據接口對象 設置 值! /// </summary> public abstract void SetDAL(); //2.增刪改查方法 #region 1.0 新增 實體 +int Add(T model) /// <summary> /// 新增 實體 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Add(T model) { return idal.Add(model); } #endregion #region 2。0 根據 用戶 id 刪除 +int Del(int uId) /// <summary> /// 根據 用戶 id 刪除 /// </summary> /// <param name="uId"></param> /// <returns></returns> public int Del(T model) { return idal.Del(model); } #endregion #region 3.0 根據條件刪除 +int DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 3.0 根據條件刪除 /// </summary> /// <param name="delWhere"></param> /// <returns></returns> public int DelBy(Expression<Func<T, bool>> delWhere) { return idal.DelBy(delWhere); } #endregion #region 4.0 修改 +int Modify(T model, params string[] proNames) /// <summary> /// 4.0 修改,如: /// </summary> /// <param name="model">要修改的實體對象</param> /// <param name="proNames">要修改的 屬性 名稱</param> /// <returns></returns> public int Modify(T model, params string[] proNames) { return idal.Modify(model, proNames); } #endregion #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// <summary> /// 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// </summary> /// <param name="model"></param> /// <param name="whereLambda"></param> /// <param name="modifiedProNames"></param> /// <returns></returns> public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) { return idal.ModifyBy(model, whereLambda, modifiedProNames); } #endregion #region 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// <summary> /// 5.0 根據條件查詢 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public List<T> GetListBy(Expression<Func<T, bool>> whereLambda) { return idal.GetListBy(whereLambda); } #endregion #region 5.1 根據條件 排序 和查詢 + List<T> GetListBy<TKey> /// <summary> /// 5.1 根據條件 排序 和查詢 /// </summary> /// <typeparam name="TKey">排序字段類型</typeparam> /// <param name="whereLambda">查詢條件 lambda表達式</param> /// <param name="orderLambda">排序條件 lambda表達式</param> /// <returns></returns> public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda) { return idal.GetListBy(whereLambda, orderLambda); } #endregion #region 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分頁查詢 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">頁碼</param> /// <param name="pageSize">頁容量</param> /// <param name="whereLambda">條件 lambda表達式</param> /// <param name="orderBy">排序 lambda表達式</param> /// <returns></returns> public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy) { return idal.GetPagedList(pageIndex, pageSize, whereLambda, orderBy); } #endregion } }
八、一樣的拷貝IBLL.tt到BLL項目下,重命名爲BLL.tt,而後修改T4模板
public override void SetDAL() { idal = DBSession.I<#=entity.Name#>DAL; }
按Ctrl+S自動生成代碼,自動生成的BLL.cs以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using IBLL; namespace BLL { public partial class Bill_LeaveBLL : BaseBLL<Model.Bill_Leave>,IBill_LeaveBLL { public override void SetDAL() { idal= DBSession.IBill_LeaveDAL; } } public partial class oldWF_AutoTransactNodeBLL : BaseBLL<Model.oldWF_AutoTransactNode>,IoldWF_AutoTransactNodeBLL { public override void SetDAL() { idal= DBSession.IoldWF_AutoTransactNodeDAL; } } public partial class oldWF_BillFlowNodeBLL : BaseBLL<Model.oldWF_BillFlowNode>,IoldWF_BillFlowNodeBLL { public override void SetDAL() { idal= DBSession.IoldWF_BillFlowNodeDAL; } } public partial class oldWF_BillFlowNodeRemarkBLL : BaseBLL<Model.oldWF_BillFlowNodeRemark>,IoldWF_BillFlowNodeRemarkBLL { public override void SetDAL() { idal= DBSession.IoldWF_BillFlowNodeRemarkDAL; } } public partial class oldWF_BillStateBLL : BaseBLL<Model.oldWF_BillState>,IoldWF_BillStateBLL { public override void SetDAL() { idal= DBSession.IoldWF_BillStateDAL; } } public partial class oldWF_NodeBLL : BaseBLL<Model.oldWF_Node>,IoldWF_NodeBLL { public override void SetDAL() { idal= DBSession.IoldWF_NodeDAL; } } public partial class oldWF_NodeStateBLL : BaseBLL<Model.oldWF_NodeState>,IoldWF_NodeStateBLL { public override void SetDAL() { idal= DBSession.IoldWF_NodeStateDAL; } } public partial class oldWF_WorkFlowBLL : BaseBLL<Model.oldWF_WorkFlow>,IoldWF_WorkFlowBLL { public override void SetDAL() { idal= DBSession.IoldWF_WorkFlowDAL; } } public partial class oldWF_WorkFlowNodeBLL : BaseBLL<Model.oldWF_WorkFlowNode>,IoldWF_WorkFlowNodeBLL { public override void SetDAL() { idal= DBSession.IoldWF_WorkFlowNodeDAL; } } public partial class Ou_DepartmentBLL : BaseBLL<Model.Ou_Department>,IOu_DepartmentBLL { public override void SetDAL() { idal= DBSession.IOu_DepartmentDAL; } } public partial class Ou_PermissionBLL : BaseBLL<Model.Ou_Permission>,IOu_PermissionBLL { public override void SetDAL() { idal= DBSession.IOu_PermissionDAL; } } public partial class Ou_RoleBLL : BaseBLL<Model.Ou_Role>,IOu_RoleBLL { public override void SetDAL() { idal= DBSession.IOu_RoleDAL; } } public partial class Ou_RolePermissionBLL : BaseBLL<Model.Ou_RolePermission>,IOu_RolePermissionBLL { public override void SetDAL() { idal= DBSession.IOu_RolePermissionDAL; } } public partial class Ou_UserInfoBLL : BaseBLL<Model.Ou_UserInfo>,IOu_UserInfoBLL { public override void SetDAL() { idal= DBSession.IOu_UserInfoDAL; } } public partial class Ou_UserRoleBLL : BaseBLL<Model.Ou_UserRole>,IOu_UserRoleBLL { public override void SetDAL() { idal= DBSession.IOu_UserRoleDAL; } } public partial class Ou_UserVipPermissionBLL : BaseBLL<Model.Ou_UserVipPermission>,IOu_UserVipPermissionBLL { public override void SetDAL() { idal= DBSession.IOu_UserVipPermissionDAL; } } public partial class W_WorkFlowBLL : BaseBLL<Model.W_WorkFlow>,IW_WorkFlowBLL { public override void SetDAL() { idal= DBSession.IW_WorkFlowDAL; } } public partial class W_WorkFlowBranchBLL : BaseBLL<Model.W_WorkFlowBranch>,IW_WorkFlowBranchBLL { public override void SetDAL() { idal= DBSession.IW_WorkFlowBranchDAL; } } public partial class W_WorkFlowNodeBLL : BaseBLL<Model.W_WorkFlowNode>,IW_WorkFlowNodeBLL { public override void SetDAL() { idal= DBSession.IW_WorkFlowNodeDAL; } } public partial class W_WrokFlowRoleBLL : BaseBLL<Model.W_WrokFlowRole>,IW_WrokFlowRoleBLL { public override void SetDAL() { idal= DBSession.IW_WrokFlowRoleDAL; } } public partial class WR_WorkFlowApplyBLL : BaseBLL<Model.WR_WorkFlowApply>,IWR_WorkFlowApplyBLL { public override void SetDAL() { idal= DBSession.IWR_WorkFlowApplyDAL; } } public partial class WR_WrokFlowApplyDetailsBLL : BaseBLL<Model.WR_WrokFlowApplyDetails>,IWR_WrokFlowApplyDetailsBLL { public override void SetDAL() { idal= DBSession.IWR_WrokFlowApplyDetailsDAL; } } }
到此,業務父類、業務類、數據父類、數據類都已經建立好了,裏面有些東西還須要優化的,後續一步一步完善。