EF架構~終於自己架構了一個相對完整的EF方案
阿新 • • 發佈:2017-05-26
context src config .cn 存儲 cte 分享 insert 變化
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 /// <summary> 2 3 /// 數據操作基類 4 5 /// </summary> 6 7 public abstract class RepositoryBase 8 9 { 10 11 #region 單件模式創建一個類對象 12 13 /// <summary> 14 15 /// 數據源對象 16 17 /// </summary> 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 #endregion 34 35 36 37 public OAContext _db = CreateInstance(); 38 39 40 41 /// <summary> 42 43 /// 存儲變化 service層可能也會使用本方法,所以聲明為public 44 45 /// </summary> 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 /// <summary> 2 3 /// 數據操作統一接口(更新) 4 5 /// </summary> 6 7 public interface IRepository<TEntity> where TEntity : class //這裏使用泛型接口 8 9 { 10 11 /// <summary> 12 13 /// 根據數據庫實體—》更新記錄 14 15 /// </summary> 16 17 /// <param name="entity"></param> 18 19 void Update(TEntity entity); 20 21 22 23 /// <summary> 24 25 /// 根據響應的屬性名稱進行更新 26 27 /// </summary> 28 29 /// <param name="entity">要更新的實體</param> 30 31 /// <param name="enums">要響應的列枚舉</param> 32 33 void Update(TEntity entity, Enum enums); 34 35 36 37 /// <summary> 38 39 /// 根據數據庫實體—》[批量]更新記錄 40 41 /// </summary> 42 43 /// <param name="entity"></param> 44 45 void Update(IList<TEntity> list); 46 47 48 49 /// <summary> 50 51 /// 根據響應的屬性名稱進行批量更新 52 53 /// </summary> 54 55 /// <param name="list">要更新的實體IList<IDataEntity></param> 56 57 /// <param name="enums">要響應的列枚舉</param> 58 59 void Update(IList<TEntity> list, Enum enums); 60 61 }
IEntityRepository.cs
1 /// <summary> 2 3 /// 數據操作統一接口(插入,查詢,刪除) 4 5 /// </summary> 6 7 public interface IEntityRepository<TEntity> where TEntity : class //這裏使用泛型接口 8 9 { 10 11 12 13 /// <summary> 14 15 /// 根據數據庫實體—》插入記錄 16 17 /// </summary> 18 19 void Insert(TEntity entity); 20 21 22 23 /// <summary> 24 25 /// 根據數據庫實體—》[批量]插入記錄 26 27 /// </summary> 28 29 void Insert(IList<TEntity> list); 30 31 32 33 /// <summary> 34 35 /// 刪除單條記錄 36 37 /// </summary> 38 39 /// <param name="oArr"></param> 40 41 void Delete(TEntity entity); 42 43 44 45 /// <summary> 46 47 /// 刪除列表 48 49 /// </summary> 50 51 /// <param name="list"></param> 52 53 void Delete(IList<TEntity> list); 54 55 56 57 /// <summary> 58 59 /// 得到實體列表 60 61 /// </summary> 62 63 /// <returns></returns> 64 65 DbSet<TEntity> GetList(); 66 67 }
EntityRepository.cs
1 /// <summary> 2 3 /// 數據操作實現類(統一實現類) 4 5 /// </summary> 6 7 /// <typeparam name="TEntity"></typeparam> 8 9 public class EntityRepository<TEntity> : RepositoryBase, IEntityRepository<TEntity> 10 11 where TEntity : class 12 13 { 14 15 #region IEntityRepository<TEntity> Members 16 17 18 19 public void Insert(TEntity entity) 20 21 { 22 23 this._db.Set<TEntity>().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<TEntity> list) 34 35 { 36 37 list.ToList().ForEach(entity => 38 39 { 40 41 this._db.Set<TEntity>().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<TEntity>().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<TEntity> list) 68 69 { 70 71 list.ToList().ForEach(entity => 72 73 { 74 75 this._db.Set<TEntity>().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<TEntity> GetList() 88 89 { 90 91 return this.DbSet; 92 93 } 94 95 96 97 /// <summary> 98 99 /// 泛型數據表屬性 100 101 /// </summary> 102 103 protected DbSet<TEntity> DbSet 104 105 { 106 107 get { return this._db.Set<TEntity>(); } 108 109 } 110 111 #endregion 112 113 114 115 /// <summary> 116 117 /// 操作提交 118 119 /// </summary> 120 121 public override void SaveChanges() 122 123 { 124 125 base.SaveChanges(); 126 127 } 128 129 }
EF架構~終於自己架構了一個相對完整的EF方案