BaseDal类:
public class BaseDal : IDisposable { #region 事件 internal Func<bool> DBCommitting; internal Action<bool> DBCommitted; internal Func<object, bool> DBAdding; internal Action<object, bool> DBAdded; internal Func<object, bool> DBUpdating; internal Action<object, bool> DBUpdated; internal Func<object, bool> DBDeleting; internal Action<object, bool> DBDeleted; internal Func<object, bool, bool> DBSaveOrUpdating; internal Action<object, bool, bool> DBSaveOrUpdated; internal Func<object, bool, bool> DBSaveOrUpdatingForList; internal Action<object, bool, bool> DBSaveOrUpdatedForList; #endregion private DBEntities db = new DBEntities();//实体模型名 private bool isTransaction = false; public bool IsBeginTranscation { get { return isTransaction; } } public IQueryable<T> GetEntity<T>() where T : class { return db.Set<T>().AsNoTracking().AsQueryable<T>(); } /// <summary> /// 添加 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lst"></param> /// <returns></returns> public bool Add<T>(List<T> lst, bool isCommit = true) where T : class { foreach (var item in lst) { db.Entry<T>(item).State = System.Data.EntityState.Added; } if (isCommit && !isTransaction) { bool isContinue = true; if (DBAdding != null) isContinue = DBAdding(lst); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBAdded != null) DBAdded(lst, ret); return ret; } else return false; } else return false; } /// <summary> /// 根据条件删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="predicate"></param> /// <returns></returns> public bool DeleteByCondition<T>(Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class { db.Set<T>().Where<T>(predicate).ToList<T>().ForEach(d => { db.Entry<T>(d).State = System.Data.EntityState.Deleted; }); if (isCommit && !isTransaction) { bool isContinue = true; if (DBDeleting != null) isContinue = DBDeleting(predicate); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBDeleted != null) DBDeleted(predicate, ret); return ret; } else return false; } else return false; } public bool UpdateByCondition<T>(Action<T> updateExpression, Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class { var lstDB = db.Set<T>().Where<T>(predicate).ToList<T>(); var lstEF = db.Set<T>().Local.AsQueryable().Where(predicate).ToList<T>(); lstDB.AddRange(lstEF); lstDB.ForEach(item => { updateExpression(item); if (db.Entry<T>(item).State != EntityState.Deleted && db.Entry<T>(item).State != EntityState.Added) db.Entry<T>(item).State = EntityState.Modified; }); if (isCommit && !isTransaction) { bool isContinue = true; if (DBUpdating != null) isContinue = DBUpdating(predicate); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBUpdated != null) DBUpdated(predicate, ret); return ret; } else return false; } else { return false; } } public bool SaveOrUpdate<T>(T entity, bool isAdd = false, bool isCommit = true) where T : class { if (isAdd) db.Set<T>().Add(entity); else db.Entry(entity).State = System.Data.EntityState.Modified; if (isCommit && !isTransaction) { bool isContinue = true; if (DBSaveOrUpdating != null) isContinue = DBSaveOrUpdating(entity, isAdd); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBSaveOrUpdated != null) DBSaveOrUpdated(entity, isAdd, ret); return ret; } else return false; } else return false; } public bool SaveOrUpdateForList<T>(List<T> entities, bool isAdd = false, bool isCommit = true) where T : class { foreach (T entity in entities) { if (isAdd) db.Set<T>().Add(entity); else db.Entry(entity).State = System.Data.EntityState.Modified; } if (isCommit && !isTransaction) { bool isContinue = true; if (DBSaveOrUpdatingForList != null) isContinue = DBSaveOrUpdatingForList(entities, isAdd); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBSaveOrUpdatedForList != null) DBSaveOrUpdatedForList(entities, isAdd, ret); return ret; } else return false; } else return false; } public int ExecuteSqlCommand(string sql, bool isCommit = true) { db.Database.ExecuteSqlCommand(sql); if (isCommit && !isTransaction) return db.SaveChanges(); else return 0; } public int ExecuteSqlCommand(string sql, bool isCommit, params object[] parameters) { db.Database.ExecuteSqlCommand(sql, parameters); if (isCommit && !isTransaction) return db.SaveChanges(); return 0; } /// <summary> /// 执行存储过程 无超时 /// </summary> /// <param name="ProcName"></param> /// <param name="parList"></param> public void ExecPro(string ProcName, List<SqlParameter> parList) { DbConnection conn = db.Database.Connection; if (conn.State == ConnectionState.Closed) conn.Open(); DbCommand cmd = conn.CreateCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = ProcName; cmd.CommandTimeout = 0; foreach (SqlParameter par in parList) { cmd.Parameters.Add(par); } cmd.ExecuteNonQuery(); } public void BeginTranscation() { isTransaction = true; } public bool Commit() { if (isTransaction) { bool isContinue = true; isTransaction = false; if (DBCommitting != null) isContinue = DBCommitting(); if (isContinue) { var ret = db.SaveChanges() > 0; if (DBCommitted != null) DBCommitted(ret); return ret; } else return false; } else return false; } /// <summary> /// 执行SQL查询语句 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List<T> FindEntityListBySql<T>(string sql) { return db.Database.SqlQuery<T>(sql).ToList<T>(); } public void Close() { if (db != null) { db.Dispose(); db = null; } } public void Dispose() { if (db != null) { db.Dispose(); db = null; } } }
DBEvent类
public class DBEvent<T> where T : class { protected BaseDal db; public DBEvent(BaseDal db) { this.db = db; db.DBUpdating = Updating; db.DBUpdated = Updated; db.DBAdding = Adding; db.DBAdded = Added; db.DBDeleting = Deleting; db.DBDeleted = Deleted; db.DBSaveOrUpdating = SaveOrUpdating; db.DBSaveOrUpdated = SaveOrUpdated; db.DBSaveOrUpdatingForList = SaveOrUpdatingForList; db.DBSaveOrUpdatedForList = SaveOrUpdatedForList; db.DBCommitting = Commintting; db.DBCommitted = Committed; } public delegate bool BeforeUpdateEvent(Expression<Func<T, bool>> predicate); public delegate void AfterUpdateEvent(Expression<Func<T, bool>> predicate, bool isSuccess); public delegate bool BeforeAddEvent(List<T> lstEntity); public delegate void AfterAddEvent(List<T> lstEntity, bool isSuccess); public delegate bool BeforeDeleteEvent(Expression<Func<T, bool>> predicate); public delegate void AfterDeleteEvent(Expression<Func<T, bool>> predicate, bool isSuccess); public delegate bool BeforeSaveOrUpdateEvent(T entity, bool isAdd); public delegate void AfterSaveOrUpdateEvent(T entity, bool isAdd, bool isSuccess); public delegate bool BeforeSaveOrUpdatingForListEvent(List<T> lstEntity, bool isAdd); public delegate bool AfterSaveOrUpdatingForListEvent(List<T> lstEntity, bool isAdd, bool isSuccess); public delegate bool BeforeCommitEvent(); public delegate void AfterCommitEvent(bool isSuccess); public BeforeUpdateEvent BeforeUpdate { set; get; } public AfterUpdateEvent AfterUpdate { set; get; } public BeforeAddEvent BeforeAdd { get; set; } public AfterAddEvent AfterAdd { get; set; } public BeforeDeleteEvent BeforeDelete { get; set; } public AfterDeleteEvent AfterDelete { get; set; } public BeforeSaveOrUpdateEvent BeforeSaveOrUpdate { get; set; } public AfterSaveOrUpdateEvent AfterSaveOrUpdate { get; set; } public BeforeSaveOrUpdatingForListEvent BeforeSaveOrUpdatingForList { get; set; } public AfterSaveOrUpdatingForListEvent AfterSaveOrUpdatingForList { get; set; } public BeforeCommitEvent BeforeCommit { get; set; } public AfterCommitEvent AfterCommit { get; set; } private bool Updating(object arg) { Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>; if (BeforeUpdate != null) return BeforeUpdate(predicate); else return true; } private void Updated(object arg, bool isSuccess) { Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>; if (AfterUpdate != null) AfterUpdate(predicate, isSuccess); } private bool Adding(object arg) { List<T> lst = arg as List<T>; if (BeforeAdd != null) return BeforeAdd(lst); else return true; } private void Added(object arg, bool isSuccess) { List<T> lst = arg as List<T>; if (AfterAdd != null) AfterAdd(lst, isSuccess); } private bool Deleting(object arg) { Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>; if (BeforeDelete != null) return BeforeDelete(predicate); else return true; } private void Deleted(object arg, bool isSuccess) { Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>; if (AfterDelete != null) AfterDelete(predicate, isSuccess); } private bool SaveOrUpdating(object arg, bool isAdd) { var entity = arg as T; if (BeforeSaveOrUpdate != null) return BeforeSaveOrUpdate(entity, isAdd); else return true; } private void SaveOrUpdated(object arg, bool isAdd, bool isSuccess) { var entity = arg as T; if (AfterSaveOrUpdate != null) AfterSaveOrUpdate(entity, isAdd, isSuccess); } private bool SaveOrUpdatingForList(object arg, bool isAdd) { List<T> lstEntity = arg as List<T>; if (BeforeSaveOrUpdatingForList != null) return BeforeSaveOrUpdatingForList(lstEntity, isAdd); else return false; } private void SaveOrUpdatedForList(object arg, bool isAdd, bool isSuccess) { List<T> lstEntity = arg as List<T>; if (AfterSaveOrUpdatingForList != null) AfterSaveOrUpdatingForList(lstEntity, isAdd, isSuccess); } private bool Commintting() { if (BeforeCommit != null) return BeforeCommit(); else return false; } private void Committed(bool isSuccess) { if (AfterCommit != null) AfterCommit(isSuccess); } }
ViewEntityDependent类
public static class ViewEntityDependent { public static void RegisterEntityDependent() { JobView(); } private static void JobView() { List<string> jobRecord = new List<string>() { "JobResumeAutonomy","JobResumeAttention","JobResumeCompanyAuto","JobResumeCompanyPut" }; CacheDependentEntitySet.RegisterDependentEntity("JobRecord", jobRecord); } }