Skip to content

Commit 0b68777

Browse files
authored
DOCSP-26286 - Class maps (#46)
* rr feedback * rr feedback 2
1 parent 65462f9 commit 0b68777

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

source/fundamentals/class-mapping.txt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ allowing the driver to automatically map the remaining properties. To do this,
7272
register a class map and call the ``AutoMap()`` method before manually
7373
specifying your properties.
7474

75-
The following example uses the ``AutoMap()`` method to automatically map the properties
76-
of the ``Person`` class. It then manually maps the ``Hobbies`` property.
75+
In the following code example, the ``AutoMap()`` method maps all properties
76+
of the ``Person`` class except ``Hobbies``, which is mapped manually:
7777

7878
.. code-block:: csharp
7979

@@ -87,7 +87,7 @@ Customize Class Serialization
8787
-----------------------------
8888

8989
You can customize how the driver serializes and deserializes documents at the class
90-
level by using attributes with the class, or by calling methods while registering
90+
level by using attributes with the class or by calling methods while registering
9191
a class map.
9292

9393
Ignore Extra Elements
@@ -96,7 +96,9 @@ Ignore Extra Elements
9696
When a BSON document is deserialized to a {+language+} class, the {+driver-short+}
9797
looks at the name of each field in the document and tries to find a matching property
9898
name in the class. By default, if a field in the document doesn't have a match in the class,
99-
the driver throws an exception. However, you can choose to ignore any elements that do
99+
the driver throws an exception.
100+
101+
You can choose to ignore any elements that do
100102
not have a matching class property by using the ``BsonIgnoreExtraElements`` attribute.
101103
This prevents the driver from throwing an exception, and maps any other fields
102104
that have matching class properties.
@@ -156,12 +158,10 @@ You can also specify a discriminator when registering a class map as follows:
156158
classmap.SetDiscriminator("personClass");
157159
});
158160

159-
.. note::
160-
161-
The driver stores your discriminator in BSON with the field name ``_t``.
161+
In BSON, discriminators have the field name ``_t``.
162162

163-
A document from the ``Person`` class with the "personClass" discriminator is
164-
stored in the collection as follows:
163+
The following example shows how a document from the ``Person`` class with the
164+
"personClass" discriminator appears in the collection after serialization:
165165

166166
.. code-block:: json
167167

@@ -172,16 +172,18 @@ stored in the collection as follows:
172172
Mapping with Constructors
173173
-------------------------
174174

175-
By default the {+driver-short+} requires a class to have a constructor with no
176-
arguments in order to automatically create a class map. However, you can
177-
instruct the driver to automatically map the class using a constructor that takes one
178-
or more arguments.
175+
By default, the {+driver-short+} can automatically map a class only if the class has
176+
a constructor with no arguments. If you want the driver to use a constructor that accepts
177+
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+
constructor arguments to class properties or fields.
179180

180-
The following example uses attributes to instruct the driver to create
181-
a class map with a constructor that accepts multiple arguments.
181+
When the driver creates a class map for the following ``Person`` class, it will use the
182+
constructor marked with the ``BsonConstructor`` attribute. The ``name`` argument will
183+
map to the ``Name`` property and the ``age`` argument will map to the ``Age`` property.
182184

183185
.. code-block:: csharp
184-
:emphasize-lines: 6
186+
:emphasize-lines: 7
185187

186188
public class Person
187189
{
@@ -199,7 +201,7 @@ a class map with a constructor that accepts multiple arguments.
199201

200202
.. tip:: Multiple ``BsonConstructor`` attributes
201203

202-
If there are more than one constructors with the ``BsonConstructor``
204+
If there is more than one constructor with the ``BsonConstructor``
203205
attribute, the driver uses the constructor that has the most
204206
parameters with matching fields in the document.
205207

@@ -230,9 +232,9 @@ You can also specify the constructor to use when registering your class map:
230232
Customize Property Serialization
231233
--------------------------------
232234

233-
You can customize how the driver serializes specific properties in a class by
234-
using attributes on the property. For more information on ways to customize
235-
properties in your {+language+} class, see :ref:`csharp-custom-serialization`.
235+
You can customize how the driver serializes a class property by
236+
adding attributes to the property. For more information about custom
237+
property serialization, see :ref:`csharp-custom-serialization`.
236238

237239
Support Extra Elements
238240
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)