Skip to content

Commit 2a79ee4

Browse files
committed
CSHARP-2239: Refactor ChangeStreamDocument to be a BsonDocumentBackedClass.
1 parent 4d24256 commit 2a79ee4

16 files changed

+572
-312
lines changed

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ protected BsonDocumentBackedClassSerializer()
3939
}
4040

4141
// public methods
42-
/// <summary>
43-
/// Deserializes a value.
44-
/// </summary>
45-
/// <param name="context">The deserialization context.</param>
46-
/// <param name="args">The deserialization args.</param>
47-
/// <returns>A deserialized value.</returns>
48-
public override TClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
49-
{
50-
var backingDocument = BsonDocumentSerializer.Instance.Deserialize(context);
51-
return CreateInstance(backingDocument);
52-
}
53-
5442
/// <summary>
5543
/// Tries to get the serialization info for a member.
5644
/// </summary>
@@ -64,19 +52,14 @@ public virtual bool TryGetMemberSerializationInfo(string memberName, out BsonSer
6452
return _memberSerializationInfo.TryGetValue(memberName, out serializationInfo);
6553
}
6654

67-
/// <summary>
68-
/// Serializes a value.
69-
/// </summary>
70-
/// <param name="context">The serialization context.</param>
71-
/// <param name="args">The serialization args.</param>
72-
/// <param name="value">The object.</param>
73-
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
55+
// protected methods
56+
/// <inheritdoc />
57+
protected override TClass DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
7458
{
75-
var backingDocument = ((BsonDocumentBackedClass)value).BackingDocument;
76-
BsonDocumentSerializer.Instance.Serialize(context, backingDocument);
59+
var backingDocument = BsonDocumentSerializer.Instance.Deserialize(context);
60+
return CreateInstance(backingDocument);
7761
}
7862

79-
// protected methods
8063
/// <summary>
8164
/// Registers a member.
8265
/// </summary>
@@ -102,6 +85,13 @@ protected void RegisterMember(string memberName, string elementName, IBsonSerial
10285
_memberSerializationInfo.Add(memberName, info);
10386
}
10487

88+
/// <inheritdoc />
89+
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
90+
{
91+
var backingDocument = ((BsonDocumentBackedClass)value).BackingDocument;
92+
BsonDocumentSerializer.Instance.Serialize(context, backingDocument);
93+
}
94+
10595
/// <summary>
10696
/// Creates the instance.
10797
/// </summary>

src/MongoDB.Driver.Core/ChangeStreamDocument.cs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,97 +14,91 @@
1414
*/
1515

1616
using MongoDB.Bson;
17-
using MongoDB.Driver.Core.Misc;
17+
using MongoDB.Bson.Serialization;
18+
using MongoDB.Bson.Serialization.Attributes;
1819

1920
namespace MongoDB.Driver
2021
{
2122
/// <summary>
2223
/// An output document from a $changeStream pipeline stage.
2324
/// </summary>
2425
/// <typeparam name="TDocument">The type of the document.</typeparam>
25-
public sealed class ChangeStreamDocument<TDocument>
26+
[BsonSerializer(typeof(ChangeStreamDocumentSerializer<>))]
27+
public sealed class ChangeStreamDocument<TDocument> : BsonDocumentBackedClass
2628
{
27-
// private fields
28-
private readonly CollectionNamespace _collectionNamespace;
29-
private readonly BsonDocument _documentKey;
30-
private readonly TDocument _fullDocument;
31-
private readonly ChangeStreamOperationType _operationType;
32-
private readonly BsonDocument _resumeToken;
33-
private readonly ChangeStreamUpdateDescription _updateDescription;
34-
3529
// constructors
3630
/// <summary>
37-
/// Initializes a new instance of the <see cref="ChangeStreamDocument{TDocument}" /> class.
31+
/// Initializes a new instance of the <see cref="ChangeStreamDocument{TDocument}"/> class.
3832
/// </summary>
39-
/// <param name="resumeToken">The resume token.</param>
40-
/// <param name="operationType">Type of the operation.</param>
41-
/// <param name="collectionNamespace">Namespace of the collection.</param>
42-
/// <param name="documentKey">The document key.</param>
43-
/// <param name="updateDescription">The update description.</param>
44-
/// <param name="fullDocument">The full document.</param>
33+
/// <param name="backingDocument">The backing document.</param>
34+
/// <param name="documentSerializer">The document serializer.</param>
4535
public ChangeStreamDocument(
46-
BsonDocument resumeToken,
47-
ChangeStreamOperationType operationType,
48-
CollectionNamespace collectionNamespace,
49-
BsonDocument documentKey,
50-
ChangeStreamUpdateDescription updateDescription,
51-
TDocument fullDocument)
36+
BsonDocument backingDocument,
37+
IBsonSerializer<TDocument> documentSerializer)
38+
: base(backingDocument, new ChangeStreamDocumentSerializer<TDocument>(documentSerializer))
5239
{
53-
_resumeToken = Ensure.IsNotNull(resumeToken, nameof(resumeToken));
54-
_operationType = operationType;
55-
_collectionNamespace = collectionNamespace; // can be null when operationType is Invalidate
56-
_documentKey = documentKey; // can be null
57-
_updateDescription = updateDescription; // can be null
58-
_fullDocument = fullDocument; // can be null
5940
}
6041

6142
// public properties
43+
/// <summary>
44+
/// Gets the backing document.
45+
/// </summary>
46+
new public BsonDocument BackingDocument => base.BackingDocument;
47+
48+
/// <summary>
49+
/// Gets the cluster time.
50+
/// </summary>
51+
/// <value>
52+
/// The cluster time.
53+
/// </value>
54+
public BsonDocument ClusterTime => GetValue<BsonDocument>(nameof(ClusterTime), null);
55+
6256
/// <summary>
6357
/// Gets the namespace of the collection.
6458
/// </summary>
6559
/// <value>
6660
/// The namespace of the collection.
6761
/// </value>
68-
public CollectionNamespace CollectionNamespace => _collectionNamespace;
62+
public CollectionNamespace CollectionNamespace => GetValue<CollectionNamespace>(nameof(CollectionNamespace), null);
6963

7064
/// <summary>
7165
/// Gets the document key.
7266
/// </summary>
7367
/// <value>
7468
/// The document key.
7569
/// </value>
76-
public BsonDocument DocumentKey => _documentKey;
70+
public BsonDocument DocumentKey => GetValue<BsonDocument>(nameof(DocumentKey), null);
7771

7872
/// <summary>
7973
/// Gets the full document.
8074
/// </summary>
8175
/// <value>
8276
/// The full document.
8377
/// </value>
84-
public TDocument FullDocument => _fullDocument;
78+
public TDocument FullDocument => GetValue<TDocument>(nameof(FullDocument), default(TDocument));
8579

8680
/// <summary>
8781
/// Gets the type of the operation.
8882
/// </summary>
8983
/// <value>
9084
/// The type of the operation.
9185
/// </value>
92-
public ChangeStreamOperationType OperationType => _operationType;
86+
public ChangeStreamOperationType OperationType => GetValue<ChangeStreamOperationType>(nameof(OperationType), (ChangeStreamOperationType)(-1));
9387

9488
/// <summary>
9589
/// Gets the resume token.
9690
/// </summary>
9791
/// <value>
9892
/// The resume token.
9993
/// </value>
100-
public BsonDocument ResumeToken => _resumeToken;
94+
public BsonDocument ResumeToken => GetValue<BsonDocument>(nameof(ResumeToken), null);
10195

10296
/// <summary>
10397
/// Gets the update description.
10498
/// </summary>
10599
/// <value>
106100
/// The update description.
107101
/// </value>
108-
public ChangeStreamUpdateDescription UpdateDescription => _updateDescription;
102+
public ChangeStreamUpdateDescription UpdateDescription => GetValue<ChangeStreamUpdateDescription>(nameof(UpdateDescription), null);
109103
}
110104
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* Copyright 2018-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.Serializers;
20+
21+
namespace MongoDB.Driver
22+
{
23+
internal class ChangeStreamDocumentCollectionNamespaceSerializer : SealedClassSerializerBase<CollectionNamespace>
24+
{
25+
#region static
26+
// private static fields
27+
private static readonly ChangeStreamDocumentCollectionNamespaceSerializer __instance = new ChangeStreamDocumentCollectionNamespaceSerializer();
28+
29+
// public static properties
30+
public static ChangeStreamDocumentCollectionNamespaceSerializer Instance => __instance;
31+
#endregion
32+
33+
// public methods
34+
public override CollectionNamespace Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
35+
{
36+
var reader = context.Reader;
37+
string collectionName = null;
38+
string databaseName = null;
39+
40+
reader.ReadStartDocument();
41+
while (reader.ReadBsonType() != 0)
42+
{
43+
var fieldName = reader.ReadName();
44+
switch (fieldName)
45+
{
46+
case "db":
47+
databaseName = reader.ReadString();
48+
break;
49+
50+
case "coll":
51+
collectionName = reader.ReadString();
52+
break;
53+
54+
default:
55+
throw new FormatException($"Invalid field name: \"{fieldName}\".");
56+
}
57+
}
58+
reader.ReadEndDocument();
59+
60+
var databaseNamespace = new DatabaseNamespace(databaseName);
61+
return new CollectionNamespace(databaseNamespace, collectionName);
62+
}
63+
64+
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, CollectionNamespace value)
65+
{
66+
var writer = context.Writer;
67+
68+
writer.WriteStartDocument();
69+
writer.WriteName("db");
70+
writer.WriteString(value.DatabaseNamespace.DatabaseName);
71+
writer.WriteName("coll");
72+
writer.WriteString(value.CollectionName);
73+
writer.WriteEndDocument();
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)