Example #1
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);
        }
Example #2
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);
        }