Skip to content

Commit e022258

Browse files
committed
refactored IBsonSerializer to not be aware of identities instead opting for a more granular interface such that implementors of IBsonSerializer don't need to implement methods they don't use.
1 parent dcf5dd8 commit e022258

File tree

10 files changed

+54
-86
lines changed

10 files changed

+54
-86
lines changed

Bson/Bson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<Compile Include="Serialization\Conventions\SerializationOptionsConventions.cs" />
105105
<Compile Include="Serialization\IBsonClassMapModifier.cs" />
106106
<Compile Include="Serialization\IBsonMemberMapModifier.cs" />
107+
<Compile Include="Serialization\IBsonIdProvider.cs" />
107108
<Compile Include="Serialization\Options\ArraySerializationOptions.cs" />
108109
<Compile Include="Serialization\Options\BsonBaseSerializationOptions.cs" />
109110
<Compile Include="Serialization\Options\DateTimeSerializationOptions.cs" />

Bson/Serialization/BsonClassMapSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace MongoDB.Bson.Serialization
3030
/// <summary>
3131
/// Represents a serializer for a class map.
3232
/// </summary>
33-
public class BsonClassMapSerializer : IBsonSerializer
33+
internal class BsonClassMapSerializer : IBsonSerializer, IBsonIdProvider
3434
{
3535
// private fields
3636
private BsonClassMap _classMap;

Bson/Serialization/IBsonIdProvider.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MongoDB.Bson.Serialization
7+
{
8+
/// <summary>
9+
/// Contract for serializers that can get and set identities.
10+
/// </summary>
11+
public interface IBsonIdProvider
12+
{
13+
/// <summary>
14+
/// Gets the document Id.
15+
/// </summary>
16+
/// <param name="document">The document.</param>
17+
/// <param name="id">The Id.</param>
18+
/// <param name="idNominalType">The nominal type of the Id.</param>
19+
/// <param name="idGenerator">The IdGenerator for the Id type.</param>
20+
/// <returns>True if the document has an Id.</returns>
21+
bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator);
22+
23+
/// <summary>
24+
/// Sets the document Id.
25+
/// </summary>
26+
/// <param name="document">The document.</param>
27+
/// <param name="id">The Id.</param>
28+
void SetDocumentId(object document, object id);
29+
}
30+
}

Bson/Serialization/IBsonSerializer.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ public interface IBsonSerializer
4747
/// <returns>The default serialization options for this serializer.</returns>
4848
IBsonSerializationOptions GetDefaultSerializationOptions();
4949
/// <summary>
50-
/// Gets the document Id.
51-
/// </summary>
52-
/// <param name="document">The document.</param>
53-
/// <param name="id">The Id.</param>
54-
/// <param name="idNominalType">The nominal type of the Id.</param>
55-
/// <param name="idGenerator">The IdGenerator for the Id type.</param>
56-
/// <returns>True if the document has an Id.</returns>
57-
bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator);
58-
/// <summary>
5950
/// Gets the serialization info for individual items of an enumerable type.
6051
/// </summary>
6152
/// <returns>The serialization info for the items.</returns>
@@ -74,11 +65,5 @@ public interface IBsonSerializer
7465
/// <param name="value">The object.</param>
7566
/// <param name="options">The serialization options.</param>
7667
void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options);
77-
/// <summary>
78-
/// Sets the document Id.
79-
/// </summary>
80-
/// <param name="document">The document.</param>
81-
/// <param name="id">The Id.</param>
82-
void SetDocumentId(object document, object id);
8368
}
8469
}

Bson/Serialization/Serializers/BsonBaseSerializer.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,6 @@ public virtual IBsonSerializationOptions GetDefaultSerializationOptions()
103103
return _defaultSerializationOptions;
104104
}
105105

106-
/// <summary>
107-
/// Gets the document Id.
108-
/// </summary>
109-
/// <param name="document">The document.</param>
110-
/// <param name="id">The Id.</param>
111-
/// <param name="idNominalType">The nominal type of the Id.</param>
112-
/// <param name="idGenerator">The IdGenerator for the Id type.</param>
113-
/// <returns>True if the document has an Id.</returns>
114-
public virtual bool GetDocumentId(
115-
object document,
116-
out object id,
117-
out Type idNominalType,
118-
out IIdGenerator idGenerator)
119-
{
120-
throw new NotSupportedException("Subclass must implement GetDocumentId.");
121-
}
122-
123106
/// <summary>
124107
/// Gets the serialization info for individual items of an enumerable type.
125108
/// </summary>
@@ -157,16 +140,6 @@ public virtual void Serialize(
157140
throw new NotSupportedException("Subclass must implement Serialize.");
158141
}
159142

160-
/// <summary>
161-
/// Sets the document Id.
162-
/// </summary>
163-
/// <param name="document">The document.</param>
164-
/// <param name="id">The Id.</param>
165-
public virtual void SetDocumentId(object document, object id)
166-
{
167-
throw new NotSupportedException("Subclass must implement SetDocumentId.");
168-
}
169-
170143
// protected methods
171144
/// <summary>
172145
/// Ensures that the serializer has serialization options of the right type (replacing null with the default serialization options if necessary).

Bson/Serialization/Serializers/BsonIBsonSerializableSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace MongoDB.Bson.Serialization.Serializers
2828
/// <summary>
2929
/// Represents a serializer for classes that implement IBsonSerializable.
3030
/// </summary>
31-
public class BsonIBsonSerializableSerializer : IBsonSerializer
31+
public class BsonIBsonSerializableSerializer : IBsonSerializer, IBsonIdProvider
3232
{
3333
// private static fields
3434
private static BsonIBsonSerializableSerializer __instance = new BsonIBsonSerializableSerializer();

Bson/Serialization/Serializers/BsonValueSerializers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public override void Serialize(
543543
/// <summary>
544544
/// Represents a serializer for BsonDocuments.
545545
/// </summary>
546-
public class BsonDocumentSerializer : BsonBaseSerializer
546+
public class BsonDocumentSerializer : BsonBaseSerializer, IBsonIdProvider
547547
{
548548
// private static fields
549549
private static BsonDocumentSerializer __instance = new BsonDocumentSerializer();
@@ -594,7 +594,7 @@ public override object Deserialize(
594594
/// <param name="idNominalType">The nominal type of the Id.</param>
595595
/// <param name="idGenerator">The IdGenerator for the Id type.</param>
596596
/// <returns>True if the document has an Id.</returns>
597-
public override bool GetDocumentId(
597+
public bool GetDocumentId(
598598
object document,
599599
out object id,
600600
out Type idNominalType,
@@ -633,7 +633,7 @@ public override void Serialize(
633633
/// </summary>
634634
/// <param name="document">The document.</param>
635635
/// <param name="id">The Id.</param>
636-
public override void SetDocumentId(object document, object id)
636+
public void SetDocumentId(object document, object id)
637637
{
638638
var bsonDocument = (BsonDocument)document;
639639
((IBsonSerializable)bsonDocument).SetDocumentId(id);

Bson/Serialization/Serializers/ObjectSerializer.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,6 @@ public IBsonSerializationOptions GetDefaultSerializationOptions()
155155
return null;
156156
}
157157

158-
/// <summary>
159-
/// Gets the document Id.
160-
/// </summary>
161-
/// <param name="document">The document.</param>
162-
/// <param name="id">The Id.</param>
163-
/// <param name="idNominalType">The nominal type of the Id.</param>
164-
/// <param name="idGenerator">The IdGenerator for the Id type.</param>
165-
/// <returns>True if the document has an Id.</returns>
166-
public bool GetDocumentId(
167-
object document,
168-
out object id,
169-
out Type idNominalType,
170-
out IIdGenerator idGenerator)
171-
{
172-
throw new NotSupportedException();
173-
}
174-
175158
/// <summary>
176159
/// Gets the serialization info for individual items of an enumerable type.
177160
/// </summary>
@@ -221,15 +204,5 @@ public void Serialize(
221204
bsonWriter.WriteEndDocument();
222205
}
223206
}
224-
225-
/// <summary>
226-
/// Sets the document Id.
227-
/// </summary>
228-
/// <param name="document">The document.</param>
229-
/// <param name="id">The Id.</param>
230-
public void SetDocumentId(object document, object id)
231-
{
232-
throw new NotSupportedException();
233-
}
234207
}
235208
}

BsonUnitTests/Jira/CSharp81Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public void TestIdMember()
5252
var idMemberMap = classMap.IdMemberMap;
5353
Assert.AreEqual("Id", idMemberMap.MemberName);
5454

55-
var serializer = BsonSerializer.LookupSerializer(typeof(User));
55+
var identityProvider = BsonSerializer.LookupSerializer(typeof(User)) as IBsonIdProvider;
5656
var idGenerator = BsonSerializer.LookupIdGenerator(typeof(ObjectId));
57-
serializer.SetDocumentId(u, idGenerator.GenerateId(null, u));
57+
identityProvider.SetDocumentId(u, idGenerator.GenerateId(null, u));
5858
Assert.IsFalse(idGenerator.IsEmpty(u.Id));
5959
Assert.IsTrue(idGenerator.IsEmpty(u.FriendId));
6060

6161
var json = u.ToJson();
6262
}
6363
}
64-
}
64+
}

Driver/Core/MongoCollection.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,18 +1075,23 @@ public virtual IEnumerable<SafeModeResult> InsertBatch(
10751075
{
10761076
throw new ArgumentException("Batch contains one or more null documents.");
10771077
}
1078+
10781079
if (_settings.AssignIdOnInsert)
10791080
{
10801081
var serializer = BsonSerializer.LookupSerializer(document.GetType());
1081-
object id;
1082-
Type idNominalType;
1083-
IIdGenerator idGenerator;
1084-
if (serializer.GetDocumentId(document, out id, out idNominalType, out idGenerator))
1082+
var identityProvider = serializer as IBsonIdProvider;
1083+
if (identityProvider != null)
10851084
{
1086-
if (idGenerator != null && idGenerator.IsEmpty(id))
1085+
object id;
1086+
Type idNominalType;
1087+
IIdGenerator idGenerator;
1088+
if (identityProvider.GetDocumentId(document, out id, out idNominalType, out idGenerator))
10871089
{
1088-
id = idGenerator.GenerateId(this, document);
1089-
serializer.SetDocumentId(document, id);
1090+
if (idGenerator != null && idGenerator.IsEmpty(id))
1091+
{
1092+
id = idGenerator.GenerateId(this, document);
1093+
identityProvider.SetDocumentId(document, id);
1094+
}
10901095
}
10911096
}
10921097
}
@@ -1351,10 +1356,11 @@ public virtual SafeModeResult Save(Type nominalType, object document, MongoInser
13511356
throw new ArgumentNullException("document");
13521357
}
13531358
var serializer = BsonSerializer.LookupSerializer(document.GetType());
1359+
var identityProvider = serializer as IBsonIdProvider;
13541360
object id;
13551361
Type idNominalType;
13561362
IIdGenerator idGenerator;
1357-
if (serializer.GetDocumentId(document, out id, out idNominalType, out idGenerator))
1363+
if (identityProvider != null && identityProvider.GetDocumentId(document, out id, out idNominalType, out idGenerator))
13581364
{
13591365
if (id == null && idGenerator == null)
13601366
{
@@ -1364,7 +1370,7 @@ public virtual SafeModeResult Save(Type nominalType, object document, MongoInser
13641370
if (idGenerator != null && idGenerator.IsEmpty(id))
13651371
{
13661372
id = idGenerator.GenerateId(this, document);
1367-
serializer.SetDocumentId(document, id);
1373+
identityProvider.SetDocumentId(document, id);
13681374
return Insert(nominalType, document, options);
13691375
}
13701376
else

0 commit comments

Comments
 (0)