BsonDocumentBackedClassSerializer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.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. /// Tries to get the serialization info for a member.
  40. /// </summary>
  41. /// <param name="memberName">Name of the member.</param>
  42. /// <param name="serializationInfo">The serialization information.</param>
  43. /// <returns>
  44. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  45. /// </returns>
  46. public virtual bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  47. {
  48. return _memberSerializationInfo.TryGetValue(memberName, out serializationInfo);
  49. }
  50. // protected methods
  51. /// <inheritdoc />
  52. protected override TClass DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  53. {
  54. var backingDocument = BsonDocumentSerializer.Instance.Deserialize(context);
  55. return CreateInstance(backingDocument);
  56. }
  57. /// <summary>
  58. /// Registers a member.
  59. /// </summary>
  60. /// <param name="memberName">The member name.</param>
  61. /// <param name="elementName">The element name.</param>
  62. /// <param name="serializer">The serializer.</param>
  63. protected void RegisterMember(string memberName, string elementName, IBsonSerializer serializer)
  64. {
  65. if (memberName == null)
  66. {
  67. throw new ArgumentNullException("memberName");
  68. }
  69. if (elementName == null)
  70. {
  71. throw new ArgumentNullException("elementName");
  72. }
  73. if (serializer == null)
  74. {
  75. throw new ArgumentNullException("serializer");
  76. }
  77. var info = new BsonSerializationInfo(elementName, serializer, serializer.ValueType);
  78. _memberSerializationInfo.Add(memberName, info);
  79. }
  80. /// <inheritdoc />
  81. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
  82. {
  83. var backingDocument = ((BsonDocumentBackedClass)value).BackingDocument;
  84. BsonDocumentSerializer.Instance.Serialize(context, backingDocument);
  85. }
  86. /// <summary>
  87. /// Creates the instance.
  88. /// </summary>
  89. /// <param name="backingDocument">The backing document.</param>
  90. /// <returns>An instance of TClass.</returns>
  91. protected abstract TClass CreateInstance(BsonDocument backingDocument);
  92. }
  93. }