BsonSerializationContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. using MongoDB.Bson.IO;
  17. namespace MongoDB.Bson.Serialization
  18. {
  19. /// <summary>
  20. /// Represents all the contextual information needed by a serializer to serialize a value.
  21. /// </summary>
  22. public class BsonSerializationContext
  23. {
  24. // private fields
  25. private readonly Func<Type, bool> _isDynamicType;
  26. private readonly IBsonWriter _writer;
  27. // constructors
  28. private BsonSerializationContext(
  29. IBsonWriter writer,
  30. Func<Type, bool> isDynamicType)
  31. {
  32. _writer = writer;
  33. _isDynamicType = isDynamicType;
  34. }
  35. // public properties
  36. /// <summary>
  37. /// Gets a function that, when executed, will indicate whether the type
  38. /// is a dynamic type.
  39. /// </summary>
  40. public Func<Type, bool> IsDynamicType
  41. {
  42. get { return _isDynamicType; }
  43. }
  44. /// <summary>
  45. /// Gets the writer.
  46. /// </summary>
  47. /// <value>
  48. /// The writer.
  49. /// </value>
  50. public IBsonWriter Writer
  51. {
  52. get { return _writer; }
  53. }
  54. // public static methods
  55. /// <summary>
  56. /// Creates a root context.
  57. /// </summary>
  58. /// <param name="writer">The writer.</param>
  59. /// <param name="configurator">The serialization context configurator.</param>
  60. /// <returns>
  61. /// A root context.
  62. /// </returns>
  63. public static BsonSerializationContext CreateRoot(
  64. IBsonWriter writer,
  65. Action<Builder> configurator = null)
  66. {
  67. var builder = new Builder(null, writer);
  68. if (configurator != null)
  69. {
  70. configurator(builder);
  71. }
  72. return builder.Build();
  73. }
  74. /// <summary>
  75. /// Creates a new context with some values changed.
  76. /// </summary>
  77. /// <param name="configurator">The serialization context configurator.</param>
  78. /// <returns>
  79. /// A new context.
  80. /// </returns>
  81. public BsonSerializationContext With(
  82. Action<Builder> configurator = null)
  83. {
  84. var builder = new Builder(this, _writer);
  85. if (configurator != null)
  86. {
  87. configurator(builder);
  88. }
  89. return builder.Build();
  90. }
  91. // nested classes
  92. /// <summary>
  93. /// Represents a builder for a BsonSerializationContext.
  94. /// </summary>
  95. public class Builder
  96. {
  97. // private fields
  98. private Func<Type, bool> _isDynamicType;
  99. private IBsonWriter _writer;
  100. // constructors
  101. internal Builder(BsonSerializationContext other, IBsonWriter writer)
  102. {
  103. if (writer == null)
  104. {
  105. throw new ArgumentNullException("writer");
  106. }
  107. _writer = writer;
  108. if (other != null)
  109. {
  110. _isDynamicType = other._isDynamicType;
  111. }
  112. else
  113. {
  114. _isDynamicType = t =>
  115. (BsonDefaults.DynamicArraySerializer != null && t == BsonDefaults.DynamicArraySerializer.ValueType) ||
  116. (BsonDefaults.DynamicDocumentSerializer != null && t == BsonDefaults.DynamicDocumentSerializer.ValueType);
  117. }
  118. }
  119. // properties
  120. /// <summary>
  121. /// Gets or sets the function used to determine if a type is a dynamic type.
  122. /// </summary>
  123. public Func<Type, bool> IsDynamicType
  124. {
  125. get { return _isDynamicType; }
  126. set { _isDynamicType = value; }
  127. }
  128. /// <summary>
  129. /// Gets the writer.
  130. /// </summary>
  131. /// <value>
  132. /// The writer.
  133. /// </value>
  134. public IBsonWriter Writer
  135. {
  136. get { return _writer; }
  137. }
  138. // public methods
  139. /// <summary>
  140. /// Builds the BsonSerializationContext instance.
  141. /// </summary>
  142. /// <returns>A BsonSerializationContext.</returns>
  143. internal BsonSerializationContext Build()
  144. {
  145. return new BsonSerializationContext(_writer, _isDynamicType);
  146. }
  147. }
  148. }
  149. }