From 978f26f02aa3c6c50d3182b02fe7f0035a58b16f Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 3 Dec 2024 14:35:52 -0500 Subject: [PATCH 1/4] DOCSP-45715: Sealed class v3.0 changes --- source/fundamentals/serialization.txt | 2 +- source/upgrade/v3.txt | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/fundamentals/serialization.txt b/source/fundamentals/serialization.txt index 0203ce86..2051b735 100644 --- a/source/fundamentals/serialization.txt +++ b/source/fundamentals/serialization.txt @@ -84,7 +84,7 @@ The following code example shows a custom ``BsonRegularExpression`` serializer: .. code-block:: csharp - class CustomRegularExpressionSerializer : SerializerBase + class CustomRegularExpressionSerializer : IBsonSerializer { public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { diff --git a/source/upgrade/v3.txt b/source/upgrade/v3.txt index 0cd51180..fd92ff5a 100644 --- a/source/upgrade/v3.txt +++ b/source/upgrade/v3.txt @@ -203,4 +203,11 @@ Version 3.0 Breaking Changes property instead. - The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for - cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__. \ No newline at end of file + cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__. + +- All concrete serializers have been sealed. To implement a custom serializer, implement the + ``IBsonSerializer`` interface. + +- The ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes have been + sealed. We recommend using the ``IMongoClient``, ``IMongoDatabase``, and + ``IMongoCollection`` interfaces directly. \ No newline at end of file From d84ef684145b410ab1e93e48d8ab88d8c5813080 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 3 Dec 2024 16:28:33 -0500 Subject: [PATCH 2/4] NR feedback --- source/upgrade/v3.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/upgrade/v3.txt b/source/upgrade/v3.txt index fd92ff5a..51cc46f0 100644 --- a/source/upgrade/v3.txt +++ b/source/upgrade/v3.txt @@ -205,9 +205,9 @@ Version 3.0 Breaking Changes - The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__. -- All concrete serializers have been sealed. To implement a custom serializer, implement the +- The driver seals all concrete serializers. To implement a custom serializer, implement the ``IBsonSerializer`` interface. -- The ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes have been - sealed. We recommend using the ``IMongoClient``, ``IMongoDatabase``, and - ``IMongoCollection`` interfaces directly. \ No newline at end of file +- The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes. + We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection`` + interfaces directly. \ No newline at end of file From 14a72c44e114ca913f2ae87ac93422f636e94768 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 4 Dec 2024 16:19:01 -0500 Subject: [PATCH 3/4] Technical feedback --- source/fundamentals/serialization.txt | 42 ++++++++++++++------------- source/upgrade/v3.txt | 13 +++++---- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/source/fundamentals/serialization.txt b/source/fundamentals/serialization.txt index 2051b735..bf279c2b 100644 --- a/source/fundamentals/serialization.txt +++ b/source/fundamentals/serialization.txt @@ -76,35 +76,37 @@ of the ``BsonSerializer`` class as follows: Custom Serializers ~~~~~~~~~~~~~~~~~~ -In some cases, you might need to create a custom serializer. When creating a -custom serializer, implement the ``SerializerBase`` abstract base class and -override the ``Deserialize()`` and ``Serialize()`` methods. +To create your own custom serializer, implement the ``IBsonSerializer`` base class, set +the ``ValueType`` member, and override the ``Deserialize()`` and ``Serialize()`` methods. The following code example shows a custom ``BsonRegularExpression`` serializer: .. code-block:: csharp - class CustomRegularExpressionSerializer : IBsonSerializer + class CustomRegularExpressionSerializer : IBsonSerializer { - public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) - { - var type = context.Reader.GetCurrentBsonType(); + public Type ValueType => typeof(Regex); + + public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) + { + var type = context.Reader.CurrentBsonType; switch (type) { - case BsonType.RegularExpression: - return context.Reader.ReadRegularExpression().AsRegex; - case BsonType.String: - var pattern = context.Reader.ReadString(); - return new Regex(pattern); - default: - throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression."); + case BsonType.RegularExpression: + return context.Reader.ReadRegularExpression().AsRegex; + case BsonType.String: + var pattern = context.Reader.ReadString() + return new Regex(pattern); + default: + throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression."); } - } - - public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Regex value) - { - context.Writer.WriteRegularExpression(value); - } + } + + public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) + { + var regex = (Regex) value; + context.Writer.WriteRegularExpression(regex); + } } Opt-in Interfaces diff --git a/source/upgrade/v3.txt b/source/upgrade/v3.txt index 51cc46f0..5aea2738 100644 --- a/source/upgrade/v3.txt +++ b/source/upgrade/v3.txt @@ -205,9 +205,12 @@ Version 3.0 Breaking Changes - The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__. -- The driver seals all concrete serializers. To implement a custom serializer, implement the - ``IBsonSerializer`` interface. +- The driver has sealed some types that were not designed for extension by using inheritance. + This includes the following changes: -- The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes. - We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection`` - interfaces directly. \ No newline at end of file + - The driver seals all concrete serializers. To implement a custom serializer, implement the + ``IBsonSerializer`` interface. + + - The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes. + We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection`` + interfaces directly. \ No newline at end of file From 72440398d8a6483c10b20b915cdc668db33239eb Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 4 Dec 2024 16:21:10 -0500 Subject: [PATCH 4/4] Fix --- source/fundamentals/serialization.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/source/fundamentals/serialization.txt b/source/fundamentals/serialization.txt index bf279c2b..f5a0bfce 100644 --- a/source/fundamentals/serialization.txt +++ b/source/fundamentals/serialization.txt @@ -157,4 +157,3 @@ guide, see the following API documentation: - `SerializerRegistry <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.BsonSerializer.SerializerRegistry.html>`__ - `BsonSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.BsonSerializer.html>`__ - `IBsonSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.IBsonSerializer.html>`__ -- `SerializerBase <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.SerializerBase-1.html>`__