Skip to content

Commit d9c718a

Browse files
committed
CSHARP-3903: Change LinqProvider class to enum.
1 parent 44ef5df commit d9c718a

File tree

12 files changed

+131
-95
lines changed

12 files changed

+131
-95
lines changed

src/MongoDB.Driver/AggregateExpressionDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public ExpressionAggregateExpressionDefinition(Expression<Func<TSource, TResult>
132132
/// <inheritdoc/>
133133
public override BsonValue Render(IBsonSerializer<TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
134134
{
135-
return linqProvider.TranslateExpressionToAggregateExpression(_expression, sourceSerializer, serializerRegistry, _translationOptions);
135+
return linqProvider.GetAdapter().TranslateExpressionToAggregateExpression(_expression, sourceSerializer, serializerRegistry, _translationOptions);
136136
}
137137
}
138138
}

src/MongoDB.Driver/FieldDefinition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public LambdaExpression Expression
300300
/// <inheritdoc />
301301
public override RenderedFieldDefinition Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
302302
{
303-
return linqProvider.TranslateExpressionToField(_expression, documentSerializer, serializerRegistry);
303+
return linqProvider.GetAdapter().TranslateExpressionToField(_expression, documentSerializer, serializerRegistry);
304304
}
305305
}
306306

@@ -343,7 +343,7 @@ public override RenderedFieldDefinition<TField> Render(
343343
LinqProvider linqProvider,
344344
bool allowScalarValueForArrayField)
345345
{
346-
return linqProvider.TranslateExpressionToField(_expression, documentSerializer, serializerRegistry, allowScalarValueForArrayField);
346+
return linqProvider.GetAdapter().TranslateExpressionToField(_expression, documentSerializer, serializerRegistry, allowScalarValueForArrayField);
347347
}
348348
}
349349

src/MongoDB.Driver/FilterDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public Expression<Func<TDocument, bool>> Expression
217217
/// <inheritdoc />
218218
public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
219219
{
220-
return linqProvider.TranslateExpressionToFilter(_expression, documentSerializer, serializerRegistry);
220+
return linqProvider.GetAdapter().TranslateExpressionToFilter(_expression, documentSerializer, serializerRegistry);
221221
}
222222
}
223223

src/MongoDB.Driver/IMongoCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,7 @@ private static IMongoQueryable<TDocument> AsQueryableHelper<TDocument>(IMongoCol
23272327
{
23282328
var linqProvider = collection.Database.Client.Settings.LinqProvider;
23292329
aggregateOptions = aggregateOptions ?? new AggregateOptions();
2330-
return linqProvider.AsQueryable(collection, session, aggregateOptions);
2330+
return linqProvider.GetAdapter().AsQueryable(collection, session, aggregateOptions);
23312331
}
23322332
}
23332333
}

src/MongoDB.Driver/Linq/Linq2Implementation/LinqProviderV2.cs renamed to src/MongoDB.Driver/Linq/Linq2Implementation/LinqProviderAdapterV2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
namespace MongoDB.Driver.Linq.Linq2Implementation
2828
{
29-
internal sealed class LinqProviderV2 : LinqProvider
29+
internal sealed class LinqProviderAdapterV2 : LinqProviderAdapter
3030
{
3131
internal override IMongoQueryable<TDocument> AsQueryable<TDocument>(
3232
IMongoCollection<TDocument> collection,

src/MongoDB.Driver/Linq/Linq3Implementation/LinqProviderV3.cs renamed to src/MongoDB.Driver/Linq/Linq3Implementation/LinqProviderAdapterV3.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
namespace MongoDB.Driver.Linq.Linq3Implementation
3030
{
31-
internal sealed class LinqProviderV3 : LinqProvider
31+
internal sealed class LinqProviderAdapterV3 : LinqProviderAdapter
3232
{
3333
internal override IMongoQueryable<TDocument> AsQueryable<TDocument>(
3434
IMongoCollection<TDocument> collection,
@@ -61,7 +61,7 @@ internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToBuc
6161
ExpressionTranslationOptions translationOptions)
6262
{
6363
// TODO: implement using LINQ3 instead of falling back to LINQ2
64-
return LinqProvider.V2.TranslateExpressionToBucketOutputProjection(valueExpression, outputExpression, documentSerializer, serializerRegistry, translationOptions);
64+
return LinqProviderAdapter.V2.TranslateExpressionToBucketOutputProjection(valueExpression, outputExpression, documentSerializer, serializerRegistry, translationOptions);
6565
}
6666

6767
internal override RenderedFieldDefinition TranslateExpressionToField<TDocument>(
@@ -114,7 +114,7 @@ internal override RenderedProjectionDefinition<TProjection> TranslateExpressionT
114114
IBsonSerializerRegistry serializerRegistry)
115115
{
116116
// TODO: implement using LINQ3 instead of falling back to LINQ2
117-
return LinqProvider.V2.TranslateExpressionToFindProjection(expression, sourceSerializer, serializerRegistry);
117+
return LinqProviderAdapter.V2.TranslateExpressionToFindProjection(expression, sourceSerializer, serializerRegistry);
118118
}
119119

120120
internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToGroupProjection<TInput, TKey, TOutput>(
@@ -125,7 +125,7 @@ internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToGro
125125
ExpressionTranslationOptions translationOptions)
126126
{
127127
// TODO: implement using LINQ3 instead of falling back to LINQ2
128-
return LinqProvider.V2.TranslateExpressionToGroupProjection(idExpression, groupExpression, documentSerializer, serializerRegistry, translationOptions);
128+
return LinqProviderAdapter.V2.TranslateExpressionToGroupProjection(idExpression, groupExpression, documentSerializer, serializerRegistry, translationOptions);
129129
}
130130

131131
internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToProjection<TInput, TOutput>(

src/MongoDB.Driver/Linq/LinqProvider.cs

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,83 +14,35 @@
1414
*/
1515

1616
using System;
17-
using System.Linq;
18-
using System.Linq.Expressions;
19-
using System.Threading;
20-
using MongoDB.Bson;
21-
using MongoDB.Bson.Serialization;
2217

2318
namespace MongoDB.Driver.Linq
2419
{
2520
/// <summary>
2621
/// Represents the different LINQ providers.
2722
/// </summary>
28-
public abstract class LinqProvider
23+
public enum LinqProvider
2924
{
30-
#region static
31-
// public static fields
3225
/// <summary>
3326
/// The LINQ provider that was first shipped with version 2.0 of the driver,
3427
/// </summary>
35-
public static readonly LinqProvider V2 = new Linq2Implementation.LinqProviderV2();
28+
V2 = 2,
3629

3730
/// <summary>
3831
/// The LINQ provider that is planned to be the default in version 3.0 of the driver and can be optionally used before that.
3932
/// </summary>
40-
public static readonly LinqProvider V3 = new Linq3Implementation.LinqProviderV3();
41-
#endregion
42-
43-
// internal methods
44-
internal abstract IMongoQueryable<TDocument> AsQueryable<TDocument>(
45-
IMongoCollection<TDocument> collection,
46-
IClientSessionHandle session,
47-
AggregateOptions options);
48-
49-
internal abstract BsonValue TranslateExpressionToAggregateExpression<TSource, TResult>(
50-
Expression<Func<TSource, TResult>> expression,
51-
IBsonSerializer<TSource> sourceSerializer,
52-
IBsonSerializerRegistry serializerRegistry,
53-
ExpressionTranslationOptions translationOptions);
54-
55-
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToBucketOutputProjection<TInput, TValue, TOutput>(
56-
Expression<Func<TInput, TValue>> valueExpression,
57-
Expression<Func<IGrouping<TValue, TInput>, TOutput>> outputExpression,
58-
IBsonSerializer<TInput> documentSerializer,
59-
IBsonSerializerRegistry serializerRegistry,
60-
ExpressionTranslationOptions translationOptions);
61-
62-
internal abstract RenderedFieldDefinition TranslateExpressionToField<TDocument>(
63-
LambdaExpression expression,
64-
IBsonSerializer<TDocument> documentSerializer,
65-
IBsonSerializerRegistry serializerRegistry);
66-
67-
internal abstract RenderedFieldDefinition<TField> TranslateExpressionToField<TDocument, TField>(
68-
Expression<Func<TDocument, TField>> expression,
69-
IBsonSerializer<TDocument> documentSerializer,
70-
IBsonSerializerRegistry serializerRegistry,
71-
bool allowScalarValueForArrayField);
72-
73-
internal abstract BsonDocument TranslateExpressionToFilter<TDocument>(
74-
Expression<Func<TDocument, bool>> expression,
75-
IBsonSerializer<TDocument> documentSerializer,
76-
IBsonSerializerRegistry serializerRegistry);
77-
78-
internal abstract RenderedProjectionDefinition<TProjection> TranslateExpressionToFindProjection<TSource, TProjection>(
79-
Expression<Func<TSource, TProjection>> expression,
80-
IBsonSerializer<TSource> sourceSerializer,
81-
IBsonSerializerRegistry serializerRegistry);
82-
83-
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToGroupProjection<TInput, TKey, TOutput>(
84-
Expression<Func<TInput, TKey>> idExpression,
85-
Expression<Func<IGrouping<TKey, TInput>, TOutput>> groupExpression,
86-
IBsonSerializer<TInput> documentSerializer,
87-
IBsonSerializerRegistry serializerRegistry,
88-
ExpressionTranslationOptions translationOptions);
33+
V3 = 3
34+
}
8935

90-
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToProjection<TInput, TOutput>(
91-
Expression<Func<TInput, TOutput>> expression,
92-
IBsonSerializer<TInput> inputSerializer,
93-
IBsonSerializerRegistry serializerRegistry,
94-
ExpressionTranslationOptions translationOptions);
36+
internal static class LinqProviderExtensions
37+
{
38+
public static LinqProviderAdapter GetAdapter(this LinqProvider linqProvider)
39+
{
40+
return linqProvider switch
41+
{
42+
LinqProvider.V2 => LinqProviderAdapter.V2,
43+
LinqProvider.V3 => LinqProviderAdapter.V3,
44+
_ => throw new ArgumentException($"Unknown LINQ provider: {linqProvider}.", nameof(linqProvider))
45+
};
46+
}
9547
}
9648
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Linq;
18+
using System.Linq.Expressions;
19+
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization;
21+
22+
namespace MongoDB.Driver.Linq
23+
{
24+
internal abstract class LinqProviderAdapter
25+
{
26+
#region static
27+
public static readonly LinqProviderAdapter V2 = new Linq2Implementation.LinqProviderAdapterV2();
28+
public static readonly LinqProviderAdapter V3 = new Linq3Implementation.LinqProviderAdapterV3();
29+
#endregion
30+
31+
// internal methods
32+
internal abstract IMongoQueryable<TDocument> AsQueryable<TDocument>(
33+
IMongoCollection<TDocument> collection,
34+
IClientSessionHandle session,
35+
AggregateOptions options);
36+
37+
internal abstract BsonValue TranslateExpressionToAggregateExpression<TSource, TResult>(
38+
Expression<Func<TSource, TResult>> expression,
39+
IBsonSerializer<TSource> sourceSerializer,
40+
IBsonSerializerRegistry serializerRegistry,
41+
ExpressionTranslationOptions translationOptions);
42+
43+
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToBucketOutputProjection<TInput, TValue, TOutput>(
44+
Expression<Func<TInput, TValue>> valueExpression,
45+
Expression<Func<IGrouping<TValue, TInput>, TOutput>> outputExpression,
46+
IBsonSerializer<TInput> documentSerializer,
47+
IBsonSerializerRegistry serializerRegistry,
48+
ExpressionTranslationOptions translationOptions);
49+
50+
internal abstract RenderedFieldDefinition TranslateExpressionToField<TDocument>(
51+
LambdaExpression expression,
52+
IBsonSerializer<TDocument> documentSerializer,
53+
IBsonSerializerRegistry serializerRegistry);
54+
55+
internal abstract RenderedFieldDefinition<TField> TranslateExpressionToField<TDocument, TField>(
56+
Expression<Func<TDocument, TField>> expression,
57+
IBsonSerializer<TDocument> documentSerializer,
58+
IBsonSerializerRegistry serializerRegistry,
59+
bool allowScalarValueForArrayField);
60+
61+
internal abstract BsonDocument TranslateExpressionToFilter<TDocument>(
62+
Expression<Func<TDocument, bool>> expression,
63+
IBsonSerializer<TDocument> documentSerializer,
64+
IBsonSerializerRegistry serializerRegistry);
65+
66+
internal abstract RenderedProjectionDefinition<TProjection> TranslateExpressionToFindProjection<TSource, TProjection>(
67+
Expression<Func<TSource, TProjection>> expression,
68+
IBsonSerializer<TSource> sourceSerializer,
69+
IBsonSerializerRegistry serializerRegistry);
70+
71+
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToGroupProjection<TInput, TKey, TOutput>(
72+
Expression<Func<TInput, TKey>> idExpression,
73+
Expression<Func<IGrouping<TKey, TInput>, TOutput>> groupExpression,
74+
IBsonSerializer<TInput> documentSerializer,
75+
IBsonSerializerRegistry serializerRegistry,
76+
ExpressionTranslationOptions translationOptions);
77+
78+
internal abstract RenderedProjectionDefinition<TOutput> TranslateExpressionToProjection<TInput, TOutput>(
79+
Expression<Func<TInput, TOutput>> expression,
80+
IBsonSerializer<TInput> inputSerializer,
81+
IBsonSerializerRegistry serializerRegistry,
82+
ExpressionTranslationOptions translationOptions);
83+
}
84+
}

src/MongoDB.Driver/MongoClientSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public LinqProvider LinqProvider
414414
set
415415
{
416416
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
417-
_linqProvider = Ensure.IsNotNull(value, nameof(value));
417+
_linqProvider = value;
418418
}
419419
}
420420

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ public Expression<Func<IGrouping<TValue, TInput>, TOutput>> OutputExpression
14761476

14771477
public override RenderedProjectionDefinition<TOutput> Render(IBsonSerializer<TInput> documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
14781478
{
1479-
return linqProvider.TranslateExpressionToBucketOutputProjection(_valueExpression, _outputExpression, documentSerializer, serializerRegistry, _translationOptions);
1479+
return linqProvider.GetAdapter().TranslateExpressionToBucketOutputProjection(_valueExpression, _outputExpression, documentSerializer, serializerRegistry, _translationOptions);
14801480
}
14811481
}
14821482

@@ -1505,7 +1505,7 @@ public Expression<Func<IGrouping<TKey, TInput>, TOutput>> GroupExpression
15051505

15061506
public override RenderedProjectionDefinition<TOutput> Render(IBsonSerializer<TInput> documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
15071507
{
1508-
return linqProvider.TranslateExpressionToGroupProjection(_idExpression, _groupExpression, documentSerializer, serializerRegistry, _translationOptions);
1508+
return linqProvider.GetAdapter().TranslateExpressionToGroupProjection(_idExpression, _groupExpression, documentSerializer, serializerRegistry, _translationOptions);
15091509
}
15101510
}
15111511

@@ -1527,7 +1527,7 @@ public Expression<Func<TInput, TOutput>> Expression
15271527

15281528
public override RenderedProjectionDefinition<TOutput> Render(IBsonSerializer<TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
15291529
{
1530-
return linqProvider.TranslateExpressionToProjection(_expression, inputSerializer, serializerRegistry, _translationOptions);
1530+
return linqProvider.GetAdapter().TranslateExpressionToProjection(_expression, inputSerializer, serializerRegistry, _translationOptions);
15311531
}
15321532
}
15331533

0 commit comments

Comments
 (0)