BsonWriterSettings.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 BsonWriter.
  20. /// </summary>
  21. [Serializable]
  22. public abstract class BsonWriterSettings
  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. private int _maxSerializationDepth = BsonDefaults.MaxSerializationDepth;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the BsonWriterSettings class.
  33. /// </summary>
  34. protected BsonWriterSettings()
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the BsonWriterSettings class.
  39. /// </summary>
  40. /// <param name="guidRepresentation">The representation for Guids.</param>
  41. [Obsolete("Configure serializers instead.")]
  42. protected BsonWriterSettings(GuidRepresentation guidRepresentation)
  43. {
  44. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  45. {
  46. throw new InvalidOperationException("BsonWriterSettings constructor with GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  47. }
  48. _guidRepresentation = guidRepresentation;
  49. }
  50. // public properties
  51. /// <summary>
  52. /// Gets or sets the representation for Guids.
  53. /// </summary>
  54. [Obsolete("Configure serializers instead.")]
  55. public GuidRepresentation GuidRepresentation
  56. {
  57. get
  58. {
  59. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  60. {
  61. throw new InvalidOperationException("BsonWriterSettings.GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  62. }
  63. return _guidRepresentation;
  64. }
  65. set
  66. {
  67. if (_isFrozen) { ThrowFrozenException(); }
  68. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  69. {
  70. throw new InvalidOperationException("BsonWriterSettings.GuidRepresentation can only be used when GuidRepresentationMode is V2.");
  71. }
  72. _guidRepresentation = value;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets whether the settings are frozen.
  77. /// </summary>
  78. public bool IsFrozen
  79. {
  80. get { return _isFrozen; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the max serialization depth allowed (used to detect circular references).
  84. /// </summary>
  85. public int MaxSerializationDepth
  86. {
  87. get { return _maxSerializationDepth; }
  88. set
  89. {
  90. if (_isFrozen) { ThrowFrozenException(); }
  91. _maxSerializationDepth = value;
  92. }
  93. }
  94. // public methods
  95. /// <summary>
  96. /// Creates a clone of the settings.
  97. /// </summary>
  98. /// <returns>A clone of the settings.</returns>
  99. public BsonWriterSettings Clone()
  100. {
  101. return CloneImplementation();
  102. }
  103. /// <summary>
  104. /// Freezes the settings.
  105. /// </summary>
  106. /// <returns>The frozen settings.</returns>
  107. public BsonWriterSettings Freeze()
  108. {
  109. _isFrozen = true;
  110. return this;
  111. }
  112. /// <summary>
  113. /// Returns a frozen copy of the settings.
  114. /// </summary>
  115. /// <returns>A frozen copy of the settings.</returns>
  116. public BsonWriterSettings FrozenCopy()
  117. {
  118. if (_isFrozen)
  119. {
  120. return this;
  121. }
  122. else
  123. {
  124. return Clone().Freeze();
  125. }
  126. }
  127. // protected methods
  128. /// <summary>
  129. /// Creates a clone of the settings.
  130. /// </summary>
  131. /// <returns>A clone of the settings.</returns>
  132. protected abstract BsonWriterSettings CloneImplementation();
  133. /// <summary>
  134. /// Throws an InvalidOperationException when an attempt is made to change a setting after the settings are frozen.
  135. /// </summary>
  136. protected void ThrowFrozenException()
  137. {
  138. var message = string.Format("{0} is frozen.", this.GetType().Name);
  139. throw new InvalidOperationException(message);
  140. }
  141. }
  142. }