Skip to content

Commit 668a306

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 d62f8d8 commit 668a306

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
@@ -262,6 +262,8 @@ The following code sample maps the ``Identifier`` property to the
262262
The ``_id`` field mapping logic described in this section only applies to the
263263
root document and does not apply to nested documents.
264264

265+
.. _csharp-poco-specify-id-gen:
266+
265267
Specify an ID Generator
266268
```````````````````````
267269

@@ -308,7 +310,9 @@ The following table describes the ID generators available in the
308310
public class House
309311
{
310312
public Guid Id { get; set; }
311-
}
313+
}
314+
315+
To learn more, see the :ref:`csharp-guids` guide.
312316

313317
* - ``ObjectId``
314318
- The driver automatically uses the ``ObjectIdGenerator`` type for ID properties with
@@ -423,13 +427,45 @@ the default value (``null``), the driver throws an exception:
423427
public Guid Id { get; set; }
424428
}
425429

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

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

434470
.. code-block:: csharp
435471
:copyable: true
@@ -439,11 +475,36 @@ serializing the ``YearBuilt`` property if it is undefined:
439475
{
440476
public Guid Id { get; set; }
441477

442-
[BsonIgnore]
443-
public int YearBuilt { get; set; }
478+
[BsonIgnoreIfNull]
444479
public string Style { get; set; }
480+
public int YearBuilt { get; set; }
445481
}
446482

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

0 commit comments

Comments
 (0)