日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

泛型应用

發(fā)布時(shí)間:2024/4/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 泛型应用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#region 私有方法/// <summary>/// 審核不同狀態(tài)的數(shù)據(jù)/// </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("正式數(shù)據(jù)不存在");}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("正式數(shù)據(jù)不存在");}ctx.DeleteObject(target);return true;});return ctx.SaveChanges();}//數(shù)據(jù)分類(lèi)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;}//分類(lèi)批核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("數(shù)據(jù)不存在");}string userID = SessionContext.Current.UserInformation.Id;if (userID == (string)en.GetProperty("MakeBy").GetValue(entity, null)){throw new BusinessException("不能批核本人提交的數(shù)據(jù)");}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

這里我利用泛型的模式將所有共用到的內(nèi)容進(jìn)行整合,這樣對(duì)應(yīng)的不同對(duì)象所處理的內(nèi)容一致,這樣所有的所對(duì)應(yīng)的內(nèi)容都可以一個(gè)處理,但具體還有沒(méi)有錯(cuò)誤需要再執(zhí)行修改就可以了。但整體的思路是不變的,這樣相同業(yè)務(wù)處理的所有數(shù)據(jù)都可以直接調(diào)用使用。

轉(zhuǎn)載于:https://www.cnblogs.com/Jack-S-Wang/p/11543677.html

總結(jié)

以上是生活随笔為你收集整理的泛型应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。