BsonReaderSettings.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Represents settings for a BsonReader.
  20. /// </summary>
  21. [Serializable]
  22. public abstract class BsonReaderSettings
  23. {
  24. // private fields
  25. #pragma warning disable 618
  26. private GuidRepresentation _guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified;
  27. #pragma warning restore 618
  28. private bool _isFrozen;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonReaderSettings class.
  32. /// </summary>
  33. protected BsonReaderSettings()
  34. {
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the BsonReaderSettings class.
  38. /// </summary>
  39. /// <param name="guidRepresentation">The representation for Guids.</param>
  40. [Obsolete("Configure serializers instead.")]
  41. protected BsonReaderSettings(GuidRepresentation guidRepresentation)
  42. {
  43. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  44. {
  45. throw new InvalidOperationException("BsonReaderSettings constructor with GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  46. }
  47. _guidRepresentation = guidRepresentation;
  48. }
  49. // public properties
  50. /// <summary>
  51. /// Gets or sets the representation for Guids.
  52. /// </summary>
  53. [Obsolete("Configure serializers instead.")]
  54. public GuidRepresentation GuidRepresentation
  55. {
  56. get
  57. {
  58. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  59. {
  60. throw new InvalidOperationException("BsonReaderSettings.GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  61. }
  62. return _guidRepresentation;
  63. }
  64. set
  65. {
  66. if (_isFrozen) { ThrowFrozenException(); }
  67. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  68. {
  69. throw new InvalidOperationException("BsonReaderSettings.GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  70. }
  71. _guidRepresentation = value;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets whether the settings are frozen.
  76. /// </summary>
  77. public bool IsFrozen
  78. {
  79. get { return _isFrozen; }
  80. }
  81. // public methods
  82. /// <summary>
  83. /// Creates a clone of the settings.
  84. /// </summary>
  85. /// <returns>A clone of the settings.</returns>
  86. public BsonReaderSettings Clone()
  87. {
  88. return CloneImplementation();
  89. }
  90. /// <summary>
  91. /// Freezes the settings.
  92. /// </summary>
  93. /// <returns>The frozen settings.</returns>
  94. public BsonReaderSettings Freeze()
  95. {
  96. _isFrozen = true;
  97. return this;
  98. }
  99. /// <summary>
  100. /// Returns a frozen copy of the settings.
  101. /// </summary>
  102. /// <returns>A frozen copy of the settings.</returns>
  103. public BsonReaderSettings FrozenCopy()
  104. {
  105. if (_isFrozen)
  106. {
  107. return this;
  108. }
  109. else
  110. {
  111. return Clone().Freeze();
  112. }
  113. }
  114. // protected methods
  115. /// <summary>
  116. /// Creates a clone of the settings.
  117. /// </summary>
  118. /// <returns>A clone of the settings.</returns>
  119. protected abstract BsonReaderSettings CloneImplementation();
  120. /// <summary>
  121. /// Throws an InvalidOperationException when an attempt is made to change a setting after the settings are frozen.
  122. /// </summary>
  123. protected void ThrowFrozenException()
  124. {
  125. var message = string.Format("{0} is frozen.", this.GetType().Name);
  126. throw new InvalidOperationException(message);
  127. }
  128. }
  129. }