BsonDocumentBackedClass.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright 2010-2014 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. namespace MongoDB.Bson.Serialization
  17. {
  18. /// <summary>
  19. /// A class backed by a BsonDocument.
  20. /// </summary>
  21. public abstract class BsonDocumentBackedClass
  22. {
  23. // private fields
  24. private readonly BsonDocument _backingDocument;
  25. private readonly IBsonDocumentSerializer _serializer;
  26. // constructors
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="BsonDocumentBackedClass"/> class.
  29. /// </summary>
  30. /// <param name="serializer">The serializer.</param>
  31. protected BsonDocumentBackedClass(IBsonDocumentSerializer serializer)
  32. : this(new BsonDocument(), serializer)
  33. { }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="BsonDocumentBackedClass"/> class.
  36. /// </summary>
  37. /// <param name="backingDocument">The backing document.</param>
  38. /// <param name="serializer">The serializer.</param>
  39. protected BsonDocumentBackedClass(BsonDocument backingDocument, IBsonDocumentSerializer serializer)
  40. {
  41. if (backingDocument == null)
  42. {
  43. throw new ArgumentNullException("backingDocument");
  44. }
  45. if (serializer == null)
  46. {
  47. throw new ArgumentNullException("serializer");
  48. }
  49. _backingDocument = backingDocument;
  50. _serializer = serializer;
  51. }
  52. // protected internal properties
  53. /// <summary>
  54. /// Gets the backing document.
  55. /// </summary>
  56. protected internal BsonDocument BackingDocument
  57. {
  58. get { return _backingDocument; }
  59. }
  60. // protected methods
  61. /// <summary>
  62. /// Gets the value from the backing document.
  63. /// </summary>
  64. /// <typeparam name="T">The type of the value.</typeparam>
  65. /// <param name="memberName">The member name.</param>
  66. /// <param name="defaultValue">The default value.</param>
  67. /// <returns>The value.</returns>
  68. protected T GetValue<T>(string memberName, T defaultValue)
  69. {
  70. var info = _serializer.GetMemberSerializationInfo(memberName);
  71. BsonValue bsonValue;
  72. if (!_backingDocument.TryGetValue(info.ElementName, out bsonValue))
  73. {
  74. return defaultValue;
  75. }
  76. return (T)info.DeserializeValue(bsonValue);
  77. }
  78. /// <summary>
  79. /// Sets the value in the backing document.
  80. /// </summary>
  81. /// <param name="memberName">The member name.</param>
  82. /// <param name="value">The value.</param>
  83. protected void SetValue(string memberName, object value)
  84. {
  85. var info = _serializer.GetMemberSerializationInfo(memberName);
  86. var bsonValue = info.SerializeValue(value);
  87. _backingDocument.Set(info.ElementName, bsonValue);
  88. }
  89. }
  90. }