Exemple #1
0
        public Expression GetInstancePermissionResolverExpression(TContext context, Expression instanceExpression)
        {
            if (!InstancePermissionResolverExpressions.Any())
            {
                return(null);
            }
            var generatedResolvers = InstancePermissionResolverExpressions.Select(resolver => {
                var contextParameter             = resolver.Parameters[0];
                var entityParameter              = resolver.Parameters[1];
                var replacementExpressionVisitor = new ReplacementExpressionVisitor(new Dictionary <Expression, Expression>()
                {
                    { contextParameter, Expression.Constant(context) },
                    { entityParameter, instanceExpression }
                });
                return(replacementExpressionVisitor.Visit(resolver.Body));
            });

            var firstExpression = generatedResolvers.First();

            if (!generatedResolvers.Skip(1).Any())
            {
                return(firstExpression);
            }
            return(generatedResolvers.Skip(1).Aggregate(firstExpression, (expression, resolver) => Expression.And(expression, resolver)));
        }
Exemple #2
0
        public Expression GetInstanceCustomResolverExpression(TContext context, Expression instanceExpression, object key)
        {
            ICollection <Expression <Func <TContext, T, object> > > resolvers = null;

            if (!InstanceCustomResolverExpressions.TryGetValue(key, out resolvers) || !resolvers.Any())
            {
                return(null);
            }
            var generatedResolvers = resolvers.Select(resolver => {
                var contextParameter             = resolver.Parameters[0];
                var entityParameter              = resolver.Parameters[1];
                var replacementExpressionVisitor = new ReplacementExpressionVisitor(new Dictionary <Expression, Expression>()
                {
                    { contextParameter, Expression.Constant(context) },
                    { entityParameter, instanceExpression }
                });
                return(replacementExpressionVisitor.Visit(resolver.Body));
            });

            var firstExpression = generatedResolvers.First();

            if (!generatedResolvers.Skip(1).Any())
            {
                return(firstExpression);
            }
            Func <Expression, Expression, Expression> reducer = null;

            if (!InstanceCustomResolverExpressionReducers.TryGetValue(key, out reducer))
            {
                reducer = DefaultExpressionReducer;
            }
            return(generatedResolvers.Skip(1).Aggregate(firstExpression, (expression, resolver) => reducer(expression, resolver)));
        }
        // <summary>
        // Use the information stored on the scratchpad to compile an immutable factory used
        // to construct the coordinators used at runtime when materializing results.
        // </summary>
        internal CoordinatorFactory Compile()
        {
            RecordStateFactory[] recordStateFactories;
            if (null != _recordStateScratchpads)
            {
                recordStateFactories = new RecordStateFactory[_recordStateScratchpads.Count];
                for (var i = 0; i < recordStateFactories.Length; i++)
                {
                    recordStateFactories[i] = _recordStateScratchpads[i].Compile();
                }
            }
            else
            {
                recordStateFactories = new RecordStateFactory[0];
            }

            var nestedCoordinators = new CoordinatorFactory[_nestedCoordinatorScratchpads.Count];

            for (var i = 0; i < nestedCoordinators.Length; i++)
            {
                nestedCoordinators[i] = _nestedCoordinatorScratchpads[i].Compile();
            }

            // compile inline delegates
            var replacementVisitor = new ReplacementExpressionVisitor(null, _inlineDelegates);
            var element            = replacementVisitor.Visit(Element);

            // substitute expressions that have error handlers into a new expression (used
            // when a more detailed exception message is needed)
            replacementVisitor = new ReplacementExpressionVisitor(_expressionWithErrorHandlingMap, _inlineDelegates);
            var elementWithErrorHandling = replacementVisitor.Visit(Element);

            var result =
                (CoordinatorFactory)Activator.CreateInstance(
                    typeof(CoordinatorFactory <>).MakeGenericType(_elementType), new object[]
            {
                Depth,
                StateSlotNumber,
                HasData,
                SetKeys,
                CheckKeys,
                nestedCoordinators,
                element,
                elementWithErrorHandling,
                InitializeCollection,
                recordStateFactories
            });

            return(result);
        }
        /// <summary>
        ///     Use the information stored on the scratchpad to compile an immutable factory used
        ///     to construct the coordinators used at runtime when materializing results.
        /// </summary>
        internal CoordinatorFactory Compile()
        {
            RecordStateFactory[] recordStateFactories;
            if (null != _recordStateScratchpads)
            {
                recordStateFactories = new RecordStateFactory[_recordStateScratchpads.Count];
                for (var i = 0; i < recordStateFactories.Length; i++)
                {
                    recordStateFactories[i] = _recordStateScratchpads[i].Compile();
                }
            }
            else
            {
                recordStateFactories = new RecordStateFactory[0];
            }

            var nestedCoordinators = new CoordinatorFactory[_nestedCoordinatorScratchpads.Count];
            for (var i = 0; i < nestedCoordinators.Length; i++)
            {
                nestedCoordinators[i] = _nestedCoordinatorScratchpads[i].Compile();
            }

            // compile inline delegates
            var replacementVisitor = new ReplacementExpressionVisitor(null, _inlineDelegates);
            var element = replacementVisitor.Visit(Element);

            // substitute expressions that have error handlers into a new expression (used
            // when a more detailed exception message is needed)
            replacementVisitor = new ReplacementExpressionVisitor(_expressionWithErrorHandlingMap, _inlineDelegates);
            var elementWithErrorHandling = replacementVisitor.Visit(Element);

            var result =
                (CoordinatorFactory)Activator.CreateInstance(
                    typeof(CoordinatorFactory<>).MakeGenericType(_elementType), new object[]
                        {
                            Depth,
                            StateSlotNumber,
                            HasData,
                            SetKeys,
                            CheckKeys,
                            nestedCoordinators,
                            element,
                            elementWithErrorHandling,
                            InitializeCollection,
                            recordStateFactories
                        });
            return result;
        }