Skip to content

Commit

Permalink
DOCSP-45822 Add convention packs section (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-smith721 authored Feb 7, 2025
1 parent 9236b14 commit 2ee7961
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
52 changes: 52 additions & 0 deletions source/fundamentals/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,55 @@ Implementing the `IBsonArraySerializer
interface enables the driver to access serialization information for individual
items in an array.

.. _csharp-conventions:

Conventions
-----------

Convention packs allow you to define and apply
**conventions** to your classes and their members that the driver uses during serialization.
Conventions specify how data is mapped between your
{+language+} objects and MongoDB documents without requiring you to decorate
each class with attributes.

The {+driver-short+} provides built-in conventions that you can use to
customize the serialization process. The following table describes some of the
built-in conventions:

.. list-table::
:header-rows: 1
:widths: 40 60

* - Convention
- Description

* - `CamelCaseElementNameConvention <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.CamelCaseElementNameConvention.html>`__
- Converts element names to camel case during serialization and deserialization.

* - `EnumRepresentationConvention <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.EnumRepresentationConvention.html>`__
- Converts enum values to a specified representation during serialization
and deserialization.

* - `IgnoreExtraElementsConvention <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.IgnoreExtraElementsConvention.html>`__
- Specifies whether to ignore extra elements in a document during deserialization.

* - `ObjectSerializerAllowedTypesConvention <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.ObjectSerializerAllowedTypesConvention.html>`__
- Specifies which types are allowed to be serialized using an object
serializer.

To view a full list of available conventions, see the `Conventions
<{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.html>`__
class reference in the API Documentation.

You can register a convention by instantiating a ``ConventionPack`` with the
specified conventions, then passing it to the
``ConventionRegistry.Register()`` method, as shown in the following example:

.. code-block:: csharp

var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCaseConvention", camelCaseConvention, t => true);

Additional Information
----------------------

Expand All @@ -157,3 +206,6 @@ 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>`__
- `ConventionPack <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.ConventionPack.html>`__
- `ConventionRegistry <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.ConventionRegistry.html>`__
- `Register() <{+new-api-root+}/MongoDB.Bson.Serialization.Conventions.ConventionRegistry.Register.html#MongoDB_Bson_Serialization_Conventions_ConventionRegistry_Register_System_String_MongoDB_Bson_Serialization_Conventions_IConventionPack_System_Func_System_Type_System_Boolean__>`__
3 changes: 3 additions & 0 deletions source/fundamentals/serialization/poco.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ MongoDB collection.

var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);

For more information about conventions, see the
:ref:`csharp-conventions` section of the Serialization guide.

Select Type Representation
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
13 changes: 13 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ What's New

Learn what's new in:

* :ref:`Version 3.2 <csharp-version-3.2>`
* :ref:`Version 3.1 <csharp-version-3.1>`
* :ref:`Version 3.0 <csharp-version-3.0>`
* :ref:`Version 2.30 <csharp-version-2.30>`
Expand All @@ -33,6 +34,18 @@ Learn what's new in:

.. _upcoming-breaking-changes:

.. _csharp-version-3.2:

What's New in 3.2
-----------------

The 3.2 driver release includes the following new features:

- Adds a new ``ObjectSerializerAllowedTypesConvention`` convention that
allows you to specify which types are allowed to be serialized and
deserialized by the object serializer. To learn more about conventions,
see the :ref:`csharp-conventions` guide.

.. _csharp-version-3.1:

What's New in 3.1
Expand Down

0 comments on commit 2ee7961

Please sign in to comment.