27
27
28
28
namespace MongoDB . Bson
29
29
{
30
- // this class is a wrapper for an object that we intend to serialize as a BSON document
30
+ // this class is a wrapper for an object that we intend to serialize as a BsonValue
31
31
// it is a subclass of BsonValue so that it may be used where a BsonValue is expected
32
32
// this class is mostly used by MongoCollection and MongoCursor when supporting generic query objects
33
33
@@ -54,22 +54,18 @@ private BsonDocumentWrapper()
54
54
/// </summary>
55
55
/// <param name="wrappedObject">The wrapped object.</param>
56
56
public BsonDocumentWrapper ( object wrappedObject )
57
- : base ( BsonType . Document )
57
+ : this ( ( wrappedObject == null ) ? typeof ( object ) : wrappedObject . GetType ( ) , wrappedObject )
58
58
{
59
- _wrappedNominalType = ( wrappedObject == null ) ? typeof ( object ) : wrappedObject . GetType ( ) ;
60
- _wrappedObject = wrappedObject ;
61
59
}
62
60
63
61
/// <summary>
64
62
/// Initializes a new instance of the BsonDocumentWrapper class.
65
63
/// </summary>
66
64
/// <param name="wrappedNominalType">The nominal type of the wrapped object.</param>
67
65
/// <param name="wrappedObject">The wrapped object.</param>
68
- public BsonDocumentWrapper ( Type wrappedNominalType , object wrappedObject )
69
- : base ( BsonType . Document )
66
+ public BsonDocumentWrapper ( Type wrappedNominalType , object value )
67
+ : this ( wrappedNominalType , value , false )
70
68
{
71
- _wrappedNominalType = wrappedNominalType ;
72
- _wrappedObject = wrappedObject ;
73
69
}
74
70
75
71
/// <summary>
@@ -78,9 +74,13 @@ public BsonDocumentWrapper(Type wrappedNominalType, object wrappedObject)
78
74
/// <param name="wrappedNominalType">The nominal type of the wrapped object.</param>
79
75
/// <param name="wrappedObject">The wrapped object.</param>
80
76
/// <param name="isUpdateDocument">Whether the wrapped object is an update document that needs to be checked.</param>
81
- internal BsonDocumentWrapper ( Type wrappedNominalType , object wrappedObject , bool isUpdateDocument )
77
+ public BsonDocumentWrapper ( Type wrappedNominalType , object wrappedObject , bool isUpdateDocument )
82
78
: base ( BsonType . Document )
83
79
{
80
+ if ( wrappedNominalType == null )
81
+ {
82
+ throw new ArgumentNullException ( "wrappedNominalType" ) ;
83
+ }
84
84
_wrappedNominalType = wrappedNominalType ;
85
85
_wrappedObject = wrappedObject ;
86
86
_isUpdateDocument = isUpdateDocument ;
@@ -92,7 +92,7 @@ internal BsonDocumentWrapper(Type wrappedNominalType, object wrappedObject, bool
92
92
/// </summary>
93
93
/// <typeparam name="TNominalType">The nominal type of the wrapped object.</typeparam>
94
94
/// <param name="value">The wrapped object.</param>
95
- /// <returns>A BsonDocumentWrapper or null .</returns>
95
+ /// <returns>A BsonDocumentWrapper.</returns>
96
96
public static BsonDocumentWrapper Create < TNominalType > ( TNominalType value )
97
97
{
98
98
return Create ( typeof ( TNominalType ) , value ) ;
@@ -104,7 +104,7 @@ public static BsonDocumentWrapper Create<TNominalType>(TNominalType value)
104
104
/// <typeparam name="TNominalType">The nominal type of the wrapped object.</typeparam>
105
105
/// <param name="value">The wrapped object.</param>
106
106
/// <param name="isUpdateDocument">Whether the wrapped object is an update document.</param>
107
- /// <returns>A BsonDocumentWrapper or null .</returns>
107
+ /// <returns>A BsonDocumentWrapper.</returns>
108
108
public static BsonDocumentWrapper Create < TNominalType > ( TNominalType value , bool isUpdateDocument )
109
109
{
110
110
return Create ( typeof ( TNominalType ) , value , isUpdateDocument ) ;
@@ -115,7 +115,7 @@ public static BsonDocumentWrapper Create<TNominalType>(TNominalType value, bool
115
115
/// </summary>
116
116
/// <param name="nominalType">The nominal type of the wrapped object.</param>
117
117
/// <param name="value">The wrapped object.</param>
118
- /// <returns>A BsonDocumentWrapper or null .</returns>
118
+ /// <returns>A BsonDocumentWrapper.</returns>
119
119
public static BsonDocumentWrapper Create ( Type nominalType , object value )
120
120
{
121
121
return Create ( nominalType , value , false ) ; // isUpdateDocument = false
@@ -127,61 +127,46 @@ public static BsonDocumentWrapper Create(Type nominalType, object value)
127
127
/// <param name="nominalType">The nominal type of the wrapped object.</param>
128
128
/// <param name="value">The wrapped object.</param>
129
129
/// <param name="isUpdateDocument">Whether the wrapped object is an update document.</param>
130
- /// <returns>A BsonDocumentWrapper or null .</returns>
130
+ /// <returns>A BsonDocumentWrapper.</returns>
131
131
public static BsonDocumentWrapper Create ( Type nominalType , object value , bool isUpdateDocument )
132
132
{
133
- if ( value != null )
134
- {
135
- return new BsonDocumentWrapper ( nominalType , value , isUpdateDocument ) ;
136
- }
137
- else
138
- {
139
- return null ;
140
- }
133
+ return new BsonDocumentWrapper ( nominalType , value , isUpdateDocument ) ;
141
134
}
142
135
143
136
/// <summary>
144
137
/// Creates a list of new instances of the BsonDocumentWrapper class.
145
138
/// </summary>
146
139
/// <typeparam name="TNominalType">The nominal type of the wrapped objects.</typeparam>
147
140
/// <param name="values">A list of wrapped objects.</param>
148
- /// <returns>A list of BsonDocumentWrappers or null .</returns>
141
+ /// <returns>A list of BsonDocumentWrappers.</returns>
149
142
public static IEnumerable < BsonDocumentWrapper > CreateMultiple < TNominalType > ( IEnumerable < TNominalType > values )
150
143
{
151
- if ( values != null )
152
- {
153
- return values . Where ( v => v != null ) . Select ( v => new BsonDocumentWrapper ( typeof ( TNominalType ) , v ) ) ;
154
- }
155
- else
144
+ if ( values == null )
156
145
{
157
- return null ;
146
+ throw new ArgumentNullException ( "values" ) ;
158
147
}
148
+
149
+ return values . Select ( v => new BsonDocumentWrapper ( typeof ( TNominalType ) , v ) ) ;
159
150
}
160
151
161
152
/// <summary>
162
153
/// Creates a list of new instances of the BsonDocumentWrapper class.
163
154
/// </summary>
164
155
/// <param name="nominalType">The nominal type of the wrapped object.</param>
165
156
/// <param name="values">A list of wrapped objects.</param>
166
- /// <returns>A list of BsonDocumentWrappers or null .</returns>
157
+ /// <returns>A list of BsonDocumentWrappers.</returns>
167
158
public static IEnumerable < BsonDocumentWrapper > CreateMultiple ( Type nominalType , IEnumerable values )
168
159
{
169
- if ( values ! = null )
160
+ if ( nominalType = = null )
170
161
{
171
- var wrappers = new List < BsonDocumentWrapper > ( ) ;
172
- foreach ( var value in values )
173
- {
174
- if ( value != null )
175
- {
176
- wrappers . Add ( new BsonDocumentWrapper ( nominalType , value ) ) ;
177
- }
178
- }
179
- return wrappers ;
162
+ throw new ArgumentNullException ( "nominalType" ) ;
180
163
}
181
- else
164
+ if ( values == null )
182
165
{
183
- return null ;
166
+ throw new ArgumentNullException ( "values" ) ;
184
167
}
168
+
169
+ return values . Cast < object > ( ) . Select ( v => new BsonDocumentWrapper ( nominalType , v ) ) ;
185
170
}
186
171
187
172
// public methods
0 commit comments