Skip to content

Commit

Permalink
Switch .Count() accessors to using RegisterTemplateMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenniak committed Oct 26, 2014
1 parent ee40b30 commit 58dd786
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public void RegisterUnaryExpressionMapping(Type operandType, ExpressionType expr

public void RegisterMemberAccessMapping(Type targetType, string memberName, ExpressionMappingDelegate<MemberExpression> memberAccessMapping)
{
if (targetType.IsGenericType)
targetType = targetType.GetGenericTypeDefinition();

memberAccessMappingRegistry[Tuple.Create(targetType, memberName)] = memberAccessMapping;
}

Expand Down
38 changes: 14 additions & 24 deletions rethinkdb-net/Expressions/LinqExpressionConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory
var whereDelegate = (WhereDelegate<int>)Enumerable.Where;
expressionConverterFactory.RegisterMethodCallMapping(whereDelegate.Method, ConvertEnumerableWhereToTerm);

var countDelegate = (CountDelegate<int>)Enumerable.Count;
expressionConverterFactory.RegisterMethodCallMapping(countDelegate.Method, ConvertEnumerableCountToTerm);

expressionConverterFactory.RegisterMemberAccessMapping(typeof(List<>), "Count", ConvertCountMemberToTerm);
expressionConverterFactory.RegisterMemberAccessMapping(typeof(ICollection<>), "Count", ConvertCountMemberToTerm);
expressionConverterFactory.RegisterTemplateMapping<IEnumerable<int>, int>(
list => list.Count(),
list => Count(list)
);
expressionConverterFactory.RegisterTemplateMapping<List<int>, int>(
list => list.Count,
list => Count(list)
);
expressionConverterFactory.RegisterTemplateMapping<ICollection<int>, int>(
list => list.Count,
list => Count(list)
);
}

public static Term ConvertCountMemberToTerm(MemberExpression memberExpression, DefaultExpressionConverterFactory.RecursiveMapDelegate recursiveMap, IDatumConverterFactory datumConverterFactory, IExpressionConverterFactory expressionConverterFactory)
private static Term Count(Term term)
{
var countTerm = new Term()
{
type = Term.TermType.COUNT,
};
countTerm.args.Add(recursiveMap(memberExpression.Expression));
return countTerm;
return new Term() { type = Term.TermType.COUNT, args = { term } };
}

public static Term ConvertAppendToTerm(MethodCallExpression methodCall, DefaultExpressionConverterFactory.RecursiveMapDelegate recursiveMap, IDatumConverterFactory datumConverterFactory, IExpressionConverterFactory expressionConverterFactory)
Expand Down Expand Up @@ -93,17 +95,5 @@ public static Term ConvertEnumerableWhereToTerm(MethodCallExpression methodCall,

return filterTerm;
}

public static Term ConvertEnumerableCountToTerm(MethodCallExpression methodCall, DefaultExpressionConverterFactory.RecursiveMapDelegate recursiveMap, IDatumConverterFactory datumConverterFactory, IExpressionConverterFactory expressionConverterFactory)
{
var target = methodCall.Arguments[0];

var countTerm = new Term()
{
type = Term.TermType.COUNT,
};
countTerm.args.Add(recursiveMap(target));
return countTerm;
}
}
}

0 comments on commit 58dd786

Please sign in to comment.