#region 私有方法 /// <summary> /// 審覈不一樣狀態的數據 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ctx"></param> /// <param name="entities"></param> /// <returns></returns> private int ApproveCheck<T>(eRASAppDevEntities ctx, IEnumerable<T> entities) { var en = typeof(T); string name = en.Name; var separated = Separate<T>(entities); //新增 Approve<T>(ctx, separated[ActionStatus.Insert], entity => { var newTD = new object(); NCS.Utility.CopyObjectProperties(entity, newTD, ENTITY_KEY); ctx.AddObject(name, newTD); return true; }); //更新 Approve<T>(ctx, separated[ActionStatus.Update], entity => { var target = ctx.ExecuteFunction<T>(name, null).FirstOrDefault(e => (Guid)en.GetProperty("ID").GetValue(e, null) == (Guid)en.GetProperty("ID").GetValue(entity, null)); if (target == null) { throw new BusinessException("正式數據不存在"); } NCS.Utility.CopyObjectProperties(entity, target, ENTITY_KEY); return true; }); //刪除 Approve(ctx, separated[ActionStatus.Delete], entity => { var target = ctx.ExecuteFunction<T>(name, null).FirstOrDefault(e => (Guid)en.GetProperty("ID").GetValue(e, null) == (Guid)en.GetProperty("ID").GetValue(entity, null)); if (target == null) { throw new BusinessException("正式數據不存在"); } ctx.DeleteObject(target); return true; }); return ctx.SaveChanges(); } //數據分類 private Dictionary<ActionStatus, List<T>> Separate<T>(IEnumerable<T> entities) { Type en = typeof(T); var result = new Dictionary<ActionStatus, List<T>> { { ActionStatus.Insert, new List<T>() }, { ActionStatus.Update, new List<T>() }, { ActionStatus.Delete, new List<T>() } }; if (entities != null && entities.Count() != 0) { foreach (var entity in entities) { PropertyInfo proInfo = en.GetProperty("ID"); if (entity == null || (Guid)proInfo.GetValue(entity,null)== Guid.Empty) continue; ActionStatus action; if (Enum.TryParse((string)en.GetProperty("ActionStatus").GetValue(entity, null), out action)) { result[action].Add(entity); } } } return result; } //分類批覈 private void Approve<T>(eRASAppDevEntities ctx, IEnumerable<T> entities, Func<T, bool> approve) { var en = typeof(T); if (entities == null || entities.Count() == 0) return; foreach (var entity in entities) { var pending = ctx.ExecuteFunction<T>(en.Name,null).FirstOrDefault(p => (Guid)en.GetProperty("ID").GetValue(p, null) == (Guid)en.GetProperty("ID").GetValue(entity,null)); if (pending == null) { throw new BusinessException("數據不存在"); } string userID = SessionContext.Current.UserInformation.Id; if (userID == (string)en.GetProperty("MakeBy").GetValue(entity, null)) { throw new BusinessException("不能批覈本人提交的數據"); } en.GetProperty("CheckBy").SetValue(entity,userID,null); en.GetProperty("CheckOn").SetValue(entity,DateTime.Now,null); en.GetProperty("AuthStatus").SetValue(entity,(int)AuthStatus.Approve,null); en.GetProperty("DbTransactionID").SetValue(entity, SessionContext.Current.DbTransactionId, null); var succeed = approve?.Invoke(entity); if (succeed.HasValue && !succeed.Value) continue; ctx.DeleteObject(pending); } } #endregion
這裏我利用泛型的模式將全部共用到的內容進行整合,這樣對應的不一樣對象所處理的內容一致,這樣全部的所對應的內容均可以一個處理,但具體還有沒有錯誤須要再執行修改就能夠了。但總體的思路是不變的,這樣相同業務處理的全部數據均可以直接調用使用。c#