@@ -37,14 +37,15 @@ public class BsonDocumentWrapper : MaterializedOnDemandBsonDocument
3737 // private fields
3838 private readonly object _wrapped ;
3939 private readonly IBsonSerializer _serializer ;
40+ private readonly IBsonSerializationDomain _serializationDomain ;
4041
4142 // constructors
4243 /// <summary>
4344 /// Initializes a new instance of the <see cref="BsonDocumentWrapper"/> class.
4445 /// </summary>
4546 /// <param name="value">The value.</param>
4647 public BsonDocumentWrapper ( object value )
47- : this ( value , UndiscriminatedActualTypeSerializer < object > . Instance )
48+ : this ( value , UndiscriminatedActualTypeSerializer < object > . Instance , BsonSerializer . DefaultSerializationDomain )
4849 {
4950 }
5051
@@ -54,14 +55,15 @@ public BsonDocumentWrapper(object value)
5455 /// <param name="value">The value.</param>
5556 /// <param name="serializer">The serializer.</param>
5657 public BsonDocumentWrapper ( object value , IBsonSerializer serializer )
58+ : this ( value , serializer , BsonSerializer . DefaultSerializationDomain )
5759 {
58- if ( serializer == null )
59- {
60- throw new ArgumentNullException ( "serializer" ) ;
61- }
60+ }
6261
62+ internal BsonDocumentWrapper ( object value , IBsonSerializer serializer , IBsonSerializationDomain serializationDomain )
63+ {
64+ _serializer = serializer ?? throw new ArgumentNullException ( nameof ( serializer ) ) ;
65+ _serializationDomain = serializationDomain ;
6366 _wrapped = value ;
64- _serializer = serializer ;
6567 }
6668
6769 // public properties
@@ -84,6 +86,7 @@ public object Wrapped
8486 get { return _wrapped ; }
8587 }
8688
89+ // DOMAIN-API All the various Create methods are used only in testing, the version without the domain should be removed.
8790 // public static methods
8891 /// <summary>
8992 /// Creates a new instance of the BsonDocumentWrapper class.
@@ -111,7 +114,7 @@ public static BsonDocumentWrapper Create(Type nominalType, object value) =>
111114 internal static BsonDocumentWrapper Create ( Type nominalType , object value , IBsonSerializationDomain domain )
112115 {
113116 var serializer = domain . LookupSerializer ( nominalType ) ;
114- return new BsonDocumentWrapper ( value , serializer ) ;
117+ return new BsonDocumentWrapper ( value , serializer , domain ) ;
115118 }
116119
117120 /// <summary>
@@ -139,7 +142,7 @@ internal static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IE
139142 }
140143
141144 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 ) ) ;
143146 }
144147
145148 /// <summary>
@@ -171,7 +174,7 @@ internal static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType
171174 }
172175
173176 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 ) ) ;
175178 }
176179
177180 // public methods
@@ -191,7 +194,8 @@ public override BsonValue Clone()
191194 {
192195 return new BsonDocumentWrapper (
193196 _wrapped ,
194- _serializer ) ;
197+ _serializer ,
198+ _serializationDomain ) ;
195199 }
196200 }
197201
@@ -206,8 +210,7 @@ protected override IEnumerable<BsonElement> Materialize()
206210 var writerSettings = BsonDocumentWriterSettings . Defaults ;
207211 using ( var bsonWriter = new BsonDocumentWriter ( bsonDocument , writerSettings ) )
208212 {
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 ) ;
211214 _serializer . Serialize ( context , _wrapped ) ;
212215 }
213216
0 commit comments