Advertisement
Nertip

Unique key from Expression<Func<TEntity, bool>>

Jun 27th, 2011
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. public static class KeyFromExpression
  2.     {
  3.         public static string GetCacheKey<TEntity>(this Expression<Func<TEntity, bool>> expression) where TEntity : class, IEntity
  4.         {
  5.             // We get the name of the class so it gets more unique
  6.             string key = typeof (TEntity).Name + " ";
  7.             if (expression.Body is BinaryExpression)
  8.             {
  9.                 //We get the binary expression from the expression body so we can get the values
  10.                 var binaryExpression = (BinaryExpression) expression.Body;
  11.                 key = binaryExpression.GetKey(key);
  12.             }
  13.             else if(expression.Body is MethodCallExpression)
  14.             {
  15.                 key += string.Format(" {0} ", ((MethodCallExpression)expression.Body).GetPropertyNameFromMethodCallExpression());
  16.             }
  17.             //We get rid of the trailing and double whitespace to make it look more clean
  18.             key = key.Replace("  ", " ").TrimEnd();
  19.  
  20.             return key;
  21.         }
  22.  
  23.         private static string GetPropertyNameFromMethodCallExpression(this MethodCallExpression methodCallExpression)
  24.         {
  25.             var name = string.Empty;
  26.             if (methodCallExpression.Object is MethodCallExpression)
  27.             {
  28.                 name = ((MethodCallExpression)methodCallExpression.Object).GetPropertyNameFromMethodCallExpression();
  29.             }
  30.             else if (methodCallExpression.Object is MemberExpression)
  31.             {
  32.                 name = ((MemberExpression) methodCallExpression.Object).GetValueFromMemberExpression();
  33.             }
  34.  
  35.             return name;
  36.         }
  37.  
  38.         private static string GetKey(this BinaryExpression binaryExpression, string key)
  39.         {
  40.             //if the nodetype on the right side of the expression not is a constant we will need to work on it in order to get the values
  41.             if (binaryExpression.Right.NodeType != ExpressionType.Constant)
  42.             {
  43.                 key += binaryExpression.Left.GetKeyFromBinaryExpressionOperand();
  44.                 //binaryExpression.NodeType here would return for example AndAlso, OrElse and so on.
  45.                 key += string.Format("{0} ", binaryExpression.NodeType);
  46.  
  47.                 key += binaryExpression.Right.GetKeyFromBinaryExpressionOperand();
  48.             }
  49.             //if the nodetype is an ExpresisonType of constant we can get the values
  50.             else
  51.             {
  52.                 //binaryExpression.Left returns for example "p.CommentId".
  53.                 //binaryExpression.NodeType returns for example "Equals", "NotEquals" and so on.
  54.                 //binaryExpression.Right contains the value for example "132"
  55.                 key += string.Format("{0} {1} {2} ",
  56.                                      binaryExpression.Left.GetValueFromBinaryExpressionOperands(),
  57.                                      binaryExpression.NodeType,
  58.                                      binaryExpression.Right.GetValueFromBinaryExpressionOperands());
  59.             }
  60.  
  61.             return key;
  62.         }
  63.  
  64.         private static string GetKeyFromBinaryExpressionOperand(this Expression expression)
  65.         {
  66.             var key = string.Empty;
  67.             if (expression is MemberExpression)
  68.                 key += string.Format(" {0} ", ((MemberExpression)expression).GetValueFromMemberExpression());
  69.             //If a method is being performed (ex. ToLower()) we just want the name and not the method
  70.             else if (expression is MethodCallExpression)
  71.             {
  72.                 //We cast the expression operand to MethodCallExpression so we can cast the Object to MemberExpression in which we can get the value
  73.                 key += string.Format(" {0} ", ((MethodCallExpression)expression).GetPropertyNameFromMethodCallExpression());
  74.             }
  75.             else
  76.             {
  77.                 //we cast the left side to a binary expression and call this method again so we can dig down the expression tree and get what we want
  78.                 var binaryExpressionNested = (BinaryExpression)expression;
  79.                 key = binaryExpressionNested.GetKey(key);
  80.             }
  81.  
  82.             return key;
  83.         }
  84.  
  85.         private static string GetValueFromMemberExpression(this MemberExpression expression)
  86.         {
  87.             if (expression.Expression is ConstantExpression)
  88.                 return ((FieldInfo)(expression.Member)).GetValue(((ConstantExpression)expression.Expression).Value).ToString();
  89.            
  90.             return expression.Member.Name;
  91.         }
  92.  
  93.         private static string GetValueFromBinaryExpressionOperands(this Expression expression)
  94.         {
  95.             string result;
  96.             if (expression is MemberExpression)
  97.                 result = ((MemberExpression)expression).GetValueFromMemberExpression();
  98.             else if (expression is ConstantExpression)
  99.             {
  100.                 var constExpression = (ConstantExpression)expression;
  101.                 result = constExpression.Value == null ? "null" : constExpression.Value.ToString();
  102.             }
  103.             else
  104.                 result = expression.ToString();
  105.  
  106.             return result;
  107.         }
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement