Skip to content

Commit af692b4

Browse files
committed
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 (cherry picked from commit 663bde2)
1 parent 69cdd9c commit af692b4

File tree

2 files changed

+181
-9
lines changed

2 files changed

+181
-9
lines changed

source/fundamentals/serialization/class-mapping.txt

Lines changed: 114 additions & 3 deletions
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

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

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

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

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

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

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

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

445-
[BsonIgnore]
446-
public int YearBuilt { get; set; }
481+
[BsonIgnoreIfNull]
447482
public string Style { get; set; }
483+
public int YearBuilt { get; set; }
448484
}
449485

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

0 commit comments

Comments
 (0)