到底什么样的框架才是好框架呢?或许不同人有不同的看法.我个人觉一个好的框架, 最重要的要是简单实用,能快速适开发,可维护性高(不会出现复制黏贴的代码),并能快速响应各种业务场景的变化的框架,同时性能不会太差.我觉的这样的框 架,就是一个好的框架.而且,我觉的做框架,千万不能设计过度,不然会得不偿失.最关键要看你的业务场景,千万不要因为模式而模式,更多的看考虑它的实用 性. 就像我接触的我们公司一个基于EF的三层,架构,我就觉得里面有一些不好的设计.主要表现如下.
- 每个类的一些基本的操作,如增加修改,删除.都是通过代码生成器生成的.其实我是很反对用代码生成器的.用代码生成的代码,就相当于我们在写代码中,进行复制黏贴.这个在我们设计上是很杜绝的. 我们以前做项目,都是不用代码生成器.用代码生成器,只会生成那个实体类.
- Bll层和DAL层,职责不分明.我看我做的那项目,写的代码比较乱.有些把业 务层的东西,写在dal层. 这个也是框架的问题导致,因为bll层,没有事务,所以把事务里面的业务,也写在dal层里.同时有些人也为了方便,把访问数据的逻辑都放在bll层. 总之让人感觉很混乱.
- 是用IOC技术,把所有的bll层,dal层都是基于接口的方式去实现.到现在 为止,我都觉的这个东西有点设计过度了.因为大部分项目,都不会去换dal层,bll层.如果硬要把所有的bll层,dal层都用IOC来做,会带来如下 问题.(1) 我们想增加一个方法,经常要在4个地方写函数. (2)想用F12跟进去查看代码,是跟不进去的.只能跟到接口那里. (3)最要命的就是它那个报错.经常会报配置错误,其实一查根本不是配置错误.只是生成的类有问题
其实EF框架,还是很好用的,尤其在快速开发上,有不可替代的优势.而且性能上,我也做过压力测试,跟ado.net,差不了多少.因此,我就想开发一个基于EF的三层架构,请看下面的相关类图.
该框架,主要实现如下功能.
- 所有的dal层和bll层都有个基类,在基类里把所有的基本操作封装起来.
- 在dal层里,并没有真正的操作数据库,只是根据ef生成相应的语句,真正执行是在bll层里.
- 在bll层里有context对象,用来是控制bll层里的事务.
- Service层,是用来给外部提供系统调用的,可以是wcf,也可以webservice,也可以webapi 的方式.
- Web 层可的数据,可以来自bll层,也可以来自service层.(在案例中还没有实现)
我觉的我这框架的优点和缺点,我将下次再介绍,有兴趣的朋友,可以先下载代码来看看.同时也欢迎各位朋友评价这框架..
下载路径 http://pan.baidu.com/share/link?shareid=464738&uk=3322219884
同时,我也提供一些实现的代码,供大家参考
dal层
View Code
1 using Architecture.Mode; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.Objects; 6 using System.Data.Objects.DataClasses; 7 using System.Linq; 8 using System.Linq.Expressions; 9 using System.Text; 10 using System.Threading.Tasks; 11 12 namespace Architecture.DAL 13 { 14 public class BaseDAL<T> : IDisposable where T : EntityObject 15 { 16 public IContext Context; 17 private Entities db; 18 private bool isSetDB; 19 public BaseDAL() 20 { 21 ENContext context = new ENContext(); 22 this.Context = context; 23 this.db = context.db; 24 isSetDB = false; 25 updateChange(); 26 } 27 28 public BaseDAL(IContext context) 29 { 30 this.Context = context; 31 var context1 = context as ENContext; 32 if (context1.db == null) throw new Exception("初始化时,上下文为空"); 33 this.db = context1.db; 34 isSetDB = true; 35 updateChange(); 36 } 37 38 private void updateChange() 39 { 40 //对公共字段的统一修改. 41 this.db.SavingChanges += new EventHandler(changing); 42 } 43 44 private void changing(object o, EventArgs e) 45 { 46 Entities ec = o as Entities; 47 if (ec != null) 48 { 49 //增加的 50 foreach (ObjectStateEntry entry in ec.ObjectStateManager.GetObjectStateEntries(EntityState.Added)) 51 { 52 53 Type t = entry.Entity.GetType(); 54 var property = t.GetProperty("CreationUser"); 55 if(property!=null) property.SetValue(entry.Entity, "test"); 56 property = t.GetProperty("ModificationUser"); 57 if (property != null) property.SetValue(entry.Entity, "test"); 58 property = t.GetProperty("CreationDate"); 59 if (property != null) property.SetValue(entry.Entity, System.DateTime.Now); 60 property = t.GetProperty("ModificationDate"); 61 if (property != null) property.SetValue(entry.Entity, DateTime.Now); 62 property = t.GetProperty("Status"); 63 if (property != null) property.SetValue(entry.Entity, 1); 64 } 65 66 //修改的 67 foreach (ObjectStateEntry entry in ec.ObjectStateManager.GetObjectStateEntries(EntityState.Modified)) 68 { 69 70 Type t = entry.Entity.GetType(); 71 var property = t.GetProperty("ModificationUser"); 72 if (property != null) property.SetValue(entry.Entity, "test"); 73 property = t.GetProperty("ModificationDate"); 74 if (property != null) property.SetValue(entry.Entity, DateTime.Now); 75 76 } 77 } 78 } 79 80 //添加 81 public T AddEntity(T entity) 82 { 83 db.CreateObjectSet<T>().AddObject(entity); 84 // db.Entry<T>(entity).State = EntityState.Added; 85 //db.SaveChanges(); 86 return entity; 87 } 88 89 //修改 90 public bool UpdateEntity(T entity) 91 { 92 93 if (entity.EntityKey != null) 94 { 95 // entity.EntityKey = null; 96 // entity. 97 // db.Attach(entity); 98 // db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 99 } 100 else 101 { 102 db.CreateObjectSet<T>().Attach(entity); 103 db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 104 } 105 return true; 106 //db.Set<T>().Attach(entity); 107 //db.Entry<T>(entity).State = EntityState.Modified; 108 // return db.SaveChanges() > 0; 109 110 } 111 112 /// <summary> 113 /// 批量修改 114 /// </summary> 115 /// <param name="where"></param> 116 /// <param name="updater"></param> 117 /// <returns></returns> 118 public int UpdateEntitiesWhere(Expression<Func<T, bool>> where, Expression<Func<T>> updater) 119 { 120 return db.CreateObjectSet<T>().Update(where, updater); 121 } 122 123 //删除 124 public bool DeleteEntity(T entity) 125 { 126 if (entity.EntityKey == null) 127 { 128 db.CreateObjectSet<T>().Attach(entity); 129 } 130 db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); 131 return true; 132 // db.Set<T>().Attach(entity); 133 //db.Entry<T>(entity).State = EntityState.Deleted; 134 //return db.SaveChanges() > 0; 135 136 } 137 138 /// <summary> 139 /// 批量删除 140 /// </summary> 141 /// <param name="where"></param> 142 /// <returns></returns> 143 public int DeleteEntitiesWhere(Expression<Func<T, bool>> where) 144 { 145 return db.CreateObjectSet<T>().Delete(where); 146 } 147 148 149 public T GetEntityByID(object id) 150 { 151 Type t = typeof(T); 152 string tableName=t.Name; 153 string sql = string.Format("select * from {0} where id=@p0", tableName); 154 var ent= db.ExecuteStoreQuery<T>(sql, id).FirstOrDefault(); 155 if (ent != null) 156 db.CreateObjectSet<T>().Attach(ent); 157 return ent; 158 } 159 160 /// <summary> 161 /// 查询实体 162 /// </summary> 163 /// <param name="where">条件</param> 164 /// <param name="includes">预加载的词</param> 165 /// <returns></returns> 166 public IQueryable<T> GetEntities(Expression<Func<T, bool>> where,params string[] includes) 167 { 168 //预加载 169 if (includes != null && includes.Length > 0) 170 { 171 var dbEntities = db.CreateObjectSet<T>().Include(includes[0]); 172 bool isFirst = true; 173 foreach (string include in includes) 174 { 175 if (isFirst) 176 isFirst = false; 177 else 178 { 179 dbEntities = dbEntities.Include(include); 180 } 181 } 182 return dbEntities.Where(where); 183 } 184 else 185 { 186 return db.CreateObjectSet<T>().Where(where); 187 } 188 //return db.Set<T>().Where<T>(wherelambda).AsQueryable(); 189 } 190 191 /// <summary> 192 /// 根据分页取查询条件 193 /// </summary> 194 /// <typeparam name="TResult"></typeparam> 195 /// <param name="pageIndex"></param> 196 /// <param name="pageSize"></param> 197 /// <param name="where"></param> 198 /// <param name="orderByProperty"></param> 199 /// <param name="isAscendingOrder"></param> 200 /// <param name="total"></param> 201 /// <param name="includes"></param> 202 /// <returns></returns> 203 public IQueryable<T> GetEntitiesByPage<TResult>(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, TResult>> orderByProperty, bool isAscendingOrder, ref int total, params string[] includes) 204 { 205 var list = GetEntities(where,includes); 206 return list.Page(pageIndex, pageSize, orderByProperty, isAscendingOrder, ref total); 207 } 208 209 210 211 public void Dispose() 212 { 213 if (this.Context != null&&!isSetDB) 214 this.Context.Dispose(); 215 } 216 } 217 }
bll层
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Architecture.DAL; 7 using System.Linq.Expressions; 8 using System.Data.Objects.DataClasses; 9 10 namespace Architecture.BLL 11 { 12 public class BaseBLL<T>:IDisposable where T : EntityObject 13 // where DAL:BaseDAL<T>,new() 14 { 15 // protected DAL myDal = new DAL(); 16 protected BaseDAL<T> myDal = new BaseDAL<T>(); 17 protected IContext Context 18 { 19 get 20 { 21 return myDal.Context; 22 } 23 } 24 public T AddEntity(T entity) 25 { 26 // using (var dal = new BaseDAO<T>()) 27 { 28 myDal.AddEntity(entity); 29 myDal.Context.SaveChanges(); 30 return entity; 31 } 32 } 33 34 //修改 35 public bool UpdateEntity(T entity) 36 { 37 // using (var dal = new BaseDAO<T>()) 38 { 39 myDal.UpdateEntity(entity); 40 return myDal.Context.SaveChanges()>0; 41 } 42 } 43 44 /// <summary> 45 /// 批量修改 46 /// </summary> 47 /// <param name="where"></param> 48 /// <param name="updater"></param> 49 /// <returns></returns> 50 public int UpdateEntitiesWhere(Expression<Func<T, bool>> where, Expression<Func<T>> updater) 51 { 52 return myDal.UpdateEntitiesWhere(where, updater); 53 } 54 55 /// <summary> 56 /// 删除 57 /// </summary> 58 /// <param name="entity"></param> 59 /// <returns></returns> 60 public bool DeleteEntity(T entity) 61 { 62 // using (var dal = new BaseDAO<T>()) 63 { 64 65 myDal.DeleteEntity(entity); 66 return myDal.Context.SaveChanges() > 0; 67 } 68 } 69 70 public int DeleteEntitiesWhere(Expression<Func<T, bool>> where) 71 { 72 return myDal.DeleteEntitiesWhere(where); 73 } 74 75 76 ////查询 77 public IList<T> GetEntities(Expression<Func<T, bool>> where,params string[] includes) 78 { 79 // using (var dal = new BaseDAO<T>()) 80 { 81 return myDal.GetEntities(where,includes).ToList<T>(); 82 } 83 } 84 85 public T GetEntityByID(object id) 86 { 87 return myDal.GetEntityByID(id); 88 } 89 90 ////分页 91 public IList<T> GetEntitiesByPage<TResult>(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, TResult>> orderByProperty, bool isAscendingOrder, ref int total, params string[] includes) 92 { 93 // using (var dal = new BaseDAO<T>()) 94 { 95 return myDal.GetEntitiesByPage(pageIndex,pageSize, where,orderByProperty,isAscendingOrder,ref total,includes).ToList<T>(); 96 } 97 } 98 99 public void Dispose() 100 { 101 if (myDal != null) myDal.Dispose(); 102 } 103 } 104 105 //public class BaseBLL<T> : BaseBLL<T, BaseDAL<T>>, IDisposable where T : EntityObject 106 //{ 107 //} 108 }