@@ -37,14 +37,15 @@ public class BsonDocumentWrapper : MaterializedOnDemandBsonDocument
37
37
// private fields
38
38
private readonly object _wrapped ;
39
39
private readonly IBsonSerializer _serializer ;
40
+ private readonly IBsonSerializationDomain _serializationDomain ;
40
41
41
42
// constructors
42
43
/// <summary>
43
44
/// Initializes a new instance of the <see cref="BsonDocumentWrapper"/> class.
44
45
/// </summary>
45
46
/// <param name="value">The value.</param>
46
47
public BsonDocumentWrapper ( object value )
47
- : this ( value , UndiscriminatedActualTypeSerializer < object > . Instance )
48
+ : this ( value , UndiscriminatedActualTypeSerializer < object > . Instance , BsonSerializer . DefaultSerializationDomain )
48
49
{
49
50
}
50
51
@@ -54,14 +55,15 @@ public BsonDocumentWrapper(object value)
54
55
/// <param name="value">The value.</param>
55
56
/// <param name="serializer">The serializer.</param>
56
57
public BsonDocumentWrapper ( object value , IBsonSerializer serializer )
58
+ : this ( value , serializer , BsonSerializer . DefaultSerializationDomain )
57
59
{
58
- if ( serializer == null )
59
- {
60
- throw new ArgumentNullException ( "serializer" ) ;
61
- }
60
+ }
62
61
62
+ internal BsonDocumentWrapper ( object value , IBsonSerializer serializer , IBsonSerializationDomain serializationDomain )
63
+ {
64
+ _serializer = serializer ?? throw new ArgumentNullException ( nameof ( serializer ) ) ;
65
+ _serializationDomain = serializationDomain ;
63
66
_wrapped = value ;
64
- _serializer = serializer ;
65
67
}
66
68
67
69
// public properties
@@ -84,6 +86,7 @@ public object Wrapped
84
86
get { return _wrapped ; }
85
87
}
86
88
89
+ // DOMAIN-API All the various Create methods are used only in testing, the version without the domain should be removed.
87
90
// public static methods
88
91
/// <summary>
89
92
/// Creates a new instance of the BsonDocumentWrapper class.
@@ -111,7 +114,7 @@ public static BsonDocumentWrapper Create(Type nominalType, object value) =>
111
114
internal static BsonDocumentWrapper Create ( Type nominalType , object value , IBsonSerializationDomain domain )
112
115
{
113
116
var serializer = domain . LookupSerializer ( nominalType ) ;
114
- return new BsonDocumentWrapper ( value , serializer ) ;
117
+ return new BsonDocumentWrapper ( value , serializer , domain ) ;
115
118
}
116
119
117
120
/// <summary>
@@ -139,7 +142,7 @@ internal static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IE
139
142
}
140
143
141
144
var serializer = domain . LookupSerializer ( typeof ( TNominalType ) ) ;
142
- return values . Select ( v => new BsonDocumentWrapper ( v , serializer ) ) ;
145
+ return values . Select ( v => new BsonDocumentWrapper ( v , serializer , domain ) ) ;
143
146
}
144
147
145
148
/// <summary>
@@ -171,7 +174,7 @@ internal static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType
171
174
}
172
175
173
176
var serializer = domain . LookupSerializer ( nominalType ) ;
174
- return values . Cast < object > ( ) . Select ( v => new BsonDocumentWrapper ( v , serializer ) ) ;
177
+ return values . Cast < object > ( ) . Select ( v => new BsonDocumentWrapper ( v , serializer , domain ) ) ;
175
178
}
176
179
177
180
// public methods
@@ -191,7 +194,8 @@ public override BsonValue Clone()
191
194
{
192
195
return new BsonDocumentWrapper (
193
196
_wrapped ,
194
- _serializer ) ;
197
+ _serializer ,
198
+ _serializationDomain ) ;
195
199
}
196
200
}
197
201
@@ -206,8 +210,7 @@ protected override IEnumerable<BsonElement> Materialize()
206
210
var writerSettings = BsonDocumentWriterSettings . Defaults ;
207
211
using ( var bsonWriter = new BsonDocumentWriter ( bsonDocument , writerSettings ) )
208
212
{
209
- //QUESTION Is it correct we only need a default domain here?
210
- var context = BsonSerializationContext . CreateRoot ( bsonWriter , BsonSerializer . DefaultSerializationDomain ) ;
213
+ var context = BsonSerializationContext . CreateRoot ( bsonWriter , _serializationDomain ) ;
211
214
_serializer . Serialize ( context , _wrapped ) ;
212
215
}
213
216
0 commit comments