• 泛型应用


    #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
    		
    

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

  • 相关阅读:
    Git 从入门到入坑
    单件模式(单例模式)
    装饰者模式
    观察者模式
    设计模式入门
    SpringBoot + Mybatis + Redis 整合入门项目
    Spring Boot 前后端交互及参数传递
    Spring Securtiy 认证流程(源码分析)
    Spring Boot 静态页面跳转
    第一条手机验证码
  • 原文地址:https://www.cnblogs.com/Jack-S-Wang/p/11543677.html
Copyright © 2020-2023  润新知