ElementAppendingSerializer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright 2017-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.Generic;
  17. using System.Linq;
  18. using MongoDB.Bson.IO;
  19. namespace MongoDB.Bson.Serialization.Serializers
  20. {
  21. /// <summary>
  22. /// A serializer that serializes a document and appends elements to the end of it.
  23. /// </summary>
  24. /// <typeparam name="TDocument">The type of the document.</typeparam>
  25. /// <seealso cref="MongoDB.Bson.Serialization.IBsonSerializer{TDocument}" />
  26. public class ElementAppendingSerializer<TDocument> : IBsonSerializer<TDocument>
  27. {
  28. // private fields
  29. private readonly IBsonSerializer<TDocument> _documentSerializer;
  30. private readonly List<BsonElement> _elements;
  31. private readonly Action<BsonWriterSettings> _writerSettingsConfigurator;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="ElementAppendingSerializer{TDocument}" /> class.
  35. /// </summary>
  36. /// <param name="documentSerializer">The document serializer.</param>
  37. /// <param name="elements">The elements to append.</param>
  38. /// <param name="writerSettingsConfigurator">The writer settings configurator.</param>
  39. public ElementAppendingSerializer(
  40. IBsonSerializer<TDocument> documentSerializer,
  41. IEnumerable<BsonElement> elements,
  42. Action<BsonWriterSettings> writerSettingsConfigurator = null)
  43. {
  44. if (documentSerializer == null) { throw new ArgumentNullException(nameof(documentSerializer)); }
  45. if (elements == null) { throw new ArgumentNullException(nameof(elements)); }
  46. _documentSerializer = documentSerializer;
  47. _elements = elements.ToList();
  48. _writerSettingsConfigurator = writerSettingsConfigurator; // can be null
  49. }
  50. // public properties
  51. /// <inheritdoc />
  52. public Type ValueType => typeof(TDocument);
  53. // public methods
  54. /// <inheritdoc />
  55. public TDocument Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  56. {
  57. throw new NotSupportedException();
  58. }
  59. object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  60. {
  61. throw new NotSupportedException();
  62. }
  63. /// <inheritdoc />
  64. public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDocument value)
  65. {
  66. var writer = context.Writer;
  67. var elementAppendingWriter = new ElementAppendingBsonWriter(writer, _elements, _writerSettingsConfigurator);
  68. var elementAppendingContext = BsonSerializationContext.CreateRoot(elementAppendingWriter, builder => ConfigureElementAppendingContext(builder, context));
  69. _documentSerializer.Serialize(elementAppendingContext, args, value);
  70. }
  71. void IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
  72. {
  73. Serialize(context, args, (TDocument)value);
  74. }
  75. // private methods
  76. private void ConfigureElementAppendingContext(BsonSerializationContext.Builder builder, BsonSerializationContext originalContext)
  77. {
  78. builder.IsDynamicType = originalContext.IsDynamicType;
  79. }
  80. }
  81. }