/// <summary> /// 通过实现此方法来拦截调用并执行所需的拦截行为。 /// </summary> /// <param name="input">调用拦截目标时的输入信息。</param> /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param> /// <returns>从拦截目标获得的返回信息。</returns> public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { if (input == null) { throw new ArgumentNullException("input"); } if (getNext == null) { throw new ArgumentNullException("getNext"); } //执行验证操作; var tempMethod = input.MethodBase as System.Reflection.MethodInfo; var targetType = tempMethod.DeclaringType; var targetName = input.Target.GetType().Name.TrimStart('I').Replace("Querier", "").Replace("Repository", "").Replace("AppService", ""); //TODO... //执行业务规则 IList <object> bussinessRules = RuleHandlerContainer.GetRuleHandlers(targetType, tempMethod.Name, targetName); if (bussinessRules.Count == 0) { return(getNext().Invoke(input, getNext)); } try { bool flag = (bool)RuleHandlerInvoker.Invoke(bussinessRules[0], null); if (flag) { return(getNext().Invoke(input, getNext)); } else { return(new VirtualMethodReturn(input, new Exception(string.Format("无法执行该操作,错误原因:{0}", "未知")))); } } catch (Exception ex) { return(new VirtualMethodReturn(input, new Exception(string.Format("无法执行该操作,错误原因:{0}", ex.Message)))); } }
/// <summary> /// 通过实现此方法来拦截调用并执行所需的拦截行为。 /// </summary> /// <param name="input">调用拦截目标时的输入信息。</param> /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param> /// <returns>从拦截目标获得的返回信息。</returns> /// <exception cref="System.ArgumentNullException"> /// input /// or /// getNext /// </exception> public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { if (input == null) { throw new ArgumentNullException("input"); } if (getNext == null) { throw new ArgumentNullException("getNext"); } //执行验证操作; var tempMethod = input.MethodBase as System.Reflection.MethodInfo; ////当方法没有返回值时 不许做任何的验证操作 //if (tempMethod.ReturnType == null) // return getNext().Invoke(input, getNext); var inputParameterCollection = input.Inputs; var arguments = input.Arguments; //input. //根据反射获取对象的成员信息 var realArguments = (object[])ReflectHelper.GetInstanceProperty(arguments, "arguments"); //预处理参数 //this.PrepareDealArguments(realArguments); if (inputParameterCollection.Count == 0) { return(getNext().Invoke(input, getNext)); } IList <ParameterInfo> parameterInfos = new List <ParameterInfo>(); //进行数据验证 this.ParseParameter(arguments, ref parameterInfos); //this.ParseParameter(inputParameterCollection, ref parameterInfos); #region 持久化规则验证 //进行业务逻辑验证 if (parameterInfos.Count == 0) { return(getNext().Invoke(input, getNext)); } var targetName = input.Target.GetType().Name.TrimStart('I').Replace("Repository", "").Replace("AppService", ""); //TODO... //执行业务规则 IList <object> bussinessRules = RuleHandlerContainer.GetRuleHandlers(parameterInfos[0].ParameterType, tempMethod.Name, targetName); if (bussinessRules.Count == 0) { return(getNext().Invoke(input, getNext)); } #endregion bool flag = (bool)RuleHandlerInvoker.Invoke(bussinessRules[0], realArguments); if (flag) { return(getNext().Invoke(input, getNext)); } else { return(new VirtualMethodReturn(input, new Exception(string.Format("无法执行该操作,错误原因:{0}", "无法删除")))); } }