EF4.1学了有段时间了,没有静下来好好研究它的架构,今天有空正好把它的架构及数据操作这段拿出来,希望给大家带来帮助,对我自己也是一种总结:P
从图中可以看到,我们用的是MVC3进行程序开发的,哈哈,也是刚开始用3.0,项目整体架构还是传统三层,其它公用层我就不说了,服务层和UI层也不说了,单说EF还在的实体层和数据层,我用EF生成器把它生成后,又整理了一个,因为我不想让EF的低层方法暴露给业务层.
我来一个一个的说我的方案:
OAContext.cs:这是生成器生成的,这不作修
RepositoryBase.cs:这是数据库基类,里面对它的子类公开了一个属性和一个方法,
属性就是OAContext的实例,而方法就是SaveChanges的一个封装,子类可以根据自己的逻辑去复写它.
IRepository.cs:这是提供更新操作的统一接口,需要有更新需要的子类去实现,EF的统一更新我还没有想出来,哈哈
IEntityRepository.cs:这是统一的数据操作接口,除了更新之外的所有操作,统一操作接口是一个泛型接口
EntityRepository.cs:对统一操作接口的实现
好了,下面我把源代码公开,大家可以看一下,有好的建设请和我联系!
RepositoryBase.cs
1 ///2 3 /// 数据操作基类 4 5 /// 6 7 public abstract class RepositoryBase 8 9 {10 11 #region 单件模式创建一个类对象12 13 ///14 15 /// 数据源对象16 17 /// 18 19 private static OAContext dbContext = null;20 21 protected static OAContext CreateInstance()22 23 {24 25 if (dbContext == null)26 27 dbContext = new OAContext();28 29 return dbContext;30 31 }32 33 #endregion34 35 36 37 public OAContext _db = CreateInstance();38 39 40 41 ///42 43 /// 存储变化 service层可能也会使用本方法,所以声明为public44 45 /// 46 47 public virtual void SaveChanges()48 49 {50 51 this._db.Configuration.ValidateOnSaveEnabled = false;52 53 this._db.SaveChanges();54 55 }56 57 }
IRepository.CS
1 ///2 3 /// 数据操作统一接口(更新) 4 5 /// 6 7 public interface IRepositorywhere TEntity : class //这里使用泛型接口 8 9 {10 11 /// 12 13 /// 根据数据库实体—》更新记录14 15 /// 16 17 /// 18 19 void Update(TEntity entity);20 21 22 23 ///24 25 /// 根据响应的属性名称进行更新26 27 /// 28 29 /// 要更新的实体30 31 /// 要响应的列枚举32 33 void Update(TEntity entity, Enum enums);34 35 36 37 ///38 39 /// 根据数据库实体—》[批量]更新记录40 41 /// 42 43 /// 44 45 void Update(IListlist);46 47 48 49 /// 50 51 /// 根据响应的属性名称进行批量更新52 53 /// 54 55 /// 要更新的实体IList56 57 /// 要响应的列枚举58 59 void Update(IList list, Enum enums);60 61 }
IEntityRepository.cs
1 ///2 3 /// 数据操作统一接口(插入,查詢,刪除) 4 5 /// 6 7 public interface IEntityRepositorywhere TEntity : class //这里使用泛型接口 8 9 {10 11 12 13 /// 14 15 /// 根据数据库实体—》插入记录16 17 /// 18 19 void Insert(TEntity entity);20 21 22 23 ///24 25 /// 根据数据库实体—》[批量]插入记录26 27 /// 28 29 void Insert(IListlist);30 31 32 33 /// 34 35 /// 删除单条记录36 37 /// 38 39 /// 40 41 void Delete(TEntity entity);42 43 44 45 ///46 47 /// 删除列表48 49 /// 50 51 /// 52 53 void Delete(IListlist);54 55 56 57 /// 58 59 /// 得到实体列表60 61 /// 62 63 ///64 65 DbSet GetList();66 67 }
EntityRepository.cs
1 ///2 3 /// 数据操作实现类(统一实现类) 4 5 /// 6 7 ///8 9 public class EntityRepository : RepositoryBase, IEntityRepository 10 11 where TEntity : class 12 13 { 14 15 #region IEntityRepository Members 16 17 18 19 public void Insert(TEntity entity) 20 21 { 22 23 this._db.Set ().Add(entity); 24 25 this._db.Entry(entity).State = System.Data.EntityState.Added; 26 27 this.SaveChanges(); 28 29 } 30 31 32 33 public void Insert(IList list) 34 35 { 36 37 list.ToList().ForEach(entity => 38 39 { 40 41 this._db.Set ().Add(entity); 42 43 this._db.Entry(entity).State = System.Data.EntityState.Added; 44 45 }); 46 47 this.SaveChanges(); 48 49 } 50 51 52 53 public void Delete(TEntity entity) 54 55 { 56 57 this._db.Set ().Remove(entity); 58 59 this._db.Entry(entity).State = System.Data.EntityState.Deleted; 60 61 this.SaveChanges(); 62 63 } 64 65 66 67 public void Delete(IList list) 68 69 { 70 71 list.ToList().ForEach(entity => 72 73 { 74 75 this._db.Set ().Remove(entity); 76 77 this._db.Entry(entity).State = System.Data.EntityState.Deleted; 78 79 }); 80 81 this.SaveChanges(); 82 83 } 84 85 86 87 public System.Data.Entity.DbSet GetList() 88 89 { 90 91 return this.DbSet; 92 93 } 94 95 96 97 /// 98 99 /// 泛型数据表属性100 101 /// 102 103 protected DbSetDbSet104 105 {106 107 get { return this._db.Set (); }108 109 }110 111 #endregion112 113 114 115 /// 116 117 /// 操作提交118 119 /// 120 121 public override void SaveChanges()122 123 {124 125 base.SaveChanges();126 127 }128 129 }