EnumerableInterfaceImplementerSerializerBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Collections;
  16. using System.Collections.Generic;
  17. namespace MongoDB.Bson.Serialization.Serializers
  18. {
  19. /// <summary>
  20. /// Represents a serializer for enumerable values.
  21. /// </summary>
  22. /// <typeparam name="TValue">The type of the value.</typeparam>
  23. public abstract class EnumerableInterfaceImplementerSerializerBase<TValue> : EnumerableSerializerBase<TValue>, IBsonArraySerializer where TValue : class, IEnumerable
  24. {
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue}"/> class.
  28. /// </summary>
  29. protected EnumerableInterfaceImplementerSerializerBase()
  30. {
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue}"/> class.
  34. /// </summary>
  35. /// <param name="itemSerializer">The item serializer.</param>
  36. protected EnumerableInterfaceImplementerSerializerBase(IBsonSerializer itemSerializer)
  37. : base(itemSerializer)
  38. {
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue}" /> class.
  42. /// </summary>
  43. /// <param name="serializerRegistry">The serializer registry.</param>
  44. protected EnumerableInterfaceImplementerSerializerBase(IBsonSerializerRegistry serializerRegistry)
  45. : base(serializerRegistry)
  46. {
  47. }
  48. // protected methods
  49. /// <summary>
  50. /// Adds the item.
  51. /// </summary>
  52. /// <param name="accumulator">The accumulator.</param>
  53. /// <param name="item">The item.</param>
  54. protected override void AddItem(object accumulator, object item)
  55. {
  56. ((IList)accumulator).Add(item);
  57. }
  58. /// <summary>
  59. /// Enumerates the items in serialization order.
  60. /// </summary>
  61. /// <param name="value">The value.</param>
  62. /// <returns>The items.</returns>
  63. protected override IEnumerable EnumerateItemsInSerializationOrder(TValue value)
  64. {
  65. return value;
  66. }
  67. /// <summary>
  68. /// Finalizes the result.
  69. /// </summary>
  70. /// <param name="accumulator">The accumulator.</param>
  71. /// <returns>The result.</returns>
  72. protected override TValue FinalizeResult(object accumulator)
  73. {
  74. return (TValue)accumulator;
  75. }
  76. }
  77. /// <summary>
  78. /// Represents a serializer for enumerable values.
  79. /// </summary>
  80. /// <typeparam name="TValue">The type of the value.</typeparam>
  81. /// <typeparam name="TItem">The type of the items.</typeparam>
  82. public abstract class EnumerableInterfaceImplementerSerializerBase<TValue, TItem> : EnumerableSerializerBase<TValue, TItem>, IBsonArraySerializer where TValue : class, IEnumerable<TItem>
  83. {
  84. // constructors
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue, TItem}"/> class.
  87. /// </summary>
  88. public EnumerableInterfaceImplementerSerializerBase()
  89. {
  90. }
  91. /// <summary>
  92. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue, TItem}"/> class.
  93. /// </summary>
  94. /// <param name="itemSerializer">The item serializer.</param>
  95. public EnumerableInterfaceImplementerSerializerBase(IBsonSerializer<TItem> itemSerializer)
  96. : base(itemSerializer)
  97. {
  98. }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="EnumerableInterfaceImplementerSerializerBase{TValue, TItem}" /> class.
  101. /// </summary>
  102. /// <param name="serializerRegistry">The serializer registry.</param>
  103. public EnumerableInterfaceImplementerSerializerBase(IBsonSerializerRegistry serializerRegistry)
  104. : base(serializerRegistry)
  105. {
  106. }
  107. // protected methods
  108. /// <summary>
  109. /// Adds the item.
  110. /// </summary>
  111. /// <param name="accumulator">The accumulator.</param>
  112. /// <param name="item">The item.</param>
  113. protected override void AddItem(object accumulator, TItem item)
  114. {
  115. ((ICollection<TItem>)accumulator).Add(item);
  116. }
  117. /// <summary>
  118. /// Enumerates the items in serialization order.
  119. /// </summary>
  120. /// <param name="value">The value.</param>
  121. /// <returns>The items.</returns>
  122. protected override IEnumerable<TItem> EnumerateItemsInSerializationOrder(TValue value)
  123. {
  124. return value;
  125. }
  126. /// <summary>
  127. /// Finalizes the result.
  128. /// </summary>
  129. /// <param name="accumulator">The accumulator.</param>
  130. /// <returns>The result.</returns>
  131. protected override TValue FinalizeResult(object accumulator)
  132. {
  133. return (TValue)accumulator;
  134. }
  135. }
  136. }