BsonWriterSettings.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright 2010-2016 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. #if NET45
  22. [Serializable]
  23. #endif
  24. public abstract class BsonWriterSettings
  25. {
  26. // private fields
  27. private GuidRepresentation _guidRepresentation = BsonDefaults.GuidRepresentation;
  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. protected BsonWriterSettings(GuidRepresentation guidRepresentation)
  42. {
  43. _guidRepresentation = guidRepresentation;
  44. }
  45. // public properties
  46. /// <summary>
  47. /// Gets or sets the representation for Guids.
  48. /// </summary>
  49. public GuidRepresentation GuidRepresentation
  50. {
  51. get { return _guidRepresentation; }
  52. set
  53. {
  54. if (_isFrozen) { ThrowFrozenException(); }
  55. _guidRepresentation = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets whether the settings are frozen.
  60. /// </summary>
  61. public bool IsFrozen
  62. {
  63. get { return _isFrozen; }
  64. }
  65. /// <summary>
  66. /// Gets or sets the max serialization depth allowed (used to detect circular references).
  67. /// </summary>
  68. public int MaxSerializationDepth
  69. {
  70. get { return _maxSerializationDepth; }
  71. set
  72. {
  73. if (_isFrozen) { ThrowFrozenException(); }
  74. _maxSerializationDepth = value;
  75. }
  76. }
  77. // public methods
  78. /// <summary>
  79. /// Creates a clone of the settings.
  80. /// </summary>
  81. /// <returns>A clone of the settings.</returns>
  82. public BsonWriterSettings Clone()
  83. {
  84. return CloneImplementation();
  85. }
  86. /// <summary>
  87. /// Freezes the settings.
  88. /// </summary>
  89. /// <returns>The frozen settings.</returns>
  90. public BsonWriterSettings Freeze()
  91. {
  92. _isFrozen = true;
  93. return this;
  94. }
  95. /// <summary>
  96. /// Returns a frozen copy of the settings.
  97. /// </summary>
  98. /// <returns>A frozen copy of the settings.</returns>
  99. public BsonWriterSettings FrozenCopy()
  100. {
  101. if (_isFrozen)
  102. {
  103. return this;
  104. }
  105. else
  106. {
  107. return Clone().Freeze();
  108. }
  109. }
  110. // protected methods
  111. /// <summary>
  112. /// Creates a clone of the settings.
  113. /// </summary>
  114. /// <returns>A clone of the settings.</returns>
  115. protected abstract BsonWriterSettings CloneImplementation();
  116. /// <summary>
  117. /// Throws an InvalidOperationException when an attempt is made to change a setting after the settings are frozen.
  118. /// </summary>
  119. protected void ThrowFrozenException()
  120. {
  121. var message = string.Format("{0} is frozen.", this.GetType().Name);
  122. throw new InvalidOperationException(message);
  123. }
  124. }
  125. }