/// <summary>
        /// Initializes a new instance of the FilterBuildingExpressionVisitor
        /// class.
        /// </summary>
        /// <param name="contractResolver">
        /// The contract resolver to use to determine property
        /// names from members used within expressions.
        /// </param>
        private FilterBuildingExpressionVisitor(MobileServiceContractResolver contractResolver)
            : base()
        {
            Debug.Assert(contractResolver != null);

            this.contractResolver = contractResolver;
        }
        /// <summary>
        /// Translate an expression tree into a compiled OData query.
        /// </summary>
        /// <param name="expression">
        /// The expression tree.
        /// </param>
        /// <param name="contractResolver">
        /// The contract resolver used to determine property names from
        /// members used within expressions.
        /// </param>
        /// <returns>
        /// An OData query.
        /// </returns>
        public static QueryNode Compile(Expression expression, MobileServiceContractResolver contractResolver)
        {
            Arguments.IsNotNull(expression, nameof(expression));
            Arguments.IsNotNull(contractResolver, nameof(contractResolver));

            // Walk the expression tree and build the filter.
            FilterBuildingExpressionVisitor visitor = new FilterBuildingExpressionVisitor(contractResolver);

            visitor.Visit(expression);

            QueryNode node = visitor.filterExpression.FirstOrDefault();

            return(node);
        }
        /// <summary>
        /// Translate an expression tree into a compiled OData query.
        /// </summary>
        /// <param name="expression">
        /// The expression tree.
        /// </param>
        /// <param name="contractResolver">
        /// The contract resolver used to determine property names from
        /// members used within expressions.
        /// </param>
        /// <returns>
        /// An OData query.
        /// </returns>
        public static QueryNode Compile(Expression expression, MobileServiceContractResolver contractResolver)
        {
            Debug.Assert(expression != null);
            Debug.Assert(contractResolver != null);

            // Walk the expression tree and build the filter.
            FilterBuildingExpressionVisitor visitor = new FilterBuildingExpressionVisitor(contractResolver);

            visitor.Visit(expression);

            QueryNode node = visitor.filterExpression.FirstOrDefault();

            return(node);
        }
コード例 #4
0
        public void ResolveTableNameIsThreadSafe()
        {
            const int iterationCount = 100;

            for (int i = 0; i < iterationCount; i++)
            {
                MobileServiceContractResolver contractResolver = new MobileServiceContractResolver();
                Action resolveTableName = () => contractResolver.ResolveTableName(typeof(PocoType));

                Task t1 = Task.Run(resolveTableName);
                Task t2 = Task.Run(resolveTableName);
                Task.WhenAll(t1, t2).Wait();
            }
        }
        public void ResolveTableNameIsThreadSafe()
        {
            const int iterationCount = 100;

            for (int i = 0; i < iterationCount; i++)
            {
                MobileServiceContractResolver contractResolver = new MobileServiceContractResolver();
                Action resolveTableName = () => contractResolver.ResolveTableName(typeof(PocoType));                

                Task t1 = Task.Run(resolveTableName);
                Task t2 = Task.Run(resolveTableName);
                Task.WhenAll(t1, t2).Wait();                
            }
        }
        /// <summary>
        /// Get the table member name referenced by an expression or return null.
        /// </summary>
        /// <param name="expression">
        /// The expression to check.
        /// </param>
        /// <param name="contractResolver">
        /// The contract resolver to use to determine property names from members used
        /// within expressions.
        /// </param>
        /// <returns>
        /// The table member name or null.
        /// </returns>
        public static string GetTableMemberName(Expression expression,
                                                MobileServiceContractResolver contractResolver)
        {
            Arguments.IsNotNull(expression, nameof(expression));
            Arguments.IsNotNull(contractResolver, nameof(contractResolver));

            // Only parameter references are valid in a query (any other
            // references should have been partially evaluated away)
            if (expression is MemberExpression member &&
                member.Expression != null &&
                member.Expression.NodeType == ExpressionType.Parameter &&
                member.Member != null)
            {
                // Lookup the Mobile Services name of the member and use that
                JsonProperty property = contractResolver.ResolveProperty(member.Member);
                return(property.PropertyName);
            }

            // Otherwise return null
            return(null);
        }
        public void ResolveTableName()
        {
            MobileServiceContractResolver contractResolver = new MobileServiceContractResolver();

            List<Tuple<Type, string>> testCases = new List<Tuple<Type, string>>() {
                new Tuple<Type, string>(typeof(PocoType), "PocoType"),
                new Tuple<Type, string>(typeof(DataContractType), "DataContractNameFromAttributeType"),
                new Tuple<Type, string>(typeof(DataTableType), "NamedDataTableType"),
                new Tuple<Type, string>(typeof(JsonContainerType), "NamedJsonContainerType"),
                new Tuple<Type, string>(typeof(UnnamedJsonContainerType), "UnnamedJsonContainerType"),
            };

            foreach (var testCase in testCases)
            {
                var input = testCase.Item1;
                string expected = testCase.Item2;

                var actual = contractResolver.ResolveTableName(input);

                Assert.AreEqual(actual, expected);
            }
        }
コード例 #8
0
        public void ResolveTableName()
        {
            MobileServiceContractResolver contractResolver = new MobileServiceContractResolver();

            List <Tuple <Type, string> > testCases = new List <Tuple <Type, string> >()
            {
                new Tuple <Type, string>(typeof(PocoType), "PocoType"),
                new Tuple <Type, string>(typeof(DataContractType), "DataContractNameFromAttributeType"),
                new Tuple <Type, string>(typeof(DataTableType), "NamedDataTableType"),
                new Tuple <Type, string>(typeof(JsonContainerType), "NamedJsonContainerType"),
                new Tuple <Type, string>(typeof(UnnamedJsonContainerType), "UnnamedJsonContainerType"),
            };

            foreach (var testCase in testCases)
            {
                var    input    = testCase.Item1;
                string expected = testCase.Item2;

                var actual = contractResolver.ResolveTableName(input);

                Assert.AreEqual(actual, expected);
            }
        }
 /// <summary>
 /// Initializes a new instance of the FilterBuildingExpressionVisitor
 /// class.
 /// </summary>
 /// <param name="contractResolver">
 /// The contract resolver to use to determine property
 /// names from members used within expressions.
 /// </param>
 private FilterBuildingExpressionVisitor(MobileServiceContractResolver contractResolver)
     : base()
 {
     Arguments.IsNotNull(contractResolver, nameof(contractResolver));
     this.contractResolver = contractResolver;
 }
        /// <summary>
        /// Get the table member name referenced by an expression or return null.
        /// </summary>
        /// <param name="expression">
        /// The expression to check.
        /// </param>
        /// <param name="contractResolver">
        /// The contract resolver to use to determine property names from members used 
        /// within expressions.
        /// </param>
        /// <returns>
        /// The table member name or null.
        /// </returns>
        public static string GetTableMemberName(Expression expression,
                                                  MobileServiceContractResolver contractResolver)
        {
            Debug.Assert(expression != null);
            Debug.Assert(contractResolver != null);

            // Only parameter references are valid in a query (any other
            // references should have been partially evaluated away)
            MemberExpression member = expression as MemberExpression;
            if (member != null &&
                member.Expression != null &&
                member.Expression.NodeType == ExpressionType.Parameter &&
                member.Member != null)
            {
                // Lookup the Mobile Services name of the member and use that
                JsonProperty property = contractResolver.ResolveProperty(member.Member);
                return property.PropertyName;
            }

            // Otherwise return null
            return null;
        }
        /// <summary>
        /// Translate an expression tree into a compiled OData query.
        /// </summary>
        /// <param name="expression">
        /// The expression tree.
        /// </param>
        /// <param name="contractResolver">
        /// The contract resolver used to determine property names from
        /// members used within expressions.
        /// </param>
        /// <returns>
        /// An OData query.
        /// </returns>
        public static QueryNode Compile(Expression expression, MobileServiceContractResolver contractResolver)
        {
            Debug.Assert(expression != null);
            Debug.Assert(contractResolver != null);
                    
            // Walk the expression tree and build the filter.
            FilterBuildingExpressionVisitor visitor = new FilterBuildingExpressionVisitor(contractResolver);
            visitor.Visit(expression);

            QueryNode node = visitor.filterExpression.FirstOrDefault();
            return node;
        }        
        /// <summary>
        /// Initializes a new instance of the FilterBuildingExpressionVisitor
        /// class.
        /// </summary>
        /// <param name="contractResolver">
        /// The contract resolver to use to determine property 
        /// names from members used within expressions.
        /// </param>
        private FilterBuildingExpressionVisitor(MobileServiceContractResolver contractResolver)
            : base()
        {
            Debug.Assert(contractResolver != null);

            this.contractResolver = contractResolver;
        }