Пример #1
0
        private Expression PrepareExpression(out IQbservableProvider realProvider)
        {
            Contract.Ensures(Contract.ValueAtReturn(out realProvider) != null);
            Contract.Ensures(Contract.Result <Expression>() != null);

            Log.ServerReceivingExpression(clientId, Expression);

            var source = Provider.GetSource(argument);

            realProvider = source.Provider;

            ExpressionVisitor visitor;
            Expression        preparedExpression = null;

            if (!Provider.Options.AllowExpressionsUnrestricted)
            {
                visitor = new SecurityExpressionVisitor(Provider.Options);

                preparedExpression = visitor.Visit(Expression);
            }

            visitor = ReplaceConstantsVisitor.Create(
                typeof(QbservableSourcePlaceholder <TSource>),
                source,
                typeof(IQbservable <TSource>),
                (actualTypeInQuery, actualTypeInServer) =>
            {
                throw new InvalidOperationException("The client specified the wrong data type for the query." + Environment.NewLine
                                                    + "Client data type: " + actualTypeInQuery.FullName + Environment.NewLine
                                                    + "Actual data type: " + actualTypeInServer.FullName);
            });

            preparedExpression = visitor.Visit(preparedExpression ?? Expression);

            visitor = ReplaceConstantsVisitor.Create(
                typeof(DuplexCallback),
                (value, _) =>
            {
                var callback = (DuplexCallback)value;

                callback.SetServerProtocol(Provider.Protocol);

                return(callback);
            },
                type => type);

            preparedExpression = visitor.Visit(preparedExpression);

            foreach (var customVisitorFactory in Provider.Options.VisitorFactories)
            {
                var customVisitor = customVisitorFactory();

                preparedExpression = customVisitor.Visit(preparedExpression);
            }

            Log.ServerRewrittenExpression(clientId, preparedExpression);

            return(preparedExpression);
        }
Пример #2
0
        private Expression PrepareExpression(out IQbservableProvider realProvider)
        {
            QbservableProviderDiagnostics.DebugPrint(Expression, "TcpServerQuery Received Expression");

            var source = Provider.GetSource(argument);

            realProvider = source.Provider;

            ExpressionVisitor visitor;
            Expression        preparedExpression = null;

            if (!Provider.Options.AllowExpressionsUnrestricted)
            {
                visitor = new SecurityExpressionVisitor(Provider.Options);

                preparedExpression = visitor.Visit(Expression);
            }

            visitor = ReplaceConstantsVisitor.Create(
                typeof(QbservableSourcePlaceholder <TSource>),
                source,
                typeof(IQbservable <TSource>),
                (actualTypeInQuery, actualTypeInServer) =>
            {
                throw new InvalidOperationException("The client specified the wrong data type for the query." + Environment.NewLine
                                                    + "Client data type: " + actualTypeInQuery.FullName + Environment.NewLine
                                                    + "Actual data type: " + actualTypeInServer.FullName);
            });

            preparedExpression = visitor.Visit(preparedExpression ?? Expression);

            visitor = ReplaceConstantsVisitor.Create(
                typeof(DuplexCallback),
                (value, _) =>
            {
                var callback = (DuplexCallback)value;

                callback.SetServerProtocol(Provider.Protocol);

                return(callback);
            },
                type => type);

            preparedExpression = visitor.Visit(preparedExpression);

            QbservableProviderDiagnostics.DebugPrint(preparedExpression, "TcpServerQuery Rewritten Expression");

            return(preparedExpression);
        }
Пример #3
0
        public Expression PrepareExpression(IQbservableProtocol protocol)
        {
            Contract.Requires(protocol != null);
            Contract.Ensures(Contract.Result <Expression>() != null);

            Log.ClientSendingExpression(clientId, Expression);

            if (!Expression.Type.GetIsGenericType() ||
                (Expression.Type.GetGenericTypeDefinition() != typeof(IQbservable <>) &&
                 Expression.Type.GetGenericTypeDefinition() != typeof(ClientQuery <>)))
            {
                throw new InvalidOperationException("The query must end as an IQbservable<T>.");
            }

            var visitor = ReplaceConstantsVisitor.CreateForGenericTypeByDefinition(
                typeof(ClientQuery <>),
#if REFLECTION
                (_, actualType) => Activator.CreateInstance(typeof(QbservableSourcePlaceholder <>).MakeGenericType(actualType.GetGenericArguments()[0]), true),
#else
                (_, actualType) => Activator.CreateInstance(typeof(QbservableSourcePlaceholder <>).MakeGenericType(actualType.GetGenericArguments()[0])),
#endif
                type => typeof(IQbservable <>).MakeGenericType(type.GetGenericArguments()[0]));

            var result = visitor.Visit(Expression);

            if (visitor.ReplacedConstants == 0)
            {
                throw new InvalidOperationException("A queryable observable service was not found in the query.");
            }

            var evaluator = Provider.ClientEvaluator;

            if (!evaluator.IsKnownType(Provider.SourceType))
            {
                evaluator.AddKnownType(Provider.SourceType);
            }

            var evaluationVisitor = new LocalEvaluationVisitor(evaluator, protocol);

            var preparedExpression = evaluationVisitor.Visit(result);

            Contract.Assume(!evaluationVisitor.HasAnyExpectedTypes);

            Log.ClientRewrittenExpression(clientId, preparedExpression);

            return(preparedExpression);
        }
Пример #4
0
        public Expression PrepareExpression(QbservableProtocol protocol)
        {
            QbservableProviderDiagnostics.DebugPrint(Expression, "TcpClientQuery Original Expression");

            if (!Expression.Type.IsGenericType ||
                (Expression.Type.GetGenericTypeDefinition() != typeof(IQbservable <>) &&
                 Expression.Type.GetGenericTypeDefinition() != typeof(TcpClientQuery <>)))
            {
                throw new InvalidOperationException("The query must end as an IQbservable<T>.");
            }

            var visitor = ReplaceConstantsVisitor.CreateForGenericTypeByDefinition(
                typeof(TcpClientQuery <>),
                (_, actualType) => Activator.CreateInstance(typeof(QbservableSourcePlaceholder <>).MakeGenericType(actualType.GetGenericArguments()[0]), true),
                type => typeof(IQbservable <>).MakeGenericType(type.GetGenericArguments()[0]));

            var result = visitor.Visit(Expression);

            if (visitor.ReplacedConstants == 0)
            {
                throw new InvalidOperationException("A queryable observable service was not found in the query.");
            }

            var evaluator = Provider.LocalEvaluator;

            if (!evaluator.IsKnownType(Provider.SourceType))
            {
                evaluator.AddKnownType(Provider.SourceType);
            }

            var evaluationVisitor = new LocalEvaluationVisitor(evaluator, protocol);

            var preparedExpression = evaluationVisitor.Visit(result);

            QbservableProviderDiagnostics.DebugPrint(preparedExpression, "TcpClientQuery Rewritten Expression");

            return(preparedExpression);
        }