Skip to content

Commit 1791906

Browse files
committed
Simplified
1 parent a3afabb commit 1791906

9 files changed

+75
-47
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 MongoDB.Bson.IO;
18+
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Conventions;
20+
21+
namespace MongoDB.Bson
22+
{
23+
internal static class InternalExtensions
24+
{
25+
public static Type GetActualTypeInternal(this IDiscriminatorConvention discriminatorConvention, IBsonReader bsonReader, Type nominalType, IBsonSerializationDomain serializationDomain)
26+
{
27+
if (discriminatorConvention is IDiscriminatorConventionInternal internalConvention)
28+
{
29+
return internalConvention.GetActualType(bsonReader, nominalType, serializationDomain);
30+
}
31+
return discriminatorConvention.GetActualType(bsonReader, nominalType);
32+
}
33+
34+
public static void ApplyInternal(this IMemberMapConvention memberMapConvention, BsonMemberMap memberMap, IBsonSerializationDomain serializationDomain)
35+
{
36+
if (memberMapConvention is IMemberMapConventionInternal internalConvention)
37+
{
38+
internalConvention.Apply(memberMap, serializationDomain);
39+
}
40+
else
41+
{
42+
memberMapConvention.Apply(memberMap);
43+
}
44+
}
45+
46+
public static void PostProcessInternal(this IPostProcessingConvention postProcessingConvention, BsonClassMap classMap, IBsonSerializationDomain serializationDomain)
47+
{
48+
if (postProcessingConvention is IPostProcessingConventionInternal internalConvention)
49+
{
50+
internalConvention.PostProcess(classMap, serializationDomain);
51+
}
52+
else
53+
{
54+
postProcessingConvention.PostProcess(classMap);
55+
}
56+
}
57+
}
58+
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,7 @@ internal void Apply(BsonClassMap classMap, IBsonSerializationDomain serializatio
6060
{
6161
foreach (var memberMap in classMap.DeclaredMemberMaps)
6262
{
63-
if (convention is IMemberMapConventionInternal internalConvention)
64-
{
65-
internalConvention.Apply(memberMap, serializationDomain);
66-
}
67-
else
68-
{
69-
convention.Apply(memberMap);
70-
}
63+
convention.ApplyInternal(memberMap, serializationDomain);
7164
}
7265
}
7366

@@ -81,14 +74,7 @@ internal void Apply(BsonClassMap classMap, IBsonSerializationDomain serializatio
8174

8275
foreach (var convention in _conventions.OfType<IPostProcessingConvention>())
8376
{
84-
if (convention is IPostProcessingConventionInternal internalConvention)
85-
{
86-
internalConvention.PostProcess(classMap, serializationDomain);
87-
}
88-
else
89-
{
90-
convention.PostProcess(classMap);
91-
}
77+
convention.PostProcessInternal(classMap, serializationDomain);
9278
}
9379
}
9480
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public interface IMemberMapConvention : IConvention
2525
/// </summary>
2626
/// <param name="memberMap">The member map.</param>
2727
void Apply(BsonMemberMap memberMap);
28-
2928
}
3029

3130
internal interface IMemberMapConventionInternal : IMemberMapConvention

src/MongoDB.Bson/Serialization/IBsonIdProvider.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public interface IBsonIdProvider
3232
/// <returns>True if the document has an Id.</returns>
3333
bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator);
3434

35+
/// <summary>
36+
/// Sets the document Id.
37+
/// </summary>
38+
/// <param name="document">The document.</param>
39+
/// <param name="id">The Id.</param>
40+
void SetDocumentId(object document, object id);
41+
}
42+
43+
internal interface IBsonIdProviderInternal : IBsonIdProvider
44+
{
3545
/// <summary>
3646
/// //TODO
3747
/// </summary>
@@ -42,12 +52,5 @@ public interface IBsonIdProvider
4252
/// <param name="idGenerator"></param>
4353
/// <returns></returns>
4454
bool GetDocumentId(object document, IBsonSerializationDomain serializationDomain, out object id, out Type idNominalType, out IIdGenerator idGenerator);
45-
46-
/// <summary>
47-
/// Sets the document Id.
48-
/// </summary>
49-
/// <param name="document">The document.</param>
50-
/// <param name="id">The Id.</param>
51-
void SetDocumentId(object document, object id);
5255
}
5356
}

src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,7 @@ public override TClass Deserialize(BsonDeserializationContext context, BsonDeser
9090

9191
var discriminatorConvention = _classMap.GetDiscriminatorConvention(context.SerializationDomain as IBsonSerializationDomainInternal);
9292

93-
Type actualType;
94-
95-
if (discriminatorConvention is IDiscriminatorConventionInternal discriminatorConventionInternal)
96-
{
97-
actualType = discriminatorConventionInternal.GetActualType(bsonReader, args.NominalType, context.SerializationDomain);
98-
}
99-
else
100-
{
101-
actualType = discriminatorConvention.GetActualType(bsonReader, args.NominalType);
102-
}
93+
var actualType = discriminatorConvention.GetActualTypeInternal(bsonReader, args.NominalType, context.SerializationDomain);
10394

10495
if (actualType == typeof(TClass))
10596
{

src/MongoDB.Bson/Serialization/Serializers/ClassSerializerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected virtual TValue DeserializeValue(BsonDeserializationContext context, Bs
101101
protected virtual Type GetActualType(BsonDeserializationContext context)
102102
{
103103
var discriminatorConvention = this.GetDiscriminatorConvention();
104-
return discriminatorConvention.GetActualType(context.Reader, typeof(TValue));
104+
return discriminatorConvention.GetActualTypeInternal(context.Reader, typeof(TValue), context.SerializationDomain);
105105
}
106106

107107
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public override TInterface Deserialize(BsonDeserializationContext context, BsonD
158158
}
159159
else
160160
{
161-
var actualType = _discriminatorConvention.GetActualType(bsonReader, typeof(TInterface));
161+
var actualType = _discriminatorConvention.GetActualTypeInternal(bsonReader, typeof(TInterface), context.SerializationDomain);
162162
if (actualType == _interfaceType)
163163
{
164164
var message = string.Format("Unable to determine actual type of object to deserialize for interface type {0}.", _interfaceType.FullName);

src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser
7373
{
7474
var bsonReader = context.Reader;
7575
var nominalType = args.NominalType;
76-
var actualType = _discriminatorConvention.GetActualType(bsonReader, nominalType);
76+
var actualType = _discriminatorConvention.GetActualTypeInternal(bsonReader, nominalType, context.SerializationDomain);
7777
var serializer = context.SerializationDomain.LookupSerializer(actualType);
7878

7979
TValue value = default(TValue);

src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,7 @@ private object DeserializeDiscriminatedValue(BsonDeserializationContext context,
364364
{
365365
var bsonReader = context.Reader;
366366

367-
Type actualType;
368-
369-
if (_discriminatorConvention is IDiscriminatorConventionInternal discriminatorConventionInternal)
370-
{
371-
actualType = discriminatorConventionInternal.GetActualType(bsonReader, args.NominalType, context.SerializationDomain); //TODO I think these type of things could become an internal extension methods
372-
}
373-
else
374-
{
375-
actualType = _discriminatorConvention.GetActualType(bsonReader, args.NominalType);
376-
}
367+
var actualType = _discriminatorConvention.GetActualTypeInternal(bsonReader, args.NominalType, context.SerializationDomain);
377368

378369
if (!_allowedDeserializationTypes(actualType))
379370
{

0 commit comments

Comments
 (0)