Example #1
0
        public bool VertifyRule(TRule rule, out IList <Exception> exceptions)
        {
            string cacheKey = GetCacheKey(rule);

            if (_cache.ContainsKey(cacheKey))
            {
                exceptions = null;
                return(true);
            }
            var visitResult = DataPrivilegeVisitor.Visit(rule.ConditionExpression);

            exceptions = visitResult.Exceptions;
            if (visitResult.Success)
            {
                ReplacementVisitor visitor = new ReplacementVisitor(Expression.Constant(this), visitResult.PredicateExpression.Parameters[0], _parameterExpression);
                var newExpression          = visitor.Visit(visitResult.PredicateExpression);
                if (newExpression.CanReduce)
                {
                    newExpression = newExpression.Reduce();
                }
                DataPrivilegeInfo <TEntity> result = new DataPrivilegeInfo <TEntity>(newExpression as Expression <Func <TEntity, bool> >, visitResult.CustomFields);

                _cache.TryAdd(cacheKey, result);
            }
            return(visitResult.Success);
        }
Example #2
0
        private Expression GetExpressionBodyReferencingValue(LambdaExpression expression, Expression newParameterExpression)
        {
            var parameterToBeReplaced = expression.Parameters[0];
            var visitor = new ReplacementVisitor(parameterToBeReplaced, newParameterExpression);

            return(visitor.Visit(expression.Body));
        }
Example #3
0
        static Expression <Func <bool> > Substitute <TException>(Expression <Func <TException, bool> > expression, TException exception)
        {
            var parameter = expression.Parameters[0];
            var constant  = Expression.Constant(exception, typeof(TException));
            var visitor   = new ReplacementVisitor(parameter, constant);
            var newBody   = visitor.Visit(expression.Body);

            return(Expression.Lambda <Func <bool> >(newBody));
        }
Example #4
0
        private Expression <TLambda> Curry <TLambda>(
            LambdaExpression searchExpression,
            int replacedParameterIndex,
            object replacement)
        {
            var parameter = searchExpression.Parameters[replacedParameterIndex];
            var constant  = Expression.Constant(replacement, parameter.Type);
            var visitor   = new ReplacementVisitor(parameter, constant);
            var newBody   = visitor.Visit(searchExpression.Body);
            var lambda    = Expression.Lambda <TLambda>(newBody, searchExpression.Parameters.Except(new[] { parameter }));

            return(lambda);
        }
    public static Expression Replace(
        LambdaExpression source,
        Expression toFind,
        Expression toReplace)
    {
        var visitor = new ReplacementVisitor
        {
            SourceParameters = source.Parameters,
            ToFind           = toFind,
            ToReplace        = toReplace,
        };

        return(visitor.Visit(source.Body));
    }
        public static Expression Transform(
            LambdaExpression source,
            Expression find,
            Expression replace)
        {
            var visitor = new ReplacementVisitor
            {
                Parameters = source.Parameters,
                Find       = find,
                Replace    = replace,
            };

            return(visitor.Visit(source.Body));
        }
Example #7
0
        private DataPrivilegeInfo <TEntity> GetDataPrevilegeInfoByRule(TRule rule)
        {
            string cacheKey = GetCacheKey(rule);

            return(_cache.GetOrAdd(cacheKey, _ =>
            {
                var visitResult = DataPrivilegeVisitor.Visit(rule.ConditionExpression);
                if (!visitResult.Success)
                {
                    throw new AggregateException(visitResult.Exceptions);
                }
                ReplacementVisitor visitor = new ReplacementVisitor(Expression.Constant(this), visitResult.PredicateExpression.Parameters[0], _parameterExpression);
                var newExpression = visitor.Visit(visitResult.PredicateExpression);
                DataPrivilegeInfo <TEntity> result = new DataPrivilegeInfo <TEntity>(newExpression as Expression <Func <TEntity, bool> >, visitResult.CustomFields);
                return result;
            }));
        }