コード例 #1
0
        public void GetWhereExpressionTest()
        {
            WhereConditionArguments <TestClassA> whereConditionArguments = new WhereConditionArguments <TestClassA>();

            whereConditionArguments.SpliceMode = Enums.EnumSpliceMode.AndAlso;
            whereConditionArguments.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = "1", MatchMode = Enums.EnumMatchMode.Equal
            });
            whereConditionArguments.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = "1,2,3,4", MatchMode = Enums.EnumMatchMode.In
            });
            WhereConditionArguments <TestClassA> w2 = new WhereConditionArguments <TestClassA>();

            w2.SpliceMode = Enums.EnumSpliceMode.OrElse;
            w2.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = "1", MatchMode = Enums.EnumMatchMode.Equal
            });
            w2.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.B), Value = "1,2,3,4", MatchMode = Enums.EnumMatchMode.In
            });
            whereConditionArguments.InnerWhereConditionArguments.Add(w2);


            Expression <Func <TestClassA, bool> > a = whereConditionArguments.GetWhereLambdaExpression();
        }
コード例 #2
0
        public void GetWhereExpressionTest()
        {
            WhereConditionArguments <TestClassA> whereConditionArguments = new WhereConditionArguments <TestClassA>();

            whereConditionArguments.SpliceMode = Enums.EnumSpliceMode.AndAlso;
            whereConditionArguments.WhereConditions.Add(new WhereCondition()
            {
                Key = "A24235634", Value = "", MatchMode = Enums.EnumMatchMode.Equal
            });
            whereConditionArguments.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = "1", MatchMode = Enums.EnumMatchMode.NotEqual
            });
            whereConditionArguments.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = new int[] { 1, 2, 3, 4 }, MatchMode = Enums.EnumMatchMode.In
            });
            WhereConditionArguments <TestClassA> w2 = new WhereConditionArguments <TestClassA>
            {
                SpliceMode = Enums.EnumSpliceMode.OrElse
            };

            w2.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.B), Value = "30", MatchMode = Enums.EnumMatchMode.NotEqual
            });
            w2.WhereConditions.Add(new WhereCondition()
            {
                Key = "DDDDD", Value = "", MatchMode = Enums.EnumMatchMode.In
            });
            w2.WhereConditions.Add(new WhereCondition()
            {
                Key = nameof(TestClassA.A), Value = new int[] { 1, 2, 3, 4 }, MatchMode = Enums.EnumMatchMode.In
            });
            whereConditionArguments.InnerWhereConditionArguments.Add(w2);

            whereConditionArguments = JsonConvert.DeserializeObject <WhereConditionArguments <TestClassA> >(JsonConvert.SerializeObject(whereConditionArguments));


            List <WhereConditionFilterAttribute> whereConditionFilterAttributes = new List <WhereConditionFilterAttribute>();

            whereConditionFilterAttributes.Add(new WhereConditionFilterAttribute(Enums.EnumMatchMode.All, nameof(TestClassA.A), nameof(TestClassA.B)));


            WhereConditionArgumentsInterpreter whereConditionArgumentsInterpret = new WhereConditionArgumentsInterpreter();

            _ = whereConditionArgumentsInterpret.ApplyFilter(whereConditionArguments, whereConditionFilterAttributes);


            WhereConditionArguments b = whereConditionArgumentsInterpret.Parse(whereConditionArguments);

            whereConditionArguments = b as WhereConditionArguments <TestClassA>;
            Expression <Func <TestClassA, bool> > c = whereConditionArguments.GetWhereLambdaExpression();
        }
コード例 #3
0
        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="whereConditionArguments"></param>
        /// <returns></returns>
        public virtual WhereConditionArguments Parse(WhereConditionArguments whereConditionArguments)
        {
            if (whereConditionArguments == null)
            {
                throw new ArgumentNullException(nameof(whereConditionArguments));
            }


            List <WhereCondition> removeList = new List <WhereCondition>();

            foreach (WhereCondition item in whereConditionArguments.WhereConditions.Where(item => !(item is null)))
            {
                WhereCondition whereCondition = item;
                if ((whereCondition.MatchMode & (EnumMatchMode.Between | EnumMatchMode.NotBetween | EnumMatchMode.In |
                                                 EnumMatchMode.NotIn)) == whereCondition.MatchMode)
                {
                    IEnumerable <object> targetList;
                    Type type = whereCondition.Value?.GetType();
                    if (type is null)
                    {
                        continue;
                    }

                    targetList = type == typeof(string)
                        ? JsonConvert.DeserializeObject <List <object> >(((string)whereCondition.Value)?.Trim())
                        : whereCondition.Value is IEnumerable array?array.Cast <object>() : null;

                    if (targetList == null ||
                        ((whereCondition.MatchMode & (EnumMatchMode.Between | EnumMatchMode.NotBetween)) ==
                         whereCondition.MatchMode && targetList.Count() != 2))
                    {
                        removeList.Add(item);
                    }

                    whereCondition.Value = targetList;
                }
                else if ((whereCondition.MatchMode & EnumMatchMode.Like) == EnumMatchMode.Like)
                {
                    //No conversion required
                }
            }

            foreach (WhereConditionArguments item in whereConditionArguments.InnerWhereConditionArguments)
            {
                if (item is WhereConditionArguments innerWhereConditionArguments)
                {
                    innerWhereConditionArguments = Parse(innerWhereConditionArguments);
                }
            }

            removeList.ForEach(item => whereConditionArguments.WhereConditions.Remove(item));
            return(whereConditionArguments);
        }
コード例 #4
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }
            string modelName = bindingContext.BinderModelName;

            if (string.IsNullOrEmpty(modelName))
            {
                modelName = bindingContext.ModelMetadata.Name;
            }
            ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                await Task.CompletedTask;
                return;
            }
            bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
            string requestString = valueProviderResult.FirstValue;

            if (string.IsNullOrEmpty(requestString))
            {
                await Task.CompletedTask;
                return;
            }
            WhereConditionArguments whereParameterArguments = JsonConvert.DeserializeObject(requestString, bindingContext.ModelType) as WhereConditionArguments;

            try
            {
                IEnumerable <WhereConditionFilterAttribute> whereParameterAttributes = ((DefaultModelMetadata)bindingContext.ModelMetadata).Attributes.ParameterAttributes
                                                                                       .Where(attr => attr.GetType() == typeof(WhereConditionFilterAttribute)).Select(attr => attr as WhereConditionFilterAttribute);
                WhereConditionArgumentsInterpreter whereConditionArgumentsInterpret = new WhereConditionArgumentsInterpreter();
                whereParameterArguments = whereConditionArgumentsInterpret.ApplyFilter(whereParameterArguments, whereParameterAttributes?.ToList());
                whereParameterArguments = whereConditionArgumentsInterpret.Parse(whereParameterArguments);
            }
            catch (Exception ex)
            {
                bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, ex.Message);
                await Task.CompletedTask;
                return;
            }

            bindingContext.Result = ModelBindingResult.Success(whereParameterArguments);
            await Task.CompletedTask;

            return;
        }
コード例 #5
0
        /// <summary>
        /// 应用过滤器
        /// </summary>
        /// <param name="whereConditionArguments">待处理参数</param>
        /// <param name="whereConditionFilterList">过滤器列表</param>
        /// <returns></returns>
        public virtual WhereConditionArguments ApplyFilter(WhereConditionArguments whereConditionArguments,
                                                           List <WhereConditionFilterAttribute> whereConditionFilterList = null)
        {
            if (whereConditionArguments == null)
            {
                throw new ArgumentNullException(nameof(whereConditionArguments));
            }

            //没有过滤器约束则不进行任何处理
            if (whereConditionFilterList == null || whereConditionFilterList.Count == 0)
            {
                return(whereConditionArguments);
            }

            List <WhereCondition> removeList = new List <WhereCondition>();

            foreach (WhereCondition item in whereConditionArguments.WhereConditions)
            {
                if (!(item is WhereCondition whereCondition))
                {
                    continue;
                }

                if (!whereConditionFilterList.Any(f
                                                  =>                                                                            //若当前被审查元素不满足以下条件,则应被过滤
                                                  (f.Keys == null || f.Keys.Contains(whereCondition.Key))                       //未定义或者已包含
                                                  &&
                                                  (f.AllowMatchPattern & whereCondition.MatchMode) == whereCondition.MatchMode) //允许的匹配模式能够匹配
                    )
                {
                    removeList.Add(item);
                }
            }

            foreach (WhereConditionArguments item in whereConditionArguments.InnerWhereConditionArguments)
            {
                if (item is WhereConditionArguments innerWhereConditionArguments)
                {
                    innerWhereConditionArguments =
                        ApplyFilter(innerWhereConditionArguments, whereConditionFilterList);
                }
            }

            removeList.ForEach(item => whereConditionArguments.WhereConditions.Remove(item));
            return(whereConditionArguments);
        }