首先附上本人托管在Github上的项目https://github.com/lichaojacobs/Tomato
线上地址:www.wanhb.cn
接下来的三层结构都包含了父接口,子接口,父类,子类部分
1、编写数据访问层
数据访问层是对数据库的增删改查的操作。首先我们在数据访问层的父接口声明一些通用的对数据库统一处理的方法:
1 #region 1.0 新增 实体 +int Add(T model) 2 ///3 /// 新增 实体 4 /// 5 /// 6 /// 7 int Add(T model); 8 #endregion 9 10 #region 2.0 根据 id 删除 +int Del(T model) 11 /// 12 /// 根据 id 删除 13 /// 14 /// 包含要删除id的对象 15 /// 16 int Del(T model); 17 #endregion 18 19 #region 3.0 根据条件删除 +int DelBy(Expression > delWhere) 20 /// 21 /// 3.0 根据条件删除 22 /// 23 /// 24 /// 25 int DelBy(Expression bool>> delWhere); 26 #endregion 27 28 #region 4.0 修改 +int Modify(T model, params string[] proNames) 29 /// 30 /// 4.0 修改,如: 31 /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; 32 /// this.Modify(u, "uLoginName"); 33 /// 34 /// 要修改的实体对象 35 /// 要修改的 属性 名称 36 /// 37 int Modify(T model, params string[] proNames); 38 #endregion 39 40 #region 4.0 批量修改 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames) 41 /// 42 /// 4.0 批量修改 43 /// 44 /// 要修改的实体对象 45 /// 查询条件 46 /// 要修改的 属性 名称 47 /// 48 int ModifyBy(T model, Expression bool>> whereLambda, params string[] modifiedProNames); 49 #endregion 50 51 #region 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 52 /// 53 /// 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 54 /// 55 /// 56 /// 57 List GetListBy(Expression bool>> whereLambda); 58 #endregion 59 60 #region 5.1 根据条件 排序 和查询 + List GetListBy 61 /// 62 /// 5.1 根据条件 排序 和查询 63 /// 64 /// 排序字段类型 65 /// 查询条件 lambda表达式 66 /// 排序条件 lambda表达式 67 /// 68 List GetListBy (Expression bool>> whereLambda, Expression > orderLambda); 69 #endregion 70 71 #region 6.0 分页查询 + List GetPagedList 72 /// 73 /// 6.0 分页查询 + List GetPagedList 74 /// 75 /// 页码 76 /// 页容量 77 /// 条件 lambda表达式 78 /// 排序 lambda表达式 79 /// 80 List GetPagedList (int pageIndex, int pageSize, Expression bool>> whereLambda, Expression > orderBy); 81 #endregion
然后是数据访问层子接口(对应于你的数据实体模型中的Model,下面是我的项目中的Model,仅供参考),让它继承自业务层父接口
1 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 8 namespace IDAL 9 { 10 public partial interface IsysdiagramsDAL : IBaseDAL11 { 12 13 14 } 15 16 public partial interface IT001账号表DAL : IBaseDAL 17 { 18 19 20 } 21 22 public partial interface IT002验证表DAL : IBaseDAL 23 { 24 25 26 } 27 28 public partial interface IT003用户角色表DAL : IBaseDAL 29 { 30 31 32 } 33 34 public partial interface IT004社团信息表DAL : IBaseDAL 35 { 36 37 38 } 39 40 public partial interface IT005票务表DAL : IBaseDAL 41 { 42 43 44 } 45 46 public partial interface IT006店铺信息表DAL : IBaseDAL 47 { 48 49 50 } 51 52 public partial interface IT007店铺货物表DAL : IBaseDAL 53 { 54 55 56 } 57 58 public partial interface IT008海报信息表DAL : IBaseDAL 59 { 60 61 62 } 63 64 public partial interface IT009社团账号表DAL : IBaseDAL 65 { 66 67 68 } 69 70 71 }
写完接口,接下来编写父接口的实现父类BaseDAL.cs,在实现类中获得一个数据上下文类用来操作数据库
1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity.Infrastructure; 4 using System.Linq; 5 using System.Linq.Expressions; 6 using System.Reflection; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Data.Entity; 10 11 namespace DAL 12 { 13 public class BaseDAL:IDAL.IBaseDAL where T:class,new() 14 { 15 //1,定义上下文对象 16 // Model.TomatoEntities db = new Model.TomatoEntities(); 17 protected DbContext db = new DBContextFactory().GetDbContext(); 18 19 //2,定义增删改查的操作 20 #region 1.0 新增 实体 +int Add(T model) 21 /// 22 /// 新增 实体 23 /// 24 /// 25 /// 26 public int Add(T model) 27 { 28 db.Set ().Add(model); 29 return db.SaveChanges();//保存成功后,会将自增的id设置给 model的 主键属性,并返回受影响行数 30 } 31 #endregion 32 33 #region 2.0 根据 id 删除 +int Del(T model) 34 /// 35 /// 根据 id 删除 36 /// 37 /// 包含要删除id的对象 38 /// 39 public int Del(T model) 40 { 41 db.Set ().Attach(model); 42 db.Set ().Remove(model); 43 return db.SaveChanges(); 44 } 45 #endregion 46 47 #region 3.0 根据条件删除 +int DelBy(Expression > delWhere) 48 /// 49 /// 3.0 根据条件删除 50 /// 51 /// 52 /// 53 public int DelBy(Expression bool>> delWhere) 54 { 55 //3.1查询要删除的数据 56 List listDeleting = db.Set ().Where(delWhere).ToList(); 57 //3.2将要删除的数据 用删除方法添加到 EF 容器中 58 listDeleting.ForEach(u => 59 { 60 db.Set ().Attach(u);//先附加到 EF容器 61 db.Set ().Remove(u);//标识为 删除 状态 62 }); 63 //3.3一次性 生成sql语句到数据库执行删除 64 return db.SaveChanges(); 65 } 66 #endregion 67 68 #region 4.0 修改 +int Modify(T model, params string[] proNames) 69 /// 70 /// 4.0 修改,如: 71 /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; 72 /// this.Modify(u, "uLoginName"); 73 /// 74 /// 要修改的实体对象 75 /// 要修改的 属性 名称 76 /// 77 public int Modify(T model,params string[] proNames) 78 { 79 //int idreal = (int)id; 80 //var ent = db.Set ().Find(idreal); 81 //if (ent != null) 82 //{ 83 // db.Entry(model).State = System.Data.EntityState.Detached; 84 //} 85 //4.1将 对象 添加到 EF中 86 DbEntityEntry entry = db.Entry(model); 87 88 ///跟踪对象 89 db.Set ().Attach(model); 90 //4.2先设置 对象的包装 状态为 Unchanged 91 entry.State = System.Data.EntityState.Unchanged; 92 //4.3循环 被修改的属性名 数组 93 foreach (string proName in proNames) 94 { 95 //4.4将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新 96 entry.Property(proName).IsModified = true; 97 } 98 //4.4一次性 生成sql语句到数据库执行 99 return db.SaveChanges(); 100 } 101 #endregion 102 103 #region 4.0 批量修改 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames) 104 /// 105 /// 4.0 批量修改 106 /// 107 /// 要修改的实体对象 108 /// 查询条件 109 /// 要修改的 属性 名称 110 /// 111 public int ModifyBy(T model, Expression bool>> whereLambda, params string[] modifiedProNames) 112 { 113 //4.1查询要修改的数据 114 List listModifing = db.Set ().Where(whereLambda).ToList(); 115 116 //获取 实体类 类型对象 117 Type t = typeof(T); // model.GetType(); 118 //获取 实体类 所有的 公有属性 119 List proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); 120 //创建 实体属性 字典集合 121 Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>(); 122 //将 实体属性 中要修改的属性名 添加到 字典集合中 键:属性名 值:属性对象 123 proInfos.ForEach(p => 124 { 125 if (modifiedProNames.Contains(p.Name)) 126 { 127 dictPros.Add(p.Name, p); 128 } 129 }); 130 131 //4.3循环 要修改的属性名 132 foreach (string proName in modifiedProNames) 133 { 134 //判断 要修改的属性名是否在 实体类的属性集合中存在 135 if (dictPros.ContainsKey(proName)) 136 { 137 //如果存在,则取出要修改的 属性对象 138 PropertyInfo proInfo = dictPros[proName]; 139 //取出 要修改的值 140 object newValue = proInfo.GetValue(model, null); //object newValue = model.uName; 141 142 //4.4批量设置 要修改 对象的 属性 143 foreach (T usrO in listModifing) 144 { 145 //为 要修改的对象 的 要修改的属性 设置新的值 146 proInfo.SetValue(usrO, newValue, null); //usrO.uName = newValue; 147 } 148 } 149 } 150 //4.4一次性 生成sql语句到数据库执行 151 return db.SaveChanges(); 152 } 153 #endregion 154 155 #region 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 156 /// 157 /// 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 158 /// 159 /// 160 /// 161 public List GetListBy(Expression bool>> whereLambda) 162 { 163 return db.Set ().Where(whereLambda).ToList(); 164 } 165 #endregion 166 167 #region 5.1 根据条件 排序 和查询 + List GetListBy 168 /// 169 /// 5.1 根据条件 排序 和查询 170 /// 171 /// 排序字段类型 172 /// 查询条件 lambda表达式 173 /// 排序条件 lambda表达式 174 /// 175 public List GetListBy (Expression bool>> whereLambda, Expression > orderLambda) 176 { 177 return db.Set ().Where(whereLambda).OrderBy(orderLambda).ToList(); 178 } 179 #endregion 180 181 #region 6.0 分页查询 + List GetPagedList 182 /// 183 /// 6.0 分页查询 + List GetPagedList 184 /// 185 /// 页码 186 /// 页容量 187 /// 条件 lambda表达式 188 /// 排序 lambda表达式 189 /// 190 public List GetPagedList (int pageIndex, int pageSize, Expression bool>> whereLambda, Expression > orderBy) 191 { 192 // 分页 一定注意: Skip 之前一定要 OrderBy 193 return db.Set ().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 194 } 195 #endregion 196 197 } 198 }
注意,在这里是通过一个工具类(DBContextFactory)获得了数据实体的上下文对象,通过这个工具类可以在一个线程中共享上下文对象,从而节省系统资源开销也可以缓解数据库的压力
1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity; 4 using System.Linq; 5 using System.Runtime.Remoting.Messaging; 6 using System.Text; 7 8 namespace DAL 9 { 10 public class DBContextFactory:IDAL.IDBContextFactory 11 { 12 #region 创建 EF上下文 对象,在线程中共享 一个 上下文对象 + DbContext GetDbContext() 13 ///14 /// 创建 EF上下文 对象,在线程中共享 一个 上下文对象 15 /// 16 /// 17 public DbContext GetDbContext() 18 { 19 //从当前线程中 获取 EF上下文对象 20 DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext; 21 if (dbContext == null) 22 { 23 dbContext = new Model.TomatoEntities(); 24 //将新创建的 ef上下文对象 存入线程 25 CallContext.SetData(typeof(DBContextFactory).Name, dbContext); 26 } 27 return dbContext; 28 } 29 #endregion 30 } 31 }
2、搭建业务逻辑层
这个过程与数据访问层类似(业务层调用数据访问层相关方法),不多说了,直接上代码
业务父接口(方法的声明与数据访问层父接口相同):
直接参考上文的数据访问层的父接口的方法声明
业务子接口(继承业务父接口,实现扩展):
1 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 8 namespace IBLL 9 { 10 public partial interface IsysdiagramsBLL : IBaseBLL11 { 12 13 } 14 15 public partial interface IT001账号表BLL : IBaseBLL 16 { 17 18 } 19 20 public partial interface IT002验证表BLL : IBaseBLL 21 { 22 23 } 24 25 public partial interface IT003用户角色表BLL : IBaseBLL 26 { 27 28 } 29 30 public partial interface IT004社团信息表BLL : IBaseBLL 31 { 32 33 } 34 35 public partial interface IT005票务表BLL : IBaseBLL 36 { 37 38 } 39 40 public partial interface IT006店铺信息表BLL : IBaseBLL 41 { 42 43 } 44 45 public partial interface IT007店铺货物表BLL : IBaseBLL 46 { 47 48 } 49 50 public partial interface IT008海报信息表BLL : IBaseBLL 51 { 52 53 } 54 55 public partial interface IT009社团账号表BLL : IBaseBLL 56 { 57 58 } 59 60 61 }
实现父类以及子类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Linq.Expressions; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 9 namespace BLL 10 { 11 public abstract class BaseBLL: IBLL.IBaseBLL where T : class,new() 12 { 13 public BaseBLL() 14 { 15 SetDAL(); 16 } 17 18 19 20 //1,定义数据操作层父接口类 21 protected DAL.BaseDAL idal= new DAL.BaseDAL (); 22 /// 23 /// 由子类实现,为 业务父类 里的 数据接口对象 设置 值! 24 /// 25 public abstract void SetDAL(); 26 27 28 //2,定义增删改查的方法 29 //2.增删改查方法 30 #region 1.0 新增 实体 +int Add(T model) 31 /// 32 /// 新增 实体 33 /// 34 /// 35 /// 36 public int Add(T model) 37 { 38 return idal.Add(model); 39 } 40 #endregion 41 42 #region 2。0 根据 用户 id 删除 +int Del(int uId) 43 /// 44 /// 根据 用户 id 删除 45 /// 46 /// 47 /// 48 public int Del(T model) 49 { 50 return idal.Del(model); 51 } 52 #endregion 53 54 #region 3.0 根据条件删除 +int DelBy(Expression > delWhere) 55 /// 56 /// 3.0 根据条件删除 57 /// 58 /// 59 /// 60 public int DelBy(Expression bool>> delWhere) 61 { 62 return idal.DelBy(delWhere); 63 } 64 #endregion 65 66 #region 4.0 修改 +int Modify(T model, params string[] proNames) 67 /// 68 /// 4.0 修改,如: 69 /// 70 /// 要修改的实体对象 71 /// 要修改的 属性 名称 72 /// 73 public int Modify(T model, params string[] proNames) 74 { 75 return idal.Modify(model, proNames); 76 } 77 #endregion 78 79 #region 4.0 批量修改 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames) 80 /// 81 /// 4.0 批量修改 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames) 82 /// 83 /// 84 /// 85 /// 86 /// 87 public int ModifyBy(T model, Expression bool>> whereLambda, params string[] modifiedProNames) 88 { 89 return idal.ModifyBy(model, whereLambda, modifiedProNames); 90 } 91 #endregion 92 93 #region 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 94 /// 95 /// 5.0 根据条件查询 +List GetListBy(Expression > whereLambda) 96 /// 97 /// 98 /// 99 public List GetListBy(Expression bool>> whereLambda) 100 { 101 return idal.GetListBy(whereLambda); 102 } 103 #endregion 104 105 #region 5.1 根据条件 排序 和查询 + List GetListBy 106 /// 107 /// 5.1 根据条件 排序 和查询 108 /// 109 /// 排序字段类型 110 /// 查询条件 lambda表达式 111 /// 排序条件 lambda表达式 112 /// 113 public List GetListBy (Expression bool>> whereLambda, Expression > orderLambda) 114 { 115 return idal.GetListBy(whereLambda, orderLambda); 116 } 117 #endregion 118 119 #region 6.0 分页查询 + List GetPagedList 120 /// 121 /// 6.0 分页查询 + List GetPagedList 122 /// 123 /// 页码 124 /// 页容量 125 /// 条件 lambda表达式 126 /// 排序 lambda表达式 127 /// 128 public List GetPagedList (int pageIndex, int pageSize, Expression bool>> whereLambda, Expression > orderBy) 129 { 130 return idal.GetPagedList(pageIndex, pageSize, whereLambda, orderBy); 131 } 132 #endregion 133 134 } 135 }
子类的实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using IBLL; using IDAL; namespace BLL { public partial class sysdiagramsBLL: BaseBLL,IsysdiagramsBLL { public override void SetDAL() { } } public partial class T001账号表BLL: BaseBLL ,IT001账号表BLL { public override void SetDAL() { idal = DAL.BaseDAL (); } } public partial class T002验证表BLL: BaseBLL ,IT002验证表BLL { public override void SetDAL() { idal = DAL.BaseDAL (); } } public partial class T003用户角色表BLL: BaseBLL ,IT003用户角色表BLL { public override void SetDAL() { idal =DAL.BaseDAL (); } } public partial class T004社团信息表BLL: BaseBLL ,IT004社团信息表BLL { public override void SetDAL() { idal = DAL.BaseDAL ();; } } public partial class T005票务表BLL: BaseBLL ,IT005票务表BLL { public override void SetDAL() { idal = DAL.BaseDAL (); } } public partial class T006店铺信息表BLL: BaseBLL ,IT006店铺信息表BLL { public override void SetDAL() { idal = DAL.BaseDAL (); } } public partial class T007店铺货物表BLL: BaseBLL ,IT007店铺货物表BLL { public override void SetDAL() { idal =DAL.BaseDAL (); } } public partial class T008海报信息表BLL: BaseBLL ,IT008海报信息表BLL { public override void SetDAL() { idal =DAL.BaseDAL (); } } public partial class T009社团账号表BLL: BaseBLL ,IT009社团账号表BLL { public override void SetDAL() { idal = DAL.BaseDAL (); } } }
3、表现层的简单调用:
IBLL.IT001账号表BLL user=new BLL.T001账号表BLL();
user.GetListBy(m => m.Email =="");//lambda表达式
注:限于篇幅和时间,现在离spring.net 以及工厂化模式还很遥远,以后我会继续更新,有兴趣可以看一下github上的项目,已经实现控制反转与工厂模式