ReadOnlyCollectionSerializer.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.Generic;
  16. using System.Collections.ObjectModel;
  17. namespace MongoDB.Bson.Serialization.Serializers
  18. {
  19. /// <summary>
  20. /// Represents a serializer for readonly collection.
  21. /// </summary>
  22. /// <typeparam name="TItem">The type of the item.</typeparam>
  23. public class ReadOnlyCollectionSerializer<TItem> :
  24. EnumerableInterfaceImplementerSerializerBase<ReadOnlyCollection<TItem>, TItem>
  25. {
  26. // constructors
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSerializer{TItem}"/> class.
  29. /// </summary>
  30. public ReadOnlyCollectionSerializer()
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSerializer{TItem}"/> class.
  35. /// </summary>
  36. /// <param name="itemSerializer">The item serializer.</param>
  37. public ReadOnlyCollectionSerializer(IBsonSerializer<TItem> itemSerializer)
  38. : base(itemSerializer)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSerializer{TItem}" /> class.
  43. /// </summary>
  44. /// <param name="serializerRegistry">The serializer registry.</param>
  45. public ReadOnlyCollectionSerializer(IBsonSerializerRegistry serializerRegistry)
  46. : base(serializerRegistry)
  47. {
  48. }
  49. // public methods
  50. /// <summary>
  51. /// Returns a serializer that has been reconfigured with the specified item serializer.
  52. /// </summary>
  53. /// <param name="itemSerializer">The item serializer.</param>
  54. /// <returns>The reconfigured serializer.</returns>
  55. public ReadOnlyCollectionSerializer<TItem> WithItemSerializer(IBsonSerializer<TItem> itemSerializer)
  56. {
  57. return new ReadOnlyCollectionSerializer<TItem>(itemSerializer);
  58. }
  59. // protected methods
  60. /// <summary>
  61. /// Creates the accumulator.
  62. /// </summary>
  63. /// <returns>The accumulator.</returns>
  64. protected override object CreateAccumulator()
  65. {
  66. return new List<TItem>();
  67. }
  68. /// <summary>
  69. /// Finalizes the result.
  70. /// </summary>
  71. /// <param name="accumulator">The accumulator.</param>
  72. /// <returns>The final result.</returns>
  73. protected override ReadOnlyCollection<TItem> FinalizeResult(object accumulator)
  74. {
  75. return ((List<TItem>)accumulator).AsReadOnly();
  76. }
  77. }
  78. }