Skip to content

Commit 2eeeaec

Browse files
DOCSP-41170 - Clarify string ID generation (#365) (#383)
Co-authored-by: Jordan Smith <[email protected]> (cherry picked from commit 30ff13b) Co-authored-by: Mike Woofter <[email protected]>
1 parent ead284a commit 2eeeaec

File tree

1 file changed

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

1 file changed

+165
-102
lines changed

source/fundamentals/serialization/poco.txt

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

181182
.. code-block:: csharp
182183
:copyable: true
@@ -247,7 +248,7 @@ The following code sample maps the ``Identifier`` property to the
247248
public string Identifier { get; set; }
248249
}
249250

250-
.. warning:: Multiple Id Fields
251+
.. warning:: Multiple ID Fields
251252

252253
If you identify more than one property as the ``_id`` field using the
253254
``[BsonId()]`` attribute, the driver throws a
@@ -261,6 +262,167 @@ The following code sample maps the ``Identifier`` property to the
261262
The ``_id`` field mapping logic described in this section only applies to the
262263
root document and does not apply to nested documents.
263264

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

@@ -377,106 +539,7 @@ The previous code example sets the following serialization behavior:
377539
- Because ``1900`` is the default value for this property, the driver will ignore the
378540
property if it has this value.
379541

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

471-
.. code-block:: csharp
472-
:copyable: false
473-
:emphasize-lines: 3
474-
475-
public class House
476-
{
477-
[BsonId(IdGenerator = typeof(NullIdChecker))]
478-
public Guid Id { get; set; }
479-
}
480543

481544
Customize DateTime Serialization
482545
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)