@@ -21,10 +21,13 @@ Overview
21
21
--------
22
22
23
23
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
25
25
to learn more about the default class mapping behavior, or if you need to
26
26
customize the way the driver serializes or deserializes your data.
27
27
28
+ You can also use POCO attributes to set serialization behavior. To learn
29
+ more, see the :ref:`csharp-poco` guide.
30
+
28
31
Automatic Class Mapping
29
32
-----------------------
30
33
69
72
classMap.MapMember(p => p.Hobbies);
70
73
});
71
74
72
- .. important::
75
+ .. important:: When to Register a Class Map
73
76
74
77
You must register a class map *before* it's needed in your code. We recommend
75
78
registering class maps prior to initializing a connection with MongoDB.
@@ -95,9 +98,34 @@ Customize Class Serialization
95
98
-----------------------------
96
99
97
100
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
99
102
a class map.
100
103
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
+
101
129
Ignore Extra Elements
102
130
~~~~~~~~~~~~~~~~~~~~~
103
131
@@ -134,6 +162,89 @@ You can also ignore any extra elements when registering a class map:
134
162
classMap.SetIgnoreExtraElements(true);
135
163
});
136
164
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
+
137
248
Mapping with Constructors
138
249
-------------------------
139
250
0 commit comments