Skip to content

Commit 2539e6e

Browse files
DOCSP-41170 - Clarify string ID generation (#365) (#384)
Co-authored-by: Jordan Smith <[email protected]> (cherry picked from commit 30ff13b) Co-authored-by: Mike Woofter <[email protected]>
1 parent 72fbfd2 commit 2539e6e

File tree

1 file changed

+165
-102
lines changed
  • source/fundamentals/serialization

1 file changed

+165
-102
lines changed

source/fundamentals/serialization/poco.txt

Lines changed: 165 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ Select Type Representation
176176
To serialize a {+language+} property to a specific BSON type, use the
177177
``[BsonRepresentation()]`` attribute. This works only if the
178178
{+language+} primitive type is convertible to the BSON type you specify.
179-
In the following code sample, the ``YearBuilt`` property, defined as a
180-
``char`` in {+language+}, is serialized as a BSON ``Int32`` type:
179+
180+
In the following code sample, the driver serializes the ``YearBuilt`` property,
181+
defined as a ``char`` in {+language+}, as a BSON ``Int32`` type:
181182

182183
.. code-block:: csharp
183184
:copyable: true
@@ -242,7 +243,7 @@ The following code sample maps the ``Identifier`` property to the
242243
public string Identifier { get; set; }
243244
}
244245

245-
.. warning:: Multiple Id Fields
246+
.. warning:: Multiple ID Fields
246247

247248
If you identify more than one property as the ``_id`` field using the
248249
``[BsonId()]`` attribute, the driver throws a
@@ -256,6 +257,167 @@ The following code sample maps the ``Identifier`` property to the
256257
The ``_id`` field mapping logic described in this section only applies to the
257258
root document and does not apply to nested documents.
258259

260+
Specify an ID Generator
261+
```````````````````````
262+
263+
Every document in a MongoDB collection must have a unique ID. When you serialize an object
264+
to a collection, if its ID property contains the default
265+
value for its data type (usually ``null``), the {+driver-short+} doesn't serialize the
266+
default value. Instead, the driver tries to generate a unique ID value and assign it to the
267+
property.
268+
269+
To enable ID generation for a property, you must specify the ID generator the driver
270+
uses for the property. You can do so by applying the ``[BsonId]`` attribute to the property
271+
and passing the ``IdGenerator`` argument to specify the generator type.
272+
The following table describes the ID generators available in the
273+
{+driver-short+}:
274+
275+
.. list-table::
276+
:header-rows: 1
277+
:widths: 25 75
278+
279+
* - Id Field Data Type
280+
- How to Use
281+
282+
* - ``Guid``
283+
- To use the COMB algorithm to generate a unique ``Guid`` value, apply the
284+
``[BsonId(IdGenerator = typeof(CombGuidGenerator))]`` attribute to the ID
285+
property, as shown in the following example:
286+
287+
.. code-block:: csharp
288+
:copyable: true
289+
290+
public class House
291+
{
292+
[BsonId(IdGenerator = typeof(CombGuidGenerator))]
293+
public Guid Id { get; set; }
294+
}
295+
296+
To generate a unique ``Guid`` value without the COMB algorithm, don't apply
297+
the preceding attribute to the ID property. The driver automatically uses
298+
the ``GuidGenerator`` type to generate a unique value for the ID property.
299+
300+
.. code-block:: csharp
301+
:copyable: true
302+
303+
public class House
304+
{
305+
public Guid Id { get; set; }
306+
}
307+
308+
* - ``ObjectId``
309+
- The driver automatically uses the ``ObjectIdGenerator`` type for ID properties with
310+
the ``ObjectId`` data type, such as the one in the following code example:
311+
312+
.. code-block:: csharp
313+
:copyable: true
314+
315+
public class House
316+
{
317+
public ObjectId Id { get; set; }
318+
}
319+
320+
* - ``string``
321+
- If you specify that an ID property with the ``string`` data type is serialized
322+
as an ``ObjectId``, the driver automatically uses the
323+
``StringObjectIdGenerator`` to generate a unique ``string`` value for the property.
324+
In the following code example, the driver uses the ``StringObjectIdGenerator``
325+
for the ``Id`` property:
326+
327+
.. code-block:: csharp
328+
:copyable: true
329+
330+
public class House
331+
{
332+
[BsonRepresentation(BsonType.ObjectId)]
333+
public string Id { get; set; }
334+
}
335+
336+
To generate a unique ``string`` value for an ID property that is not serialized
337+
as an ``ObjectId``, apply the
338+
``[BsonID(IdGenerator = typeof(StringObjectIdGenerator))]`` attribute to the
339+
property, as shown in the following code example. The driver
340+
uses the ``StringObjectIdGenerator`` type to generate a unique ``ObjectId`` value,
341+
convert it to a ``string``, and assign it to the property.
342+
343+
.. code-block:: csharp
344+
:copyable: true
345+
346+
public class House
347+
{
348+
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
349+
public string Id { get; set; }
350+
}
351+
352+
* - ``BsonObjectId``
353+
- Apply the ``[BsonId(IdGenerator = typeof(BsonObjectIdGenerator))]`` attribute to the
354+
ID property, as shown in the following code example. The driver uses the
355+
``BsonObjectIdGenerator`` type to generate a unique ``BsonObjectId`` value for
356+
the property.
357+
358+
.. code-block:: csharp
359+
:copyable: true
360+
361+
public class House
362+
{
363+
[BsonId(IdGenerator = typeof(BsonObjectIdGenerator))]
364+
public BsonObjectId Id { get; set; }
365+
}
366+
367+
Alternatively, you can specify an ``IIdGenerator`` type when you register the class map,
368+
as shown in the following example:
369+
370+
.. code-block:: csharp
371+
:copyable: true
372+
373+
BsonClassMap.RegisterClassMap<House>(classMap =>
374+
{
375+
classMap.AutoMap();
376+
classMap.MapIdMember(h => h.Id).SetIdGenerator(CombGuidGenerator.Instance);
377+
});
378+
379+
.. tip:: Specify an ``IIdGenerator`` for Multiple Classes
380+
381+
You can use the ``RegisterIdGenerator()`` method to specify a single ``IIdGenerator``
382+
for all ``Id`` properties of a certain data type. The following code example instructs
383+
the driver to use the ``CombGuidGenerator`` type for all ``Guid`` IDs:
384+
385+
.. code-block:: csharp
386+
:copyable: true
387+
388+
BsonSerializer.RegisterIdGenerator(
389+
typeof(Guid),
390+
CombGuidGenerator.Instance
391+
);
392+
393+
The {+driver-short+} also includes ``IIdGenerator`` types that validate the ``Id``
394+
property and throw an exception if the ID is invalid. The following table lists these
395+
types:
396+
397+
.. list-table::
398+
:header-rows: 1
399+
:widths: 10 10
400+
401+
* - ID Validation
402+
- IIdGenerator Type
403+
* - Not null
404+
- ``NullIdChecker``
405+
* - Not all zeroes
406+
- ``ZeroIdChecker<T>``
407+
408+
In the following code example, if the ``Id`` property of the ``House`` class contains
409+
the default value (``null``), the driver throws an exception:
410+
411+
.. code-block:: csharp
412+
:copyable: false
413+
:emphasize-lines: 3
414+
415+
public class House
416+
{
417+
[BsonId(IdGenerator = typeof(NullIdChecker))]
418+
public Guid Id { get; set; }
419+
}
420+
259421
Omit Empty Fields
260422
~~~~~~~~~~~~~~~~~
261423

@@ -372,106 +534,7 @@ The previous code example sets the following serialization behavior:
372534
- Because ``1900`` is the default value for this property, the driver will ignore the
373535
property if it has this value.
374536

375-
Specify an ID Generator
376-
~~~~~~~~~~~~~~~~~~~~~~~
377-
378-
Every document in a MongoDB collection must have a unique ID. When you serialize an object
379-
to a collection, if the ``Id`` property contains the default
380-
value for its data type, the {+driver-short+} doesn't serialize it. Instead, the driver
381-
generates a unique ID value for the property.
382-
383-
If the ``Id`` property is of type ``Guid``, ``ObjectId``, or ``string``, the driver can
384-
generate the ID value on its own. If the ``Id`` property is any other data type, you must
385-
specify the ``IIdGenerator`` type that the driver uses to generate the value.
386-
To specify the ``IIdGenerator`` type, apply the ``[BsonId(IdGenerator)]`` attribute
387-
to the property and pass the ``IIdGenerator`` type as an argument.
388-
389-
The {+driver-short+} includes the following ``IIdGenerator`` types:
390-
391-
.. list-table::
392-
:header-rows: 1
393-
:widths: 10 10
394-
395-
* - ``Id`` Field Data Type
396-
- ``IIdGenerator`` Type
397-
* - ``Guid`` value generated by the COMB algorithm
398-
- ``CombGuidGenerator``
399-
* - ``BsonObjectId``
400-
- ``BsonObjectIdGenerator``
401-
402-
In the following code example, if the ``Id`` property of the ``House`` class contains
403-
the default value (``null``), the driver uses the COMB algorithm to generate a
404-
unique value during serialization:
405-
406-
.. code-block:: csharp
407-
:copyable: false
408-
:emphasize-lines: 3
409-
410-
public class House
411-
{
412-
[BsonId(IdGenerator = typeof(CombGuidGenerator))]
413-
public Guid Id { get; set; }
414-
}
415-
416-
.. note::
417-
418-
In the previous code example, if the ``Id`` property didn't have the
419-
``[BsonId(IdGenerator)]`` attribute, the driver would generate a non-COMB GUID
420-
to assign to the ``Id`` field.
421-
422-
You can also specify an ``IIdGenerator`` type while registering the class map, as shown in
423-
the following example:
424-
425-
.. code-block:: csharp
426-
:copyable: true
427-
428-
BsonClassMap.RegisterClassMap<House>(classMap =>
429-
{
430-
classMap.AutoMap();
431-
classMap.MapIdMember(h => h.Id).SetIdGenerator(CombGuidGenerator.Instance);
432-
});
433-
434-
.. tip:: Specify an ``IIdGenerator`` for Multiple Classes
435-
436-
You can use the ``RegisterIdGenerator()`` method to specify a single ``IIdGenerator``
437-
for all ``Id`` properties of a certain data type. The following code example instructs
438-
the driver to use the ``CombGuidGenerator`` type for all ``Guid`` IDs:
439-
440-
.. code-block:: csharp
441-
:copyable: true
442-
443-
BsonSerializer.RegisterIdGenerator(
444-
typeof(Guid),
445-
CombGuidGenerator.Instance
446-
);
447-
448-
The {+driver-short+} also includes ``IIdGenerator`` types that validate the ``Id``
449-
property and throw an exception if the ID is invalid. The following table lists these
450-
types:
451-
452-
.. list-table::
453-
:header-rows: 1
454-
:widths: 10 10
455-
456-
* - ID Validation
457-
- ``IIdGenerator`` Type
458-
* - Not null
459-
- ``NullIdChecker``
460-
* - Not all zeroes
461-
- ``ZeroIdChecker<T>``
462-
463-
In the following code example, if the ``Id`` property of the ``House`` class contains
464-
the default value (``null``), the driver throws an exception:
465537

466-
.. code-block:: csharp
467-
:copyable: false
468-
:emphasize-lines: 3
469-
470-
public class House
471-
{
472-
[BsonId(IdGenerator = typeof(NullIdChecker))]
473-
public Guid Id { get; set; }
474-
}
475538

476539
Customize DateTime Serialization
477540
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)