1.項目中經常要用到 EF,有時候大多數的增刪改查都是重復性的東西,本次封裝就是為了快速開發,期間沒有考慮到架構上的各種思想,就感覺到欠缺點什么東西所以這次將這些拉出來,有存在問題的話還請各位多多指導。
2.封裝后從壓力和並發上也沒有去測試,有興趣的小伙伴還望給看下。好了不廢話了直接上了。
a.先看下大概結構如下 按照順序介紹 a.1: 實體就是一般大家手動寫的實體
a.2: DALContext.cs 代碼如下:
namespace Test.Web.Site.DAL { public class DALContext<T> : DbContext where T : class { public DALContext(string con) : base(con){} public DALContext(){} public DbSet<T> TV { get; set; } } }
a.3 BaseDAL.cs 主要的增刪改方法
namespace Test.Web.Site.DAL { public class BaseDAL<TEntity> where TEntity : class,new() { private static readonly object s_lock = new object(); private static string constr = "xxxConn";//默認一個字符串 public BaseDAL(string con = "") { if (con != constr && con.Length > 0) { lock (s_lock) { constr = con; } } }
#region Modify and Delete public virtual bool Insert(TEntity entity) { using (var db = new DALContext<TEntity>(constr)) { db.Set<TEntity>().Add(entity); return db.SaveChanges() > 0; } } public virtual bool Delete(object col) { using (var db = new DALContext<TEntity>(constr)) { TEntity entityToDelete = db.Set<TEntity>().Find(col); if (entityToDelete != null) { return Delete(entityToDelete); } else { return false; } } } public virtual bool Delete(Expression<Func<TEntity, bool>> predicate) { TEntity entityToDelete = Get(predicate); if (entityToDelete != null) { return Delete(entityToDelete); } else { return false; } } public virtual IEnumerable<TEntity> InsertAll(List<TEntity> list) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); List<TEntity> tList = new List<TEntity>(); foreach (var item in list) { try { db.Set<TEntity>().Add(item); } catch (Exception) { tList.Add(item); throw; } } db.SaveChanges(); return tList; } } public virtual IEnumerable<TEntity> UpdateAll(List<TEntity> list) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); List<TEntity> tList = new List<TEntity>(); foreach (var item in list) { try { var entity = dbSet.Attach(item); db.Entry(item).State = System.Data.EntityState.Modified; } catch (Exception) { tList.Add(item); throw; } } db.SaveChanges(); return tList; } } public virtual bool Update(TEntity entityToUpdate) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); var entity = dbSet.Attach(entityToUpdate); db.Entry(entityToUpdate).State = System.Data.EntityState.Modified; return db.SaveChanges() > 0; } } #endregion } }
a.4 DatabaseExtensions.cs 一個EF 擴展類 用於 連接其他數據庫比如 mysql(參考的網絡資源),執行sql語句查詢視圖有需要可在下方評論,我會及時回復
a.5 : Test.Web.Site.BLL 對Control 開放的一系列方法 ,這里僅列舉了部分代碼出來,如有需要可在下方評論我及時回復你。
namespace Test.Web.Site.BLL { public class DataBLL<T> where T : class,new() { // 連接字符串 public DataBLL(string constr = "") { SingletonBase<BaseDAL<T>>.Initialize(new BaseDAL<T>(constr)); }public T Get(Expression<Func<T, bool>> filter, string includeProperties = "") { return SingletonBase<BaseDAL<T>>.Instance.Get(filter, includeProperties); } // 插入一個實體 public bool AddEntity(T t_entity) { return SingletonBase<BaseDAL<T>>.Instance.Insert(t_entity); } // 跟新實體 public bool Update(T t_enttiy) { return SingletonBase<BaseDAL<T>>.Instance.Update(t_enttiy); } // 依據sql查詢 list public IEnumerable<object> GetListBySql(string query,bool otherdb = true) { return SingletonBase<BaseDAL<T>>.Instance.GetBySql(query, otherdb); } // 刪除滿足條件的所有數據 public IEnumerable<T> DeleteAll(Expression<Func<T, bool>> predicate) { return SingletonBase<BaseDAL<T>>.Instance.DeleteAll(predicate); } // 批量更新 list public IEnumerable<T> UpdateAll(List<T> list) { return SingletonBase<BaseDAL<T>>.Instance.UpdateAll(list); } // 批量添加 public IEnumerable<T> InsertAll(List<T> list) { return SingletonBase<BaseDAL<T>>.Instance.InsertAll(list); } } }
3.Control 調用只需要類似:
Account currAccount = new DataBLL<Account>(ConfigHelper.Constr_read).Get(p => p.UserId == 1111);
new DataBLL<Account>.GetPage(out totalCount, query.Expression, pageIndex, pageSize, k => k.OrderByDescending(_ => _.CreateTime)).ToList();
總結:到此就結束了所有代碼可復制過去加入EF引用就開始用了。有發現問題的小伙伴敬請拋磚。小弟不勝感激。