Beispiel #1
0
        public static LambdaExpression BuildPredicate(this FilterGroup filterGroup, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroup == null)
            {
                throw new ArgumentNullException(nameof(filterGroup));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filterGroup, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            return(FilterExtensions.BuildPredicateInternal(filterGroup.Filters, type, arg));
        }
        public static LambdaExpression BuildPredicate(this Filter filter, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filter, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            return(Interpreter.BuildPredicate(filter.Operator.GetComparisonOperator(), arg.MapProperty(filter.Property), filter.Value, type, arg.VariableResolver));
        }
Beispiel #3
0
        public static EvaluationResult <T, bool> TryBuildPredicate <T>(this FilterGroup filterGroup, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroup == null)
            {
                throw new ArgumentNullException(nameof(filterGroup));
            }
            ;
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = typeof(T),
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(TryBuildPredicate <T>(filterGroup, arg));
        }
        public static EvaluationResult TryBuildPredicate(this Filter filter, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(TryBuildPredicate(filter, type, arg));
        }
Beispiel #5
0
        public static EvaluationResult <OrderByClause> TryBuildOrderByClause(this OrderByInfo orderByInfo, Type type, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (orderByInfo is null)
            {
                throw new ArgumentNullException(nameof(orderByInfo));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var arg = new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = type,
                PropertyMapping = propertyMapping
            };

            return(OrderByParser.Parse(new[] { orderByInfo }, arg));
        }
        public static EvaluationResult <OrderByClause> TryBuildOrderByClause(this string expression, Type type, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException($"{nameof(expression)} is required", nameof(expression));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var arg = new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = type,
                PropertyMapping = propertyMapping
            };

            return(OrderByParser.Parse(expression, arg));
        }
        public static LambdaExpression BuildPredicate(this IEnumerable <Filter> filters, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(BuildPredicateInternal(filters, type, arg));
        }
Beispiel #8
0
        public static LambdaExpression BuildPredicate(this IEnumerable <FilterGroup> filterGroups, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroups == null)
            {
                throw new ArgumentNullException(nameof(filterGroups));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filterGroups, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            var allFilterGroups = filterGroups as FilterGroup[] ?? filterGroups.ToArray();

            allFilterGroups = allFilterGroups.Where(p => p?.Filters?.Any() == true).ToArray();
            if (!allFilterGroups.Any())
            {
                return(FilterExtensions.AlwaysTruePredicate(type));
            }

            var param = type.CreateParameterExpression();
            var body  = allFilterGroups.Aggregate((Expression)null, (current, next) => current == null
                                ? FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)
                                : Expression.OrElse(current, FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)));

            return(Expression.Lambda(body, param));
        }
        public virtual Condition BuildCondition(Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                VariableResolver = variableResolver,
                EvaluationType   = type,
                PropertyMapping  = propertyMapping
            };

            var           error         = new ConditionBase.ErrorInfo();
            var           result        = new Condition();
            var           predicates    = new List <LambdaExpression>();
            var           exceptions    = new List <Exception>();
            OrderByClause orderByClause = null;

            if (Filters != null && Filters.Any())
            {
                var filtersResult = Filters.TryBuildPredicate(type, arg);
                if (filtersResult.Succeeded)
                {
                    predicates.Add(filtersResult.Result);
                }
                else
                {
                    if (filtersResult.Exception != null)
                    {
                        exceptions.Add(filtersResult.Exception);
                    }
                }
            }

            if (FilterGroups != null && FilterGroups.Any())
            {
                var filterGroupsResult = FilterGroups.TryBuildPredicate(type, arg);
                if (filterGroupsResult.Succeeded)
                {
                    predicates.Add(filterGroupsResult.Result);
                }
                else
                {
                    if (filterGroupsResult.Exception != null)
                    {
                        exceptions.Add(filterGroupsResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrEmpty(Where))
            {
                var whereResult = ExpressionParser.Parse(Where, type, arg);
                if (whereResult.Succeeded)
                {
                    predicates.Add(whereResult.Result);
                }
                else
                {
                    if (whereResult.Exception != null)
                    {
                        exceptions.Add(whereResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(OrderBy))
            {
                var orderByResult = OrderByParser.Parse(OrderBy, arg);
                if (orderByResult.Succeeded)
                {
                    orderByClause = orderByResult.Result;
                }
                else
                {
                    if (orderByResult.Exception != null)
                    {
                        exceptions.Add(orderByResult.Exception);
                    }
                }
            }
            else if (OrderBys != null && OrderBys.Any())
            {
                var orderBysResult = OrderByParser.Parse(OrderBys.ToArray(), arg);
                if (orderBysResult.Succeeded)
                {
                    orderByClause = orderBysResult.Result;
                }
                else
                {
                    if (orderBysResult.Exception != null)
                    {
                        exceptions.Add(orderBysResult.Exception);
                    }
                }
            }

            var isInvalid = arg.InvalidProperties.Any() || arg.InvalidOperators.Any() || arg.InvalidVariables.Any() || exceptions.Any();

            result.IsValid = !isInvalid;
            if (isInvalid)
            {
                error.EvaluationResult = new EvaluationResultBase
                {
                    InvalidProperties        = arg.InvalidProperties,
                    InvalidValues            = arg.InvalidValues,
                    InvalidOperators         = arg.InvalidOperators,
                    InvalidVariables         = arg.InvalidVariables,
                    InvalidOrderByDirections = arg.InvalidOrderByDirections
                };
                error.Exceptions = exceptions;
                result.Error     = error;
            }
            else
            {
                result.Predicates    = predicates;
                result.OrderByClause = orderByClause;
            }

            return(result);
        }
        internal static Expression BuildCondition(ParameterExpression param, Filter filter, BuildArgument arg)
        {
            var prop = param.CreatePropertyExpression(arg.MapProperty(filter.Property));

            return(Interpreter.BuildBody(filter.Operator.GetComparisonOperator(), prop, filter.Value, arg.VariableResolver));
        }
        internal static Expression BuildBody(Filter[] filters, ParameterExpression param, BuildArgument arg)
        {
            if (!filters.Any())
            {
                return(AlwaysTruePredicate(param.Type));
            }
            var condition = filters.Aggregate((Expression)null, (current, next) => current == null ? BuildCondition(param, next, arg) : Expression.AndAlso(current, BuildCondition(param, next, arg)));

            return(condition);
        }
 internal static bool IsValid(this IEnumerable <Filter> filters, BuildArgument arg)
 {
     return(filters != null && filters.All(p => IsValid(p, arg)));
 }
 internal static bool IsValid(this Filter filter, BuildArgument arg)
 {
     return(arg.IsValidProperty(filter.Property) &&
            !string.IsNullOrWhiteSpace(filter?.Operator) &&
            Interpreter.ComparisonOperators.Contains(filter.Operator.ToLower()));
 }
        internal static EvaluationResult TryBuildPredicate(this IEnumerable <Filter> filters, Type type, BuildArgument arg)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!filters.Any())
            {
                return new EvaluationResult
                       {
                           Succeeded = true,
                           Result    = AlwaysTruePredicate(type)
                       }
            }
            ;

            var allFilters = filters as Filter[] ?? filters.ToArray();

            foreach (var filter in allFilters)
            {
                arg.IsValidProperty(filter?.Property);

                arg.IsValidOperator(filter?.Operator);
            }

            if (arg.InvalidOperators.Any() || arg.InvalidProperties.Any())
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var param = type.CreateParameterExpression();
                var body  = BuildBody(allFilters, param, arg);
                return(new EvaluationResult
                {
                    Succeeded = true,
                    Result = Expression.Lambda(body, param)
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }
        internal static EvaluationResult TryBuildPredicate(this Filter filter, Type type, BuildArgument arg)
        {
            var isInvalid = !arg.IsValidProperty(filter.Property);

            isInvalid |= !arg.IsValidOperator(filter.Operator);
            if (isInvalid)
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var expression = Interpreter.BuildPredicate(filter.Operator.GetComparisonOperator(), arg.MapProperty(filter.Property), filter.Value, type, arg.VariableResolver);

                return(new EvaluationResult
                {
                    Result = expression,
                    Succeeded = true
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }
        internal static EvaluationResult <IOrderedQueryable> TryOrderBy(this IQueryable source, IEnumerable <OrderByInfo> orderByInfos, BuildArgument arg)
        {
            var evaluationResult = OrderByParser.Parse(orderByInfos.ToArray(), arg);

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedQueryable>
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedQueryable>
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
        internal static LambdaExpression BuildPredicateInternal(this IEnumerable <Filter> filters, Type type, BuildArgument arg)
        {
            if (!IsValid(filters, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            var allFilters = filters as Filter[] ?? filters.ToArray();

            if (allFilters.Length == 0)
            {
                return(AlwaysTruePredicate(type));
            }

            var param = type.CreateParameterExpression();
            var body  = BuildBody(allFilters, param, arg);

            return(Expression.Lambda(body, param));
        }
        internal static EvaluationResult <IOrderedQueryable> TryOrderBy(this IQueryable source, string expression, BuildArgument arg)
        {
            var evaluationResult = OrderByParser.Parse(expression, arg);

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedQueryable>
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedQueryable>
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
Beispiel #19
0
        internal static EvaluationResult <T, bool> TryBuildPredicate <T>(this FilterGroup filterGroup, BuildArgument arg)
        {
            filterGroup.Filters.ForEach(f => { arg.IsValidProperty(f.Property); arg.IsValidOperator(f.Operator); });
            if (arg.InvalidProperties.Any() || arg.InvalidOperators.Any())
            {
                return(new EvaluationResult <T, bool>
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var expression = (Expression <Func <T, bool> >)FilterExtensions.BuildPredicateInternal(filterGroup.Filters, typeof(T), arg);

                return(new EvaluationResult <T, bool>
                {
                    Result = expression,
                    Succeeded = true
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult <T, bool>
                {
                    Exception = ex
                });
            }
        }
Beispiel #20
0
 internal static bool IsValid(FilterGroup filterGroup, BuildArgument arg)
 {
     return(filterGroup != null &&
            filterGroup.Filters != null &&
            filterGroup.Filters.All(p => p.IsValid(arg)));
 }
Beispiel #21
0
        internal static EvaluationResult TryBuildPredicate(this IEnumerable <FilterGroup> filterGroups, Type type, BuildArgument arg)
        {
            if (!filterGroups.Any())
            {
                return new EvaluationResult
                       {
                           Succeeded = true,
                           Result    = FilterExtensions.AlwaysTruePredicate(type)
                       }
            }
            ;

            var allFilterGroups = filterGroups as FilterGroup[] ?? filterGroups.ToArray();

            foreach (var filterGroup in allFilterGroups)
            {
                if (filterGroup is null)
                {
                    throw new InvalidOperationException("FilterGroup can not be null");
                }
                if (filterGroup.Filters is null)
                {
                    throw new InvalidOperationException("Filters of FilterGroup can not be null");
                }

                foreach (var filter in filterGroup.Filters)
                {
                    arg.IsValidProperty(filter.Property);
                    arg.IsValidOperator(filter.Operator);
                }
            }

            if (arg.InvalidOperators.Any() || arg.InvalidProperties.Any())
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var param = type.CreateParameterExpression();
                var body  = allFilterGroups.Aggregate((Expression)null, (current, next) => current == null
                                        ? FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)
                                        : Expression.OrElse(current, FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)));
                return(new EvaluationResult
                {
                    Succeeded = true,
                    Result = Expression.Lambda(body, param)
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }