Skip to content

Commit 48d8337

Browse files
committed
Added DefaultTranslators support
1 parent 12e3eae commit 48d8337

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using MongoFramework.Infrastructure.Querying.Translators;
5+
6+
namespace MongoFramework.Infrastructure.Querying
7+
{
8+
public static class DefaultTranslators
9+
{
10+
public static void AddTranslators()
11+
{
12+
ExpressionTranslation.AddTranslator(new WhereTranslator());
13+
ExpressionTranslation.AddTranslator(new OrderByTranslator());
14+
ExpressionTranslation.AddTranslator(new SelectTranslator());
15+
}
16+
}
17+
}

src/MongoFramework/Infrastructure/Querying/ExpressionTranslation.cs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,67 @@ public static class ExpressionTranslation
4949

5050
static ExpressionTranslation()
5151
{
52-
AddTranslator(new WhereTranslator(), WhereTranslator.GetSupportedMethods());
53-
AddTranslator(new OrderByTranslator(), OrderByTranslator.GetSupportedMethods());
54-
AddTranslator(new SelectTranslator(), SelectTranslator.GetSupportedMethods());
52+
DefaultTranslators.AddTranslators();
5553
}
5654

57-
public static void AddTranslator(IMethodTranslator translator, IEnumerable<MethodInfo> methods)
55+
public static void AddTranslator(IQueryTranslator translator)
5856
{
59-
lock (MethodTranslatorMap)
57+
if (translator is IMethodTranslator methodTranslator)
6058
{
61-
foreach (var method in methods)
59+
lock (MethodTranslatorMap)
6260
{
63-
MethodTranslatorMap.Add(method, translator);
61+
foreach (var method in methodTranslator.GetSupportedMethods())
62+
{
63+
MethodTranslatorMap.Add(method, methodTranslator);
64+
}
6465
}
6566
}
66-
}
67-
68-
public static void AddTranslator(IMemberTranslator translator, IEnumerable<MemberInfo> members)
69-
{
70-
lock (MemberTranslatorMap)
67+
else if (translator is IMemberTranslator memberTranslator)
7168
{
72-
foreach (var member in members)
69+
lock (MemberTranslatorMap)
7370
{
74-
MemberTranslatorMap.Add(member, translator);
71+
foreach (var member in memberTranslator.GetSupportedMembers())
72+
{
73+
MemberTranslatorMap.Add(member, memberTranslator);
74+
}
7575
}
7676
}
77+
else if (translator is IBinaryExpressionTranslator binaryExpressionTranslator)
78+
{
79+
lock (BinaryTranslatorMap)
80+
{
81+
foreach (var expressionType in binaryExpressionTranslator.GetSupportedExpressionTypes())
82+
{
83+
if (DefaultSupportedTypes.Contains(expressionType))
84+
{
85+
throw new ArgumentException($"{expressionType} is a default expression type and can not have a custom translator");
86+
}
87+
88+
BinaryTranslatorMap.Add(expressionType, binaryExpressionTranslator);
89+
}
90+
}
91+
}
92+
else
93+
{
94+
throw new ArgumentException($"Invalid type of translator. It must implement {nameof(IMethodTranslator)}, {nameof(IMemberTranslator)} or {nameof(IBinaryExpressionTranslator)}.", nameof(translator));
95+
}
7796
}
7897

79-
public static void AddTranslator(IBinaryExpressionTranslator translator, IEnumerable<ExpressionType> expressionTypes)
98+
public static void ClearTranslators()
8099
{
100+
lock (MethodTranslatorMap)
101+
{
102+
MethodTranslatorMap.Clear();
103+
}
81104

82-
lock (BinaryTranslatorMap)
105+
lock (MemberTranslatorMap)
83106
{
84-
foreach (var expressionType in expressionTypes)
85-
{
86-
if (DefaultSupportedTypes.Contains(expressionType))
87-
{
88-
throw new ArgumentException($"{expressionType} is a default expression type and can not have a custom translator");
89-
}
107+
MemberTranslatorMap.Clear();
108+
}
90109

91-
BinaryTranslatorMap.Add(expressionType, translator);
92-
}
110+
lock (BinaryTranslatorMap)
111+
{
112+
BinaryTranslatorMap.Clear();
93113
}
94114
}
95115

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq.Expressions;
4+
using System.Reflection;
45
using System.Text;
56
using MongoDB.Bson;
67

78
namespace MongoFramework.Infrastructure.Querying
89
{
9-
public interface IMethodTranslator
10+
public interface IQueryTranslator { }
11+
public interface IMethodTranslator : IQueryTranslator
1012
{
13+
IEnumerable<MethodInfo> GetSupportedMethods();
1114
BsonValue TranslateMethod(MethodCallExpression expression, IEnumerable<Expression> suffixExpressions = default);
1215
}
1316

14-
public interface IMemberTranslator
17+
public interface IMemberTranslator : IQueryTranslator
1518
{
19+
IEnumerable<MemberInfo> GetSupportedMembers();
1620
BsonValue TranslateMember(MemberExpression expression, IEnumerable<Expression> suffixExpressions = default);
1721
}
1822

19-
public interface IBinaryExpressionTranslator
23+
public interface IBinaryExpressionTranslator : IQueryTranslator
2024
{
25+
IEnumerable<ExpressionType> GetSupportedExpressionTypes();
2126
BsonValue TranslateBinary(BinaryExpression expression);
2227
}
2328
}

src/MongoFramework/Infrastructure/Querying/Translators/OrderByTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace MongoFramework.Infrastructure.Querying.Translators
1010
{
1111
public class OrderByTranslator : IMethodTranslator
1212
{
13-
public static IEnumerable<MethodInfo> GetSupportedMethods()
13+
public IEnumerable<MethodInfo> GetSupportedMethods()
1414
{
1515
yield return TranslationHelper.GetMethodDefinition(() => Queryable.OrderBy(null, (Expression<Func<object, bool>>)null));
1616
yield return TranslationHelper.GetMethodDefinition(() => Queryable.OrderByDescending(null, (Expression<Func<object, bool>>)null));

src/MongoFramework/Infrastructure/Querying/Translators/SelectTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace MongoFramework.Infrastructure.Querying.Translators
1010
{
1111
public class SelectTranslator : IMethodTranslator
1212
{
13-
public static IEnumerable<MethodInfo> GetSupportedMethods()
13+
public IEnumerable<MethodInfo> GetSupportedMethods()
1414
{
1515
yield return TranslationHelper.GetMethodDefinition(() => Queryable.Select(null, (Expression<Func<object, object>>)null));
1616
}

src/MongoFramework/Infrastructure/Querying/Translators/WhereTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace MongoFramework.Infrastructure.Querying.Translators
1010
{
1111
public class WhereTranslator : IMethodTranslator
1212
{
13-
public static IEnumerable<MethodInfo> GetSupportedMethods()
13+
public IEnumerable<MethodInfo> GetSupportedMethods()
1414
{
1515
yield return TranslationHelper.GetMethodDefinition(() => Queryable.Where(null, (Expression<Func<object, bool>>)null));
1616
}

0 commit comments

Comments
 (0)