业务层和数据层父类及接口-T4模板
在上一篇中,我们已经把项目的基本框架搭起来了,这一篇我们就来实现业务层和数据层的父接口及父类。
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
}
}
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
}
}
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 } }
3、我们数据库中有许多的表,通常每一张表都对应一个业务实体,如果我们每一个业务实体类都自己去手写的话,太浪费时间了,我们可以使用T4模板,简化这些枯燥的操作,提高我们的开发效率。
新建一个文本模板
然后把以下代码复制上去,
<#@ 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 = @"..MODELOA.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#>>
{
}
<#}#>
}
<#@ 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 = @"..MODELOA.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#>> { } <#}#> }
按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>
{
}
}
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> { } }
4、把IBLL.tt拷贝到IDAL项目下,然后修改命名空间为IDAL,修改接口声明模板
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>
{
}
}
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 = @"..MODELOA.edmx":这里使用相对路径指向我们新建的ADO.NET实体数据模型文件。
5、添加数据层的父类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
}
}
6、拷贝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
{
}
}
7、添加业务层的父类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
}
}
8、同样的拷贝IBLL.tt到BLL项目下,重命名为BLL.tt,然后修改T4模板,按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 partial class oldWF_AutoTransactNodeBLL : BaseBLL<Model.oldWF_AutoTransactNode>,IoldWF_AutoTransactNodeBLL
{
}
public partial class oldWF_BillFlowNodeBLL : BaseBLL<Model.oldWF_BillFlowNode>,IoldWF_BillFlowNodeBLL
{
}
public partial class oldWF_BillFlowNodeRemarkBLL : BaseBLL<Model.oldWF_BillFlowNodeRemark>,IoldWF_BillFlowNodeRemarkBLL
{
}
public partial class oldWF_BillStateBLL : BaseBLL<Model.oldWF_BillState>,IoldWF_BillStateBLL
{
}
public partial class oldWF_NodeBLL : BaseBLL<Model.oldWF_Node>,IoldWF_NodeBLL
{
}
public partial class oldWF_NodeStateBLL : BaseBLL<Model.oldWF_NodeState>,IoldWF_NodeStateBLL
{
}
public partial class oldWF_WorkFlowBLL : BaseBLL<Model.oldWF_WorkFlow>,IoldWF_WorkFlowBLL
{
}
public partial class oldWF_WorkFlowNodeBLL : BaseBLL<Model.oldWF_WorkFlowNode>,IoldWF_WorkFlowNodeBLL
{
}
public partial class Ou_DepartmentBLL : BaseBLL<Model.Ou_Department>,IOu_DepartmentBLL
{
}
public partial class Ou_PermissionBLL : BaseBLL<Model.Ou_Permission>,IOu_PermissionBLL
{
}
public partial class Ou_RoleBLL : BaseBLL<Model.Ou_Role>,IOu_RoleBLL
{
}
public partial class Ou_RolePermissionBLL : BaseBLL<Model.Ou_RolePermission>,IOu_RolePermissionBLL
{
}
public partial class Ou_UserInfoBLL : BaseBLL<Model.Ou_UserInfo>,IOu_UserInfoBLL
{
}
public partial class Ou_UserRoleBLL : BaseBLL<Model.Ou_UserRole>,IOu_UserRoleBLL
{
}
public partial class Ou_UserVipPermissionBLL : BaseBLL<Model.Ou_UserVipPermission>,IOu_UserVipPermissionBLL
{
}
public partial class W_WorkFlowBLL : BaseBLL<Model.W_WorkFlow>,IW_WorkFlowBLL
{
}
public partial class W_WorkFlowBranchBLL : BaseBLL<Model.W_WorkFlowBranch>,IW_WorkFlowBranchBLL
{
}
public partial class W_WorkFlowNodeBLL : BaseBLL<Model.W_WorkFlowNode>,IW_WorkFlowNodeBLL
{
}
public partial class W_WrokFlowRoleBLL : BaseBLL<Model.W_WrokFlowRole>,IW_WrokFlowRoleBLL
{
}
public partial class WR_WorkFlowApplyBLL : BaseBLL<Model.WR_WorkFlowApply>,IWR_WorkFlowApplyBLL
{
}
public partial class WR_WrokFlowApplyDetailsBLL : BaseBLL<Model.WR_WrokFlowApplyDetails>,IWR_WrokFlowApplyDetailsBLL
{
}
}
到此,业务父类、业务类、数据父类、数据类都已经创建好了,里面有些东西还需要优化的,后续一步一步完善。
如果VS2012中没有T4模板和智能提示,可以使用NuGet安装tangibleT4EditorPlusModellingToolsVS2012