Skip to content

Commit 57aa0d6

Browse files
committed
CSHARP-1507: Refactor MongoDB.Driver.Legacy to reduce dependencies on APIs not supported by .NET Core.
1 parent 16a5e1a commit 57aa0d6

File tree

7 files changed

+72
-51
lines changed

7 files changed

+72
-51
lines changed

src/MongoDB.Driver.Legacy/Linq/Expressions/ExpressionFormatter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
using System.Collections.ObjectModel;
1919
using System.Linq;
2020
using System.Linq.Expressions;
21+
using System.Reflection;
2122
using System.Runtime.CompilerServices;
2223
using System.Text;
2324
using System.Text.RegularExpressions;
@@ -131,7 +132,8 @@ protected override Expression VisitConditional(ConditionalExpression node)
131132
protected override Expression VisitConstant(ConstantExpression node)
132133
{
133134
// need to check node.Type instead of value.GetType() because boxed Nullable<T> values are boxed as <T>
134-
if (node.Type.IsGenericType && node.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
135+
var nodeTypeInfo = node.Type.GetTypeInfo();
136+
if (nodeTypeInfo.IsGenericType && nodeTypeInfo.GetGenericTypeDefinition() == typeof(Nullable<>))
135137
{
136138
_sb.AppendFormat("({0})", FriendlyTypeName(node.Type));
137139
}
@@ -406,11 +408,12 @@ private string FriendlyTypeName(Type type)
406408
{
407409
var typeName = IsAnonymousType(type) ? "__AnonymousType" : type.Name;
408410

409-
if (type.IsGenericType)
411+
var typeInfo = type.GetTypeInfo();
412+
if (typeInfo.IsGenericType)
410413
{
411414
var sb = new StringBuilder();
412415
sb.AppendFormat("{0}<", Regex.Replace(typeName, @"\`\d+$", ""));
413-
foreach (var typeParameter in type.GetGenericArguments())
416+
foreach (var typeParameter in typeInfo.GetGenericArguments())
414417
{
415418
sb.AppendFormat("{0}, ", FriendlyTypeName(typeParameter));
416419
}
@@ -427,10 +430,11 @@ private string FriendlyTypeName(Type type)
427430
private bool IsAnonymousType(Type type)
428431
{
429432
// don't test for too many things in case implementation details change in the future
433+
var typeInfo = type.GetTypeInfo();
430434
return
431435
Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) &&
432-
type.IsGenericType &&
433-
type.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also
436+
typeInfo.IsGenericType &&
437+
typeInfo.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also
434438
}
435439

436440
private void VisitValue(object value)

src/MongoDB.Driver.Legacy/Linq/Expressions/ExpressionPrettyPrinter.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Collections.ObjectModel;
1919
using System.Linq.Expressions;
20+
using System.Reflection;
2021
using System.Text;
2122
using System.Text.RegularExpressions;
2223

@@ -361,14 +362,15 @@ protected override Expression VisitUnary(UnaryExpression node)
361362
// private methods
362363
private string FriendlyClassName(Type type)
363364
{
364-
if (!type.IsGenericType)
365+
var typeInfo = type.GetTypeInfo();
366+
if (!typeInfo.IsGenericType)
365367
{
366-
return type.Name;
368+
return typeInfo.Name;
367369
}
368370

369371
var sb = new StringBuilder();
370-
sb.AppendFormat("{0}<", Regex.Replace(type.Name, @"\`\d+$", ""));
371-
foreach (var typeParameter in type.GetGenericArguments())
372+
sb.AppendFormat("{0}<", Regex.Replace(typeInfo.Name, @"\`\d+$", ""));
373+
foreach (var typeParameter in typeInfo.GetGenericArguments())
372374
{
373375
sb.AppendFormat("{0}, ", FriendlyClassName(typeParameter));
374376
}
@@ -379,9 +381,10 @@ private string FriendlyClassName(Type type)
379381

380382
private string PublicClassName(Type type)
381383
{
382-
while (!type.IsPublic)
384+
var typeInfo = type.GetTypeInfo();
385+
while (!typeInfo.IsPublic)
383386
{
384-
type = type.BaseType;
387+
type = typeInfo.BaseType;
385388
}
386389
return FriendlyClassName(type);
387390
}

src/MongoDB.Driver.Legacy/Linq/MongoQueryProvider.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -167,14 +167,15 @@ private static Type FindIEnumerable(Type seqType)
167167
return null;
168168
}
169169

170-
if (seqType.IsArray)
170+
var seqTypeInfo = seqType.GetTypeInfo();
171+
if (seqTypeInfo.IsArray)
171172
{
172-
return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType());
173+
return typeof(IEnumerable<>).MakeGenericType(seqTypeInfo.GetElementType());
173174
}
174175

175-
if (seqType.IsGenericType)
176+
if (seqTypeInfo.IsGenericType)
176177
{
177-
foreach (Type arg in seqType.GetGenericArguments())
178+
foreach (Type arg in seqTypeInfo.GetGenericArguments())
178179
{
179180
Type ienum = typeof(IEnumerable<>).MakeGenericType(arg);
180181
if (ienum.IsAssignableFrom(seqType))
@@ -184,7 +185,7 @@ private static Type FindIEnumerable(Type seqType)
184185
}
185186
}
186187

187-
Type[] ifaces = seqType.GetInterfaces();
188+
Type[] ifaces = seqTypeInfo.GetInterfaces();
188189
if (ifaces != null && ifaces.Length > 0)
189190
{
190191
foreach (Type iface in ifaces)
@@ -194,9 +195,9 @@ private static Type FindIEnumerable(Type seqType)
194195
}
195196
}
196197

197-
if (seqType.BaseType != null && seqType.BaseType != typeof(object))
198+
if (seqTypeInfo.BaseType != null && seqTypeInfo.BaseType != typeof(object))
198199
{
199-
return FindIEnumerable(seqType.BaseType);
200+
return FindIEnumerable(seqTypeInfo.BaseType);
200201
}
201202

202203
return null;

src/MongoDB.Driver.Legacy/Linq/Translators/ExpressionNormalizer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq.Expressions;
19+
using System.Reflection;
1920

2021
namespace MongoDB.Driver.Linq
2122
{
@@ -98,10 +99,11 @@ protected override Expression VisitBinary(BinaryExpression node)
9899
// VB creates coalescing operations when dealing with nullable value comparisons, so we try and make this look like C#
99100
if (node.NodeType == ExpressionType.Coalesce)
100101
{
102+
var nodeLeftTypeInfo = node.Left.Type.GetTypeInfo();
101103
var right = node.Right as ConstantExpression;
102104
if (node.Left.NodeType == ExpressionType.Equal &&
103-
node.Left.Type.IsGenericType &&
104-
node.Left.Type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
105+
nodeLeftTypeInfo.IsGenericType &&
106+
nodeLeftTypeInfo.GetGenericTypeDefinition() == typeof(Nullable<>) &&
105107
right != null &&
106108
right.Type == typeof(bool) &&
107109
(bool)right.Value == false)

src/MongoDB.Driver.Legacy/Linq/Translators/MongoQueryTranslator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Linq;
1818
using System.Linq.Expressions;
19+
using System.Reflection;
1920

2021
namespace MongoDB.Driver.Linq
2122
{
@@ -60,12 +61,13 @@ private static Type GetDocumentType(Expression expression)
6061
if (constantExpression != null)
6162
{
6263
var constantType = constantExpression.Type;
63-
if (constantType.IsGenericType)
64+
var constantTypeInfo = constantType.GetTypeInfo();
65+
if (constantTypeInfo.IsGenericType)
6466
{
65-
var genericTypeDefinition = constantType.GetGenericTypeDefinition();
67+
var genericTypeDefinition = constantTypeInfo.GetGenericTypeDefinition();
6668
if (genericTypeDefinition == typeof(MongoQueryable<>))
6769
{
68-
return constantType.GetGenericArguments()[0];
70+
return constantTypeInfo.GetGenericArguments()[0];
6971
}
7072
}
7173
}

src/MongoDB.Driver.Legacy/Linq/Translators/PredicateTranslator.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
using System.Globalization;
2020
using System.Linq;
2121
using System.Linq.Expressions;
22+
using System.Reflection;
2223
using System.Text.RegularExpressions;
2324
using MongoDB.Bson;
2425
using MongoDB.Bson.Serialization;
@@ -329,7 +330,9 @@ private IMongoQuery BuildComparisonQuery(Expression variableExpression, Expressi
329330
var unaryExpression = variableExpression as UnaryExpression;
330331
if (unaryExpression != null && (unaryExpression.NodeType == ExpressionType.Convert || unaryExpression.NodeType == ExpressionType.ConvertChecked))
331332
{
332-
if (unaryExpression.Operand.Type.IsEnum)
333+
var unaryExpressionTypeInfo = unaryExpression.Type.GetTypeInfo();
334+
var unaryExpressionOperandTypeInfo = unaryExpression.Operand.Type.GetTypeInfo();
335+
if (unaryExpressionOperandTypeInfo.IsEnum)
333336
{
334337
var enumType = unaryExpression.Operand.Type;
335338
if (unaryExpression.Type == Enum.GetUnderlyingType(enumType))
@@ -339,14 +342,14 @@ private IMongoQuery BuildComparisonQuery(Expression variableExpression, Expressi
339342
}
340343
}
341344
else if (
342-
unaryExpression.Type.IsGenericType &&
343-
unaryExpression.Type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
344-
unaryExpression.Operand.Type.IsGenericType &&
345-
unaryExpression.Operand.Type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
346-
unaryExpression.Operand.Type.GetGenericArguments()[0].IsEnum)
345+
unaryExpressionTypeInfo.IsGenericType &&
346+
unaryExpressionTypeInfo.GetGenericTypeDefinition() == typeof(Nullable<>) &&
347+
unaryExpressionOperandTypeInfo.IsGenericType &&
348+
unaryExpressionOperandTypeInfo.GetGenericTypeDefinition() == typeof(Nullable<>) &&
349+
unaryExpressionOperandTypeInfo.GetGenericArguments()[0].GetTypeInfo().IsEnum)
347350
{
348-
var enumType = unaryExpression.Operand.Type.GetGenericArguments()[0];
349-
if (unaryExpression.Type.GetGenericArguments()[0] == Enum.GetUnderlyingType(enumType))
351+
var enumType = unaryExpressionOperandTypeInfo.GetGenericArguments()[0];
352+
if (unaryExpressionTypeInfo.GetGenericArguments()[0] == Enum.GetUnderlyingType(enumType))
350353
{
351354
serializationInfo = _serializationInfoHelper.GetSerializationInfo(unaryExpression.Operand);
352355
if (value != null)
@@ -449,8 +452,9 @@ private IMongoQuery BuildContainsAnyQuery(MethodCallExpression methodCallExpress
449452
private IMongoQuery BuildContainsKeyQuery(MethodCallExpression methodCallExpression)
450453
{
451454
var dictionaryType = methodCallExpression.Object.Type;
452-
var implementedInterfaces = new List<Type>(dictionaryType.GetInterfaces());
453-
if (dictionaryType.IsInterface)
455+
var dictionaryTypeInfo = dictionaryType.GetTypeInfo();
456+
var implementedInterfaces = new List<Type>(dictionaryTypeInfo.GetInterfaces());
457+
if (dictionaryTypeInfo.IsInterface)
454458
{
455459
implementedInterfaces.Add(dictionaryType);
456460
}
@@ -459,9 +463,10 @@ private IMongoQuery BuildContainsKeyQuery(MethodCallExpression methodCallExpress
459463
Type dictionaryInterface = null;
460464
foreach (var implementedInterface in implementedInterfaces)
461465
{
462-
if (implementedInterface.IsGenericType)
466+
var implementedInterfaceTypeInfo = implementedInterface.GetTypeInfo();
467+
if (implementedInterfaceTypeInfo.IsGenericType)
463468
{
464-
if (implementedInterface.GetGenericTypeDefinition() == typeof(IDictionary<,>))
469+
if (implementedInterfaceTypeInfo.GetGenericTypeDefinition() == typeof(IDictionary<,>))
465470
{
466471
dictionaryGenericInterface = implementedInterface;
467472
}
@@ -633,6 +638,7 @@ private IMongoQuery BuildEqualsQuery(MethodCallExpression methodCallExpression)
633638
private IMongoQuery BuildInQuery(MethodCallExpression methodCallExpression)
634639
{
635640
var methodDeclaringType = methodCallExpression.Method.DeclaringType;
641+
var methodDeclaringTypeInfo = methodDeclaringType.GetTypeInfo();
636642
var arguments = methodCallExpression.Arguments.ToArray();
637643
BsonSerializationInfo serializationInfo = null;
638644
ConstantExpression valuesExpression = null;
@@ -654,12 +660,13 @@ private IMongoQuery BuildInQuery(MethodCallExpression methodCallExpression)
654660
}
655661
else
656662
{
657-
if (methodDeclaringType.IsGenericType)
663+
if (methodDeclaringTypeInfo.IsGenericType)
658664
{
659-
methodDeclaringType = methodDeclaringType.GetGenericTypeDefinition();
665+
methodDeclaringType = methodDeclaringTypeInfo.GetGenericTypeDefinition();
666+
methodDeclaringTypeInfo = methodDeclaringType.GetTypeInfo();
660667
}
661668

662-
bool contains = methodDeclaringType == typeof(ICollection<>) || methodDeclaringType.GetInterface("ICollection`1") != null;
669+
bool contains = methodDeclaringType == typeof(ICollection<>) || methodDeclaringTypeInfo.GetInterface("ICollection`1") != null;
663670
if (contains && arguments.Length == 1)
664671
{
665672
serializationInfo = _serializationInfoHelper.GetSerializationInfo(arguments[0]);
@@ -1148,10 +1155,10 @@ private IMongoQuery BuildStringCaseInsensitiveComparisonQuery(Expression variabl
11481155
{
11491156
var stringValue = serializedValue.AsString;
11501157
var stringValueCaseMatches =
1151-
methodName == "ToLower" && stringValue == stringValue.ToLower(CultureInfo.InvariantCulture) ||
1152-
methodName == "ToLowerInvariant" && stringValue == stringValue.ToLower(CultureInfo.InvariantCulture) ||
1153-
methodName == "ToUpper" && stringValue == stringValue.ToUpper(CultureInfo.InvariantCulture) ||
1154-
methodName == "ToUpperInvariant" && stringValue == stringValue.ToUpper(CultureInfo.InvariantCulture);
1158+
methodName == "ToLower" && stringValue == stringValue.ToLowerInvariant() ||
1159+
methodName == "ToLowerInvariant" && stringValue == stringValue.ToLowerInvariant() ||
1160+
methodName == "ToUpper" && stringValue == stringValue.ToUpperInvariant() ||
1161+
methodName == "ToUpperInvariant" && stringValue == stringValue.ToUpperInvariant();
11551162

11561163
if (stringValueCaseMatches)
11571164
{

src/MongoDB.Driver.Legacy/Linq/Translators/SelectQuery.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
using System.Collections.ObjectModel;
2020
using System.Linq;
2121
using System.Linq.Expressions;
22+
using System.Reflection;
2223
using MongoDB.Bson;
2324
using MongoDB.Bson.Serialization;
2425
using MongoDB.Driver.Builders;
@@ -744,11 +745,12 @@ private void TranslateOfType(MethodCallExpression methodCallExpression)
744745
throw new NotSupportedException("Expected OfType method to have a single argument.");
745746
}
746747
var sourceExpression = args[0];
747-
if (!sourceExpression.Type.IsGenericType)
748+
var sourceExpressionTypeInfo = sourceExpression.Type.GetTypeInfo();
749+
if (!sourceExpressionTypeInfo.IsGenericType)
748750
{
749751
throw new NotSupportedException("Expected source argument to OfType to be a generic type.");
750752
}
751-
var nominalType = sourceExpression.Type.GetGenericArguments()[0];
753+
var nominalType = sourceExpressionTypeInfo.GetGenericArguments()[0];
752754

753755
if (_projection != null)
754756
{

0 commit comments

Comments
 (0)