泛型应用

#region 私有方法
        /// 
        /// 审核不同状态的数据
        /// 
        /// 
        /// 
        /// 
        /// 
        private int ApproveCheck(eRASAppDevEntities ctx, IEnumerable entities)
        {
            var en = typeof(T);
            string name = en.Name;
            var separated = Separate(entities);

            //新增
            Approve(ctx, separated[ActionStatus.Insert], entity =>
            {
                var newTD = new object();

                NCS.Utility.CopyObjectProperties(entity, newTD, ENTITY_KEY);
                ctx.AddObject(name, newTD);
                return true;
            });

            //更新
            Approve(ctx, separated[ActionStatus.Update], entity =>
            {
                var target = ctx.ExecuteFunction(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(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> Separate(IEnumerable entities)
        {
            Type en = typeof(T);
            var result = new Dictionary>
            {
                { ActionStatus.Insert, new List() },
                { ActionStatus.Update, new List() },
                { ActionStatus.Delete, new List() }
            };
            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(eRASAppDevEntities ctx, IEnumerable entities,
            Func approve)
        {
            var en = typeof(T);
            if (entities == null || entities.Count() == 0)
                return;

            foreach (var entity in entities)
            {
                var pending = ctx.ExecuteFunction(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
        

这里我利用泛型的模式将所有共用到的内容进行整合,这样对应的不同对象所处理的内容一致,这样所有的所对应的内容都可以一个处理,但具体还有没有错误需要再执行修改就可以了。但整体的思路是不变的,这样相同业务处理的所有数据都可以直接调用使用。

转载于:https://www.cnblogs.com/Jack-S-Wang/p/11543677.html

你可能感兴趣的:(泛型应用)