BsonDocumentBackedClassSerializer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright 2010-2015 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 MongoDB.Bson.Serialization.Serializers;
  18. namespace MongoDB.Bson.Serialization
  19. {
  20. /// <summary>
  21. /// Represents a serializer for TClass (a subclass of BsonDocumentBackedClass).
  22. /// </summary>
  23. /// <typeparam name="TClass">The subclass of BsonDocumentBackedClass.</typeparam>
  24. public abstract class BsonDocumentBackedClassSerializer<TClass> : ClassSerializerBase<TClass>, IBsonDocumentSerializer
  25. where TClass : BsonDocumentBackedClass
  26. {
  27. // private fields
  28. private readonly Dictionary<string, BsonSerializationInfo> _memberSerializationInfo;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="BsonDocumentBackedClassSerializer{TClass}"/> class.
  32. /// </summary>
  33. protected BsonDocumentBackedClassSerializer()
  34. {
  35. _memberSerializationInfo = new Dictionary<string, BsonSerializationInfo>();
  36. }
  37. // public methods
  38. /// <summary>
  39. /// Deserializes a value.
  40. /// </summary>
  41. /// <param name="context">The deserialization context.</param>
  42. /// <param name="args">The deserialization args.</param>
  43. /// <returns>A deserialized value.</returns>
  44. public override TClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  45. {
  46. var backingDocument = BsonDocumentSerializer.Instance.Deserialize(context);
  47. return CreateInstance(backingDocument);
  48. }
  49. /// <summary>
  50. /// Tries to get the serialization info for a member.
  51. /// </summary>
  52. /// <param name="memberName">Name of the member.</param>
  53. /// <param name="serializationInfo">The serialization information.</param>
  54. /// <returns>
  55. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  56. /// </returns>
  57. public virtual bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  58. {
  59. return _memberSerializationInfo.TryGetValue(memberName, out serializationInfo);
  60. }
  61. /// <summary>
  62. /// Serializes a value.
  63. /// </summary>
  64. /// <param name="context">The serialization context.</param>
  65. /// <param name="args">The serialization args.</param>
  66. /// <param name="value">The object.</param>
  67. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
  68. {
  69. var backingDocument = ((BsonDocumentBackedClass)value).BackingDocument;
  70. BsonDocumentSerializer.Instance.Serialize(context, backingDocument);
  71. }
  72. // protected methods
  73. /// <summary>
  74. /// Registers a member.
  75. /// </summary>
  76. /// <param name="memberName">The member name.</param>
  77. /// <param name="elementName">The element name.</param>
  78. /// <param name="serializer">The serializer.</param>
  79. protected void RegisterMember(string memberName, string elementName, IBsonSerializer serializer)
  80. {
  81. if (memberName == null)
  82. {
  83. throw new ArgumentNullException("memberName");
  84. }
  85. if (elementName == null)
  86. {
  87. throw new ArgumentNullException("elementName");
  88. }
  89. if (serializer == null)
  90. {
  91. throw new ArgumentNullException("serializer");
  92. }
  93. var info = new BsonSerializationInfo(elementName, serializer, serializer.ValueType);
  94. _memberSerializationInfo.Add(memberName, info);
  95. }
  96. /// <summary>
  97. /// Creates the instance.
  98. /// </summary>
  99. /// <param name="backingDocument">The backing document.</param>
  100. /// <returns>An instance of TClass.</returns>
  101. protected abstract TClass CreateInstance(BsonDocument backingDocument);
  102. }
  103. }