Esempio n. 1
0
        /// <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 inputParameterCollection = input.Inputs;

            //根据反射获取对象的成员信息
            var realArguments = (object[])ReflectHelper.GetInstanceProperty(inputParameterCollection, "arguments");

            if (inputParameterCollection.Count == 0)
            {
                return(getNext().Invoke(input, getNext));
            }

            //获取方法参数
            //System.Reflection.ParameterInfo[] parameterInfos = input.MethodBase.GetParameters();

            //DTO预处理-自动截取空字符串
            this.PreDealDto(realArguments);

            //IList<EntityValidationResult> tempResults = new List<EntityValidationResult>();
            //foreach (var realArgument in realArguments)
            //{
            //    var entityValidationResult = new EntityValidationResult(realArgument as IAggregateRoot, new List<EntityValidationError>());
            //    tempResults.Add(entityValidationResult);
            //}

            //var flag = true;

            //进行数据验证
            //flag = this.ValidateInputParameter(realArguments, ref tempResults);
            //if (parameterInfos != null && parameterInfos.Length > 0)
            //{
            //    //验证所有的参数,判定参数类型,是否需要验证
            //    flag = this.ValidateInputParameter(parameterInfos, ref results);
            //}

            //进行业务逻辑验证
            var outMessage = string.Empty;

            //flag = this.ValidateInputParameter(tempMethod,inputParameterCollection, out outMessage);
            //if (!flag)
            //{
            //    return new VirtualMethodReturn(input, new ValidationException(tempResults[0].ValidationErrors.First().ErrorMessage));
            //}

            return(getNext().Invoke(input, getNext));
        }
Esempio n. 2
0
        /// <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 inputParameterCollection = input.Inputs;

            var tempLogAttributes = tempMethod.GetCustomAttributes(typeof(LogAttribute));

            if (tempLogAttributes.Count() == 0)
            {
                return(getNext().Invoke(input, getNext));
            }

            //根据反射获取对象的成员信息
            var realArguments = (object[])ReflectHelper.GetInstanceProperty(inputParameterCollection, "arguments");

            if (inputParameterCollection.Count == 0)
            {
                return(getNext().Invoke(input, getNext));
            }

            //计时器开始 监视方法执行时间---Debug模式下启用

            //开始监视
            //计时器
#if DEBUG
            var stopwatch        = Stopwatch.StartNew();
            var tempReturnResult = getNext().Invoke(input, getNext);
            stopwatch.Stop();
            var totalMillisencodes = stopwatch.Elapsed.TotalMilliseconds;
#else
            var tempReturnResult = getNext().Invoke(input, getNext);
#endif
            //生成日志对象
            var logInfo = string.Format("TODO....");
            //输出到log---采用异步方式
            ILogProvider logProvider = ObjectContainer.CreateInstance <ILogProvider>();
            logProvider.Debug(logInfo);
            return(tempReturnResult);
        }
Esempio n. 3
0
        /// <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 inputParameterCollection = input.Inputs;

            //根据反射获取对象的成员信息
            var realArguments = (object[])ReflectHelper.GetInstanceProperty(inputParameterCollection, "arguments");

            //获取当前方法所属的对象
            var tempTarget = input.Target;

            var methodReturn = getNext().Invoke(input, getNext);

            if (methodReturn.Exception != null && (methodReturn.Exception is TimeoutException))
            {
                //将当前操作的参数序列化到对象中
                var tempResult = this.PreDealOfflineDto(input, tempTarget, tempMethod, realArguments);

                return(new VirtualMethodReturn(input, tempResult, realArguments));
            }
            else
            {
                //离线数据同步事件委托
                Action tempSyncAction = new Action(() =>
                {
                    //同步离线数据到数据中心
                    IOfflineSyncService syncService = ObjectContainer.CreateInstance <IOfflineSyncService>();
                    syncService.Sync();
                });

                //异步调用
                tempSyncAction.BeginInvoke(null, null);
            }

            return(methodReturn);
        }
        /// <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}", "无法删除"))));
            }
        }
        /// <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 methodReturn = getNext().Invoke(input, getNext);

            //获取当前操作的方法
            var tempMethod = input.MethodBase as System.Reflection.MethodInfo;

            var tempDataSyncAttributes      = tempMethod.GetCustomAttributes(typeof(DataSyncAttribute), false);
            var tempDataSyncCloudAttributes = tempMethod.GetCustomAttributes(typeof(DataSyncCloudAttribute), false);

            //如果该方法上有标记
            if (tempDataSyncAttributes.Length > 0)
            {
                //方法参数
                var inputParameterCollection = input.Inputs;

                //根据反射获取对象的成员信息
                var realArguments = (object[])ReflectHelper.GetInstanceProperty(inputParameterCollection, "arguments");

                //获取当前方法所属的对象
                var tempTarget = input.Target;

                //生成对应的信息-并保存到数据库中
                DataSyncContract tempDataSyncContract = new DataSyncContract();

                if (realArguments.Length > 0)
                {
                    tempDataSyncContract.Data     = NS.Framework.Utility.Xml.XmlHelper.Instance.SerializeToString(realArguments[0]);
                    tempDataSyncContract.DataType = string.Format("{0};{1}", realArguments[0].GetType().Assembly.GetName().Name + ".dll", realArguments[0].GetType().FullName);
                }

                tempDataSyncContract.Id            = Guid.NewGuid().ToString();
                tempDataSyncContract.MethodName    = tempMethod.Name;
                tempDataSyncContract.ServicePath   = string.Format("{0};{1}", input.MethodBase.ReflectedType.Assembly.GetName().Name + ".dll", input.Target.GetType().FullName);
                tempDataSyncContract.OperationTime = DateTime.Now;
                tempDataSyncContract.SyncFlag      = "0";
                var tempDataSyncAttribute = tempDataSyncAttributes[0] as DataSyncAttribute;

                //如果要求实时同步
                if (tempDataSyncAttribute.IsRealTimeSync)
                {
                    this.RemoteCall(tempDataSyncContract);
                }
                else //不要求实时同步的数据,就通过离线同步版本的方式来传输
                {
                    this.SaveUnlineData(tempDataSyncContract);
                }
            }
            //如果该方法上有标记
            if (tempDataSyncCloudAttributes.Length > 0)
            {
                //方法参数
                var inputParameterCollection = input.Inputs;

                //根据反射获取对象的成员信息
                var realArguments = (object[])ReflectHelper.GetInstanceProperty(inputParameterCollection, "arguments");

                //获取当前方法所属的对象
                var tempTarget = input.Target;

                //生成对应的信息-并保存到数据库中
                DataSyncContract tempDataSyncContract = new DataSyncContract();

                if (realArguments.Length > 0)
                {
                    tempDataSyncContract.Data     = NS.Framework.Utility.Xml.XmlHelper.Instance.SerializeToString(realArguments[0]);
                    tempDataSyncContract.DataType = string.Format("{0};{1}", realArguments[0].GetType().Assembly.GetName().Name + ".dll", realArguments[0].GetType().FullName);
                }

                tempDataSyncContract.Id            = Guid.NewGuid().ToString();
                tempDataSyncContract.MethodName    = tempMethod.Name;
                tempDataSyncContract.ServicePath   = string.Format("{0};{1}", input.MethodBase.ReflectedType.Assembly.GetName().Name + ".dll", input.Target.GetType().FullName);
                tempDataSyncContract.OperationTime = DateTime.Now;
                tempDataSyncContract.SyncFlag      = "1";
                var tempDataSyncAttribute = tempDataSyncCloudAttributes[0] as DataSyncCloudAttribute;

                //如果要求实时同步
                if (tempDataSyncAttribute.IsRealTimeSync)
                {
                    this.RemoteCall(tempDataSyncContract);
                }
                else //不要求实时同步的数据,就通过离线同步版本的方式来传输
                {
                    this.SaveUnlineData(tempDataSyncContract);
                }
            }
            return(methodReturn);
        }