Skip to content

Commit 663bde2

Browse files
authored
DOCSP-42606: class maps feedback (#592)
* DOCSP-42606: class maps improvements * wip * RM PR fixes 1 * BD tech review comments 1 * BD tech review comments 2
1 parent 46f9b7b commit 663bde2

File tree

2 files changed

+181
-9
lines changed

2 files changed

+181
-9
lines changed

source/fundamentals/serialization/class-mapping.txt

+114-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ Overview
2121
--------
2222

2323
In this guide, you can learn how to customize the way the {+driver-long+}
24-
maps BSON documents to and from {+language+} classes. You should read this page
24+
maps BSON documents to and from {+language+} classes. You can read this page
2525
to learn more about the default class mapping behavior, or if you need to
2626
customize the way the driver serializes or deserializes your data.
2727

28+
You can also use POCO attributes to set serialization behavior. To learn
29+
more, see the :ref:`csharp-poco` guide.
30+
2831
Automatic Class Mapping
2932
-----------------------
3033

@@ -69,7 +72,7 @@ class:
6972
classMap.MapMember(p => p.Hobbies);
7073
});
7174

72-
.. important::
75+
.. important:: When to Register a Class Map
7376

7477
You must register a class map *before* it's needed in your code. We recommend
7578
registering class maps prior to initializing a connection with MongoDB.
@@ -95,9 +98,34 @@ Customize Class Serialization
9598
-----------------------------
9699

97100
You can customize how the driver serializes and deserializes documents at the class
98-
level by using attributes with the class or by calling methods while registering
101+
level by applying attributes to the class or by calling methods while registering
99102
a class map.
100103

104+
.. _csharp-class-map-omit-fields:
105+
106+
Omit Fields
107+
~~~~~~~~~~~
108+
109+
You can prevent specified fields from being serialized by using the
110+
``UnmapMember()`` method when registering a class map.
111+
112+
The following example shows how to create a class map to prevent the
113+
``Age`` property from being serialized:
114+
115+
.. code-block:: csharp
116+
:emphasize-lines: 4
117+
118+
BsonClassMap.RegisterClassMap<Person>(classMap =>
119+
{
120+
classMap.AutoMap();
121+
classMap.UnmapMember(p => p.Age);
122+
});
123+
124+
.. tip::
125+
126+
To learn about how to set this behavior by using a field attribute,
127+
see the :ref:`csharp-poco-omit-fields` section of the POCOs guide.
128+
101129
Ignore Extra Elements
102130
~~~~~~~~~~~~~~~~~~~~~
103131

@@ -134,6 +162,89 @@ You can also ignore any extra elements when registering a class map:
134162
classMap.SetIgnoreExtraElements(true);
135163
});
136164

165+
Identify Id Field
166+
~~~~~~~~~~~~~~~~~
167+
168+
By default, the driver maps any public property named ``Id``, ``id``, or
169+
``_id`` to the BSON ``_id`` field. To explicitly select the
170+
property to map to the ``_id`` field, use the ``MapIdMember()`` class
171+
map method.
172+
173+
The following code sample maps the ``Identifier`` property to the
174+
``_id`` field:
175+
176+
.. code-block:: csharp
177+
:emphasize-lines: 4
178+
179+
BsonClassMap.RegisterClassMap<Person>(classMap =>
180+
{
181+
classMap.AutoMap();
182+
classMap.MapIdMember(p => p.Identifier);
183+
});
184+
185+
String IDs
186+
``````````
187+
188+
If you are using strings to represent your
189+
documement IDs, you can use the ``IdMemberMap.SetSerializer()`` class
190+
map method to serialize these values to ``ObjectId`` instances in
191+
MongoDB. This way, you can customize how your ID values are represented
192+
in your application while adhering to MongoDB best practices for ID
193+
management in the database.
194+
195+
The following example directs the driver to serialize string ID values
196+
as ``ObjectId`` values:
197+
198+
.. code-block:: csharp
199+
:emphasize-lines: 4
200+
201+
BsonClassMap.RegisterClassMap<Person>(classMap =>
202+
{
203+
classMap.AutoMap();
204+
classMap.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
205+
});
206+
207+
If you want the driver to generate valid 24-digit hex strings to
208+
use as your ID values, you can chain the ``SetIdGenerator()`` method to
209+
the ``MapIdMember()`` method and pass an instance of
210+
``StringObjectIdGenerator``:
211+
212+
.. code-block:: csharp
213+
:emphasize-lines: 4
214+
215+
BsonClassMap.RegisterClassMap<Person>(classMap =>
216+
{
217+
classMap.AutoMap();
218+
classMap.MapIdMember(p => p.Identifier).SetIdGenerator(StringObjectIdGenerator.Instance);
219+
});
220+
221+
To learn more about ID generators, see the
222+
:ref:`csharp-poco-specify-id-gen` section of the POCOs guide.
223+
224+
GUID IDs
225+
````````
226+
227+
If your application uses GUIDs as unique identifiers in your documents,
228+
you can register a class map to specify how ``Guid`` values are
229+
serialized. By default, the driver uses the
230+
``GuidGenerator`` type to generate a unique value for the ID property.
231+
You must specify the GUID representation by initializing a
232+
``GuidSerializer``.
233+
234+
The following code specifies the ``Standard`` GUID representation when
235+
registering the class map:
236+
237+
.. code-block:: csharp
238+
:emphasize-lines: 4
239+
240+
BsonClassMap.RegisterClassMap<Person>(classMap =>
241+
{
242+
classMap.AutoMap();
243+
classMap.IdMemberMap.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
244+
});
245+
246+
To learn more about serializing GUIDs, see the :ref:`csharp-guids` guide.
247+
137248
Mapping with Constructors
138249
-------------------------
139250

source/fundamentals/serialization/poco.txt

+67-6
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ The following code sample maps the ``Identifier`` property to the
266266
The ``_id`` field mapping logic described in this section only applies to the
267267
root document and does not apply to nested documents.
268268

269+
.. _csharp-poco-specify-id-gen:
270+
269271
Specify an ID Generator
270272
```````````````````````
271273

@@ -312,7 +314,9 @@ The following table describes the ID generators available in the
312314
public class House
313315
{
314316
public Guid Id { get; set; }
315-
}
317+
}
318+
319+
To learn more, see the :ref:`csharp-guids` guide.
316320

317321
* - ``ObjectId``
318322
- The driver automatically uses the ``ObjectIdGenerator`` type for ID properties with
@@ -427,13 +431,45 @@ the default value (``null``), the driver throws an exception:
427431
public Guid Id { get; set; }
428432
}
429433

434+
.. _csharp-poco-omit-fields:
435+
436+
Omit Fields
437+
~~~~~~~~~~~
438+
439+
To prevent the driver from serializing a specified field, use the
440+
``[BsonIgnore]`` attribute. The following code shows how you can prevent
441+
the driver from serializing the ``YearBuilt`` property:
442+
443+
.. code-block:: csharp
444+
:copyable: true
445+
:emphasize-lines: 5
446+
447+
public class House
448+
{
449+
public Guid Id { get; set; }
450+
451+
[BsonIgnore]
452+
public int YearBuilt { get; set; }
453+
public string Style { get; set; }
454+
}
455+
456+
Even if you define the ``YearBuilt`` property, the field is
457+
not saved in MongoDB.
458+
459+
.. note::
460+
461+
You can define a class map to prevent specific fields from being
462+
serialized. To learn more and view an example, see the
463+
:ref:`csharp-class-map-omit-fields` section of the Class Mapping
464+
guide.
465+
430466
Omit Empty Fields
431467
~~~~~~~~~~~~~~~~~
432468

433-
By default, the driver serializes undefined properties to fields with ``null``
434-
values. To ignore undefined properties during serialization, use the ``[BsonIgnore]``
469+
By default, the driver serializes uninitialized properties with ``null``
470+
values. To ignore empty properties during serialization, use the ``[BsonIgnoreIfNull]``
435471
attribute. The following code shows how you can prevent the driver from
436-
serializing the ``YearBuilt`` property if it is undefined:
472+
serializing the ``Style`` property if it is uninitialized:
437473

438474
.. code-block:: csharp
439475
:copyable: true
@@ -443,11 +479,36 @@ serializing the ``YearBuilt`` property if it is undefined:
443479
{
444480
public Guid Id { get; set; }
445481

446-
[BsonIgnore]
447-
public int YearBuilt { get; set; }
482+
[BsonIgnoreIfNull]
448483
public string Style { get; set; }
484+
public int YearBuilt { get; set; }
449485
}
450486

487+
You can also instruct the driver to ignore a property that contains a
488+
``null`` value when you register the class map, as shown in the
489+
following example:
490+
491+
.. code-block:: csharp
492+
:copyable: true
493+
:emphasize-lines: 4
494+
495+
BsonClassMap.RegisterClassMap<House>(classMap =>
496+
{
497+
classMap.AutoMap();
498+
classMap.MapMember(h => h.Style).SetIgnoreIfNull(true);
499+
});
500+
501+
.. note:: Value Type Properties
502+
503+
You cannot use the ``[BsonIgnoreIfNull]`` attribute or
504+
``SetIgnoreIfNull()`` method to prevent uninitialized value type
505+
properties from being serialized unless you mark the properties as
506+
nullable. Instead, use the ``[BsonIgnoreIfDefault]`` attribute or
507+
``SetIgnoreIfDefault()`` class map method, which are described in the
508+
:ref:`csharp-poco-default-values` section of this guide.
509+
510+
.. _csharp-poco-default-values:
511+
451512
Customize Default Values
452513
~~~~~~~~~~~~~~~~~~~~~~~~
453514

0 commit comments

Comments
 (0)