@@ -31,7 +31,7 @@ the name of the field in the document to the name of the property in the class.
31
31
The type of the property in your class should match the type of the field in
32
32
the document. The {+driver-short+} instantiates a serializer based on the
33
33
type of the property in your class. If the types don't match when the driver
34
- attempts to serialize the data, the serializer will throw an exception.
34
+ attempts to deserialize the data, the serializer throws an exception.
35
35
36
36
Manually Creating A Class Map
37
37
-----------------------------
@@ -55,11 +55,11 @@ class:
55
55
56
56
.. code-block:: csharp
57
57
58
- BsonClassMap.RegisterClassMap<Person>(classmap =>
58
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
59
59
{
60
- classmap .MapMember(p => p.Name);
61
- classmap .MapMember(p => p.Age);
62
- classmap .MapMember(p => p.Hobbies);
60
+ classMap .MapMember(p => p.Name);
61
+ classMap .MapMember(p => p.Age);
62
+ classMap .MapMember(p => p.Hobbies);
63
63
});
64
64
65
65
.. important::
@@ -73,14 +73,15 @@ register a class map and call the ``AutoMap()`` method before manually
73
73
specifying your properties.
74
74
75
75
In the following code example, the ``AutoMap()`` method maps all properties
76
- of the ``Person`` class except ``Hobbies``, which is mapped manually:
76
+ of the ``Person`` class, then manually adjusts the mapping for the ``Hobbies``
77
+ field:
77
78
78
79
.. code-block:: csharp
79
80
80
- BsonClassMap.RegisterClassMap<Person>(classmap =>
81
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
81
82
{
82
- classmap .AutoMap();
83
- classmap .MapMember(p => p.Hobbies);
83
+ classMap .AutoMap();
84
+ classMap .MapMember(p => p.Hobbies).SetElementName("favorite_hobbies" );
84
85
});
85
86
86
87
Customize Class Serialization
@@ -120,10 +121,10 @@ You can also ignore any extra elements when registering a class map:
120
121
121
122
.. code-block:: csharp
122
123
123
- BsonClassMap.RegisterClassMap<Person>(classmap =>
124
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
124
125
{
125
- classmap .AutoMap();
126
- classmap .SetIgnoreExtraElements(true);
126
+ classMap .AutoMap();
127
+ classMap .SetIgnoreExtraElements(true);
127
128
});
128
129
129
130
Using Class Discriminators
@@ -152,10 +153,10 @@ You can also specify a discriminator when registering a class map as follows:
152
153
153
154
.. code-block:: csharp
154
155
155
- BsonClassMap.RegisterClassMap<Person>(classmap =>
156
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
156
157
{
157
- classmap .AutoMap();
158
- classmap .SetDiscriminator("personClass");
158
+ classMap .AutoMap();
159
+ classMap .SetDiscriminator("personClass");
159
160
});
160
161
161
162
In BSON, discriminators have the field name ``_t``.
@@ -165,7 +166,7 @@ The following example shows how a document from the ``Person`` class with the
165
166
166
167
.. code-block:: json
167
168
168
- { "_id": "...", "_t": "personClass", "name ": "...", "age ": "...", "hobbies ": [...]}
169
+ { "_id": "...", "_t": "personClass", "Name ": "...", "Age ": "...", "Hobbies ": [...]}
169
170
170
171
.. TODO: Link to page on polymorphism/discriminators
171
172
@@ -175,7 +176,7 @@ Mapping with Constructors
175
176
By default, the {+driver-short+} can automatically map a class only if the class has
176
177
a constructor with no arguments. If you want the driver to use a constructor that accepts
177
178
one or more arguments, you can add the ``BsonConstructor`` attribute to the constructor.
178
- In this case, the driver parses the expression tree to determine how to map the
179
+ In this case, the driver the driver examines the types to determine how to map the
179
180
constructor arguments to class properties or fields.
180
181
181
182
When the driver creates a class map for the following ``Person`` class, it will use the
@@ -223,10 +224,10 @@ You can also specify the constructor to use when registering your class map:
223
224
}
224
225
}
225
226
226
- BsonClassMap.RegisterClassMap<Person>(classmap =>
227
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
227
228
{
228
- classmap .AutoMap();
229
- classmap .MapCreator(p => new Person(p.Name, p.Age));
229
+ classMap .AutoMap();
230
+ classMap .MapCreator(p => new Person(p.Name, p.Age));
230
231
});
231
232
232
233
Customize Property Serialization
@@ -261,10 +262,10 @@ You can also support extra elements when initializing a class map as follows:
261
262
262
263
.. code-block:: csharp
263
264
264
- BsonClassMap.RegisterClassMap<Person>(classmap =>
265
+ BsonClassMap.RegisterClassMap<Person>(classMap =>
265
266
{
266
- classmap .AutoMap();
267
- classmap .MapExtraElementsMember(p => p.ExtraElements);
267
+ classMap .AutoMap();
268
+ classMap .MapExtraElementsMember(p => p.ExtraElements);
268
269
});
269
270
270
271
.. note::
@@ -277,14 +278,15 @@ Dynamically Serialize Properties
277
278
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278
279
279
280
You can use a method to determine whether or not to serialize a property. For
280
- the driver to automatically use the method when serializing, the method must be
281
- named ``ShouldSerialize<property name>``. When the driver sees a method with
282
- this naming convention, it will use the method to determine whether or not to
283
- serialize properties of the provided property name.
281
+ the driver to automatically use the method when serializing, you must prefix the
282
+ method name with ``ShouldSerialize`` followed by the name of the property that
283
+ the method applies to. When the driver sees a method with this naming
284
+ convention, it uses that method to determine whether or not to serialize
285
+ properties that have the provided property name.
284
286
285
287
The following example creates a method that only serializes the ``Age`` property
286
- if its value is greater than ``0``. The driver ignores any properties whose
287
- values don't meet this requirement:
288
+ if its value is not equal to ``0``. The driver does not serialize any properties
289
+ whose values don't meet this requirement:
288
290
289
291
.. code-block:: csharp
290
292
@@ -296,19 +298,19 @@ values don't meet this requirement:
296
298
297
299
public bool ShouldSerializeAge()
298
300
{
299
- return Age > 0;
301
+ return Age != 0;
300
302
}
301
303
}
302
304
303
305
You can also specify the method while registering a class map:
304
306
305
307
.. code-block:: csharp
306
308
307
- BsonClassMap.RegisterClassMap<Employee>(classmap =>
309
+ BsonClassMap.RegisterClassMap<Employee>(classMap =>
308
310
{
309
- classmap .AutoMap();
310
- classmap .MapMember(p => c.Age).SetShouldSerializeMethod(
311
- obj => ((Person) obj).Age > 0
311
+ classMap .AutoMap();
312
+ classMap .MapMember(p => c.Age).SetShouldSerializeMethod(
313
+ obj => ((Person) obj).Age != 0
312
314
);
313
315
});
314
316
0 commit comments