BsonDocumentWrapper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Bson.Serialization.Serializers;
  22. namespace MongoDB.Bson
  23. {
  24. // this class is a wrapper for an object that we intend to serialize as a BsonDocument
  25. // it is a subclass of BsonDocument so that it may be used where a BsonDocument is expected
  26. // this class is mostly used by MongoCollection and MongoCursor when supporting generic query objects
  27. // if all that ever happens with this wrapped object is that it gets serialized then the BsonDocument is never materialized
  28. /// <summary>
  29. /// Represents a BsonDocument wrapper.
  30. /// </summary>
  31. public class BsonDocumentWrapper : MaterializedOnDemandBsonDocument
  32. {
  33. // private fields
  34. private readonly object _wrapped;
  35. private readonly IBsonSerializer _serializer;
  36. // constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="BsonDocumentWrapper"/> class.
  39. /// </summary>
  40. /// <param name="value">The value.</param>
  41. public BsonDocumentWrapper(object value)
  42. : this(value, UndiscriminatedActualTypeSerializer<object>.Instance)
  43. {
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="BsonDocumentWrapper"/> class.
  47. /// </summary>
  48. /// <param name="value">The value.</param>
  49. /// <param name="serializer">The serializer.</param>
  50. public BsonDocumentWrapper(object value, IBsonSerializer serializer)
  51. {
  52. if (serializer == null)
  53. {
  54. throw new ArgumentNullException("serializer");
  55. }
  56. _wrapped = value;
  57. _serializer = serializer;
  58. }
  59. // public properties
  60. /// <summary>
  61. /// Gets the serializer.
  62. /// </summary>
  63. /// <value>
  64. /// The serializer.
  65. /// </value>
  66. public IBsonSerializer Serializer
  67. {
  68. get { return _serializer; }
  69. }
  70. /// <summary>
  71. /// Gets the wrapped value.
  72. /// </summary>
  73. public object Wrapped
  74. {
  75. get { return _wrapped; }
  76. }
  77. // public static methods
  78. /// <summary>
  79. /// Creates a new instance of the BsonDocumentWrapper class.
  80. /// </summary>
  81. /// <typeparam name="TNominalType">The nominal type of the wrapped object.</typeparam>
  82. /// <param name="value">The wrapped object.</param>
  83. /// <returns>A BsonDocumentWrapper.</returns>
  84. public static BsonDocumentWrapper Create<TNominalType>(TNominalType value)
  85. {
  86. return Create(typeof(TNominalType), value);
  87. }
  88. /// <summary>
  89. /// Creates a new instance of the BsonDocumentWrapper class.
  90. /// </summary>
  91. /// <param name="nominalType">The nominal type of the wrapped object.</param>
  92. /// <param name="value">The wrapped object.</param>
  93. /// <returns>A BsonDocumentWrapper.</returns>
  94. public static BsonDocumentWrapper Create(Type nominalType, object value)
  95. {
  96. var serializer = BsonSerializer.LookupSerializer(nominalType);
  97. return new BsonDocumentWrapper(value, serializer);
  98. }
  99. /// <summary>
  100. /// Creates a list of new instances of the BsonDocumentWrapper class.
  101. /// </summary>
  102. /// <typeparam name="TNominalType">The nominal type of the wrapped objects.</typeparam>
  103. /// <param name="values">A list of wrapped objects.</param>
  104. /// <returns>A list of BsonDocumentWrappers.</returns>
  105. public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnumerable<TNominalType> values)
  106. {
  107. if (values == null)
  108. {
  109. throw new ArgumentNullException("values");
  110. }
  111. var serializer = BsonSerializer.LookupSerializer(typeof(TNominalType));
  112. return values.Select(v => new BsonDocumentWrapper(v, serializer));
  113. }
  114. /// <summary>
  115. /// Creates a list of new instances of the BsonDocumentWrapper class.
  116. /// </summary>
  117. /// <param name="nominalType">The nominal type of the wrapped object.</param>
  118. /// <param name="values">A list of wrapped objects.</param>
  119. /// <returns>A list of BsonDocumentWrappers.</returns>
  120. public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType, IEnumerable values)
  121. {
  122. if (nominalType == null)
  123. {
  124. throw new ArgumentNullException("nominalType");
  125. }
  126. if (values == null)
  127. {
  128. throw new ArgumentNullException("values");
  129. }
  130. var serializer = BsonSerializer.LookupSerializer(nominalType);
  131. return values.Cast<object>().Select(v => new BsonDocumentWrapper(v, serializer));
  132. }
  133. // public methods
  134. /// <summary>
  135. /// Creates a shallow clone of the document (see also DeepClone).
  136. /// </summary>
  137. /// <returns>
  138. /// A shallow clone of the document.
  139. /// </returns>
  140. public override BsonValue Clone()
  141. {
  142. if (IsMaterialized)
  143. {
  144. return base.Clone();
  145. }
  146. else
  147. {
  148. return new BsonDocumentWrapper(
  149. _wrapped,
  150. _serializer);
  151. }
  152. }
  153. // protected methods
  154. /// <summary>
  155. /// Materializes the BsonDocument.
  156. /// </summary>
  157. /// <returns>The materialized elements.</returns>
  158. protected override IEnumerable<BsonElement> Materialize()
  159. {
  160. var bsonDocument = new BsonDocument();
  161. var writerSettings = BsonDocumentWriterSettings.Defaults;
  162. using (var bsonWriter = new BsonDocumentWriter(bsonDocument, writerSettings))
  163. {
  164. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  165. _serializer.Serialize(context, _wrapped);
  166. }
  167. return bsonDocument.Elements;
  168. }
  169. /// <summary>
  170. /// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
  171. /// </summary>
  172. protected override void MaterializeCompleted()
  173. {
  174. }
  175. }
  176. }