BsonWriterSettings.cs 4.0 KB

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