Example #1
0
            private Expression Process(Expression visited, Expression processed)
            {
                if (this.processor == null)
                {
                    this.processor = this.context.QueryContext.GetApiService <IQueryExpressionProcessor>();
                }

                if (this.processor != null)
                {
                    var filtered = processor.Process(this.context);
                    var callback = this.context.AfterNestedVisitCallback;
                    this.context.AfterNestedVisitCallback = null;
                    if (filtered != null && filtered != visited)
                    {
                        if (!visited.Type.IsAssignableFrom(filtered.Type))
                        {
                            // In order to filter on the navigation properties,
                            // the type is changed from ICollection<> to IQueryable<>
                            var collectionType = visited.Type.FindGenericType(typeof(ICollection <>));
                            var queryableType  = filtered.Type.FindGenericType(typeof(IQueryable <>));
                            if (collectionType == null || queryableType == null)
                            {
                                throw new InvalidOperationException(
                                          Resources.ProcessorCannotChangeExpressionType);
                            }

                            var queryableElementType  = queryableType.GenericTypeArguments[0];
                            var collectionElementType = collectionType.GenericTypeArguments[0];
                            if (collectionElementType != queryableElementType &&
                                !queryableElementType.IsAssignableFrom(collectionElementType))
                            {
                                throw new InvalidOperationException(
                                          Resources.ProcessorCannotChangeExpressionType);
                            }
                        }

                        this.processedExpressions.Add(visited, processed);
                        this.context.PushVisitedNode(null);
                        try
                        {
                            processed = this.Visit(filtered);
                        }
                        finally
                        {
                            this.context.PopVisitedNode();
                            this.processedExpressions.Remove(visited);
                        }

                        if (callback != null)
                        {
                            callback();
                        }
                    }
                }

                return(processed);
            }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the DefaultQueryHandler class.
        /// </summary>
        /// <param name="sourcer">The query expression sourcer to use.</param>
        /// <param name="authorizer">The query expression authorizer to use.</param>
        /// <param name="expander">The query expression expander to use.</param>
        /// <param name="processor">The query expression processor to use.</param>
        public DefaultQueryHandler(IQueryExpressionSourcer sourcer,
                                   IQueryExpressionAuthorizer authorizer = null,
                                   IQueryExpressionExpander expander     = null,
                                   IQueryExpressionProcessor processor   = null)
        {
            Ensure.NotNull(sourcer, nameof(sourcer));

            this.authorizer = authorizer;
            this.expander   = expander;
            this.processor  = processor;
            this.sourcer    = sourcer;
        }
Example #3
0
            public QueryExpressionVisitor(QueryContext context,
                                          IQueryExpressionSourcer sourcer,
                                          IQueryExpressionAuthorizer authorizer = null,
                                          IQueryExpressionExpander expander     = null,
                                          IQueryExpressionProcessor processor   = null)
            {
                Ensure.NotNull(sourcer, nameof(sourcer));

                this.context         = new QueryExpressionContext(context);
                this.authorizer      = authorizer;
                this.expander        = expander;
                this.processor       = processor;
                this.sourcer         = sourcer;
                processedExpressions = new Dictionary <Expression, Expression>();
            }
            private Expression Process(Expression visited, Expression processed)
            {
                if (this.processor == null)
                {
                    this.processor = this.context.QueryContext.GetApiService<IQueryExpressionProcessor>();
                }

                if (this.processor != null)
                {
                    var filtered = processor.Process(this.context);
                    var callback = this.context.AfterNestedVisitCallback;
                    this.context.AfterNestedVisitCallback = null;
                    if (filtered != null && filtered != visited)
                    {
                        if (!visited.Type.IsAssignableFrom(filtered.Type))
                        {
                            // In order to filter on the navigation properties,
                            // the type is changed from ICollection<> to IQueryable<>
                            var collectionType = visited.Type.FindGenericType(typeof(ICollection<>));
                            var queryableType = filtered.Type.FindGenericType(typeof(IQueryable<>));
                            if (collectionType == null || queryableType == null)
                            {
                                throw new InvalidOperationException(
                                    Resources.ProcessorCannotChangeExpressionType);
                            }

                            var queryableElementType = queryableType.GenericTypeArguments[0];
                            var collectionElementType = collectionType.GenericTypeArguments[0];
                            if (collectionElementType != queryableElementType
                                && !queryableElementType.IsAssignableFrom(collectionElementType))
                            {
                                throw new InvalidOperationException(
                                    Resources.ProcessorCannotChangeExpressionType);
                            }
                        }

                        this.processedExpressions.Add(visited, processed);
                        this.context.PushVisitedNode(null);
                        try
                        {
                            processed = this.Visit(filtered);
                        }
                        finally
                        {
                            this.context.PopVisitedNode();
                            this.processedExpressions.Remove(visited);
                        }

                        if (callback != null)
                        {
                            callback();
                        }
                    }
                }

                return processed;
            }