BsonDocumentBackedClass.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace MongoDB.Bson.Serialization
  18. {
  19. /// <summary>
  20. /// A class backed by a BsonDocument.
  21. /// </summary>
  22. public abstract class BsonDocumentBackedClass
  23. {
  24. // private fields
  25. private readonly BsonDocument _backingDocument;
  26. private readonly IBsonDocumentSerializer _serializer;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="BsonDocumentBackedClass"/> class.
  30. /// </summary>
  31. /// <param name="serializer">The serializer.</param>
  32. protected BsonDocumentBackedClass(IBsonDocumentSerializer serializer)
  33. : this(new BsonDocument(), serializer)
  34. { }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="BsonDocumentBackedClass"/> class.
  37. /// </summary>
  38. /// <param name="backingDocument">The backing document.</param>
  39. /// <param name="serializer">The serializer.</param>
  40. protected BsonDocumentBackedClass(BsonDocument backingDocument, IBsonDocumentSerializer serializer)
  41. {
  42. if (backingDocument == null)
  43. {
  44. throw new ArgumentNullException("backingDocument");
  45. }
  46. if (serializer == null)
  47. {
  48. throw new ArgumentNullException("serializer");
  49. }
  50. _backingDocument = backingDocument;
  51. _serializer = serializer;
  52. }
  53. // protected internal properties
  54. /// <summary>
  55. /// Gets the backing document.
  56. /// </summary>
  57. protected internal BsonDocument BackingDocument
  58. {
  59. get { return _backingDocument; }
  60. }
  61. // protected methods
  62. /// <summary>
  63. /// Gets the value from the backing document.
  64. /// </summary>
  65. /// <typeparam name="T">The type of the value.</typeparam>
  66. /// <param name="memberName">The member name.</param>
  67. /// <returns>The value.</returns>
  68. protected T GetValue<T>(string memberName)
  69. {
  70. BsonSerializationInfo info;
  71. if (!_serializer.TryGetMemberSerializationInfo(memberName, out info))
  72. {
  73. var message = string.Format("The member {0} does not exist.", memberName);
  74. throw new ArgumentException(message, "memberName");
  75. }
  76. BsonValue bsonValue;
  77. if (!_backingDocument.TryGetValue(info.ElementName, out bsonValue))
  78. {
  79. var message = string.Format("The backing document does not contain an element named '{0}'.", info.ElementName);
  80. throw new KeyNotFoundException(message);
  81. }
  82. return (T)info.DeserializeValue(bsonValue);
  83. }
  84. /// <summary>
  85. /// Gets the value from the backing document.
  86. /// </summary>
  87. /// <typeparam name="T">The type of the value.</typeparam>
  88. /// <param name="memberName">The member name.</param>
  89. /// <param name="defaultValue">The default value.</param>
  90. /// <returns>The value.</returns>
  91. protected T GetValue<T>(string memberName, T defaultValue)
  92. {
  93. BsonSerializationInfo info;
  94. if (!_serializer.TryGetMemberSerializationInfo(memberName, out info))
  95. {
  96. var message = string.Format("The member {0} does not exist.", memberName);
  97. throw new ArgumentException(message, "memberName");
  98. }
  99. BsonValue bsonValue;
  100. if (!_backingDocument.TryGetValue(info.ElementName, out bsonValue))
  101. {
  102. return defaultValue;
  103. }
  104. return (T)info.DeserializeValue(bsonValue);
  105. }
  106. /// <summary>
  107. /// Sets the value in the backing document.
  108. /// </summary>
  109. /// <param name="memberName">The member name.</param>
  110. /// <param name="value">The value.</param>
  111. protected void SetValue(string memberName, object value)
  112. {
  113. BsonSerializationInfo info;
  114. if (!_serializer.TryGetMemberSerializationInfo(memberName, out info))
  115. {
  116. var message = string.Format("The member {0} does not exist.", memberName);
  117. throw new ArgumentException(message, "memberName");
  118. }
  119. var bsonValue = info.SerializeValue(value);
  120. _backingDocument.Set(info.ElementName, bsonValue);
  121. }
  122. }
  123. }