Skip to content

Commit 85ed653

Browse files
authored
CSHARP-5507: Improve readability of some conventions code (#1622)
1 parent a98fe70 commit 85ed653

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/MongoDB.Bson/Serialization/Conventions/EnumRepresentationConvention.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void Apply(BsonMemberMap memberMap)
7070
{
7171
var memberType = memberMap.MemberType;
7272

73-
if (!(memberType.IsEnum || memberType.IsNullableEnum() || memberType.IsArray || typeof(IEnumerable).IsAssignableFrom(memberType)))
73+
if (!CouldApply(memberType))
7474
{
7575
return;
7676
}
@@ -87,6 +87,9 @@ public void Apply(BsonMemberMap memberMap)
8787

8888
IBsonSerializer Reconfigure(IBsonSerializer s)
8989
=> s.ValueType.IsEnum ? (s as IRepresentationConfigurable)?.WithRepresentation(_representation) : null;
90+
91+
bool CouldApply(Type type)
92+
=> type.IsEnum || type.IsNullableEnum() || type.IsArray || typeof(IEnumerable).IsAssignableFrom(type);
9093
}
9194

9295
// private methods

src/MongoDB.Bson/Serialization/Conventions/ObjectSerializerAllowedTypesConvention.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ public void Apply(BsonMemberMap memberMap)
155155
{
156156
var memberType = memberMap.MemberType;
157157

158-
if (memberType != typeof(object) && Nullable.GetUnderlyingType(memberType) == null && !memberType.IsArray &&
159-
!typeof(IEnumerable).IsAssignableFrom(memberType))
158+
if (!CouldApply(memberType))
160159
{
161160
return;
162161
}
@@ -168,8 +167,12 @@ public void Apply(BsonMemberMap memberMap)
168167
{
169168
memberMap.SetSerializer(reconfiguredSerializer);
170169
}
170+
171+
bool CouldApply(Type type)
172+
=> type == typeof(object) || type.IsNullable() || type.IsArray || typeof(IEnumerable).IsAssignableFrom(type);
171173
}
172174

175+
// private methods
173176
private IBsonSerializer Reconfigure(IBsonSerializer serializer)
174177
{
175178
if (serializer is IChildSerializerConfigurable childSerializerConfigurable)

0 commit comments

Comments
 (0)