Advertisement
Guest User

Strongly-typed Expand and async queries.

a guest
Oct 13th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6.  
  7. namespace System.Data.Services.Client
  8. {
  9.     public static class AsyncRequests
  10.     {
  11.         public static Task<IEnumerable<T>> WhereAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
  12.         {
  13.             var task = new Task<IEnumerable<T>>(
  14.                 () =>
  15.                 {
  16.                     T[] result = null;
  17.                     if (predicate != null)
  18.                         result = collection.Where(predicate).ToArray();
  19.                     else
  20.                         result = collection.ToArray();
  21.                     return result;
  22.                 }
  23.                 );
  24.             task.Start();
  25.             return task;
  26.         }
  27.  
  28.         public static Task<T> FirstOrDefaultAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
  29.         {
  30.             var task = new Task<T>(
  31.                 () =>
  32.                 {
  33.                     return collection.Where(predicate).FirstOrDefault();
  34.                 }
  35.                 );
  36.             task.Start();
  37.             return task;
  38.         }
  39.  
  40.         public static Task<int> CountAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
  41.         {
  42.             var task = new Task<int>(
  43.                 () =>
  44.                 {
  45.                     return collection.Count(predicate);
  46.                 }
  47.                 );
  48.             task.Start();
  49.             return task;
  50.         }
  51.     }
  52.  
  53.     // http://blogs.msdn.com/b/stuartleeks/archive/2008/09/15/dataservicequery-t-expand.aspx
  54.     public static class DataServiceQueryExtensions
  55.     {
  56.         /// <summary>
  57.         /// Тёплый ламповый типизированный инклуд.
  58.         /// </summary>
  59.         public static DataServiceQuery<TSource> Include<TSource, TPropType>(this DataServiceQuery<TSource> source, Expression<Func<TSource, TPropType>> propertySelector)
  60.         {
  61.             string expandString = BuildString(propertySelector);
  62.             return source.Expand(expandString);
  63.         }
  64.         private static string BuildString(Expression propertySelector)
  65.         {
  66.             switch (propertySelector.NodeType)
  67.             {
  68.                 case ExpressionType.Lambda:
  69.                     LambdaExpression lambdaExpression = (LambdaExpression)propertySelector;
  70.                     return BuildString(lambdaExpression.Body);
  71.  
  72.                 case ExpressionType.Quote:
  73.                     UnaryExpression unaryExpression = (UnaryExpression)propertySelector;
  74.                     return BuildString(unaryExpression.Operand);
  75.  
  76.                 case ExpressionType.MemberAccess:
  77.                     MemberExpression expression = (MemberExpression)propertySelector;
  78.                     string resultString = expression.Member.Name;
  79.                     string expressionString = BuildString(expression.Expression);
  80.                     if (!string.IsNullOrEmpty(expressionString))
  81.                         resultString = expressionString + "/" + resultString;
  82.                     return resultString;
  83.  
  84.                 case ExpressionType.Call:
  85.                     MethodCallExpression methodCallExpression = (MethodCallExpression)propertySelector;
  86.                     if (IsSelect(methodCallExpression.Method))
  87.                         return BuildString(methodCallExpression.Arguments[0]) + "/" + BuildString(methodCallExpression.Arguments[1]);
  88.                     break;
  89.  
  90.                 case ExpressionType.Parameter:
  91.                     return null;
  92.             }
  93.             throw new InvalidOperationException("Expression must be a member expression or an Select call: " + propertySelector.ToString());
  94.         }
  95.  
  96.         private static readonly MethodInfo[] SelectMethods;
  97.         static DataServiceQueryExtensions()
  98.         {
  99.             Type type = typeof(System.Linq.Enumerable);
  100.             SelectMethods = type.GetMethods().Where(mi => mi.Name == "Select").ToArray();
  101.         }
  102.         private static bool IsSelect(MethodInfo methodInfo)
  103.         {
  104.             if (methodInfo.IsGenericMethod)
  105.             {
  106.                 if (!methodInfo.IsGenericMethodDefinition)
  107.                 {
  108.                     methodInfo = methodInfo.GetGenericMethodDefinition();
  109.                 }
  110.             }
  111.             return SelectMethods.Contains(methodInfo);
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement