DictionaryInterfaceImplementerSerializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using MongoDB.Bson.Serialization.Options;
  20. namespace MongoDB.Bson.Serialization.Serializers
  21. {
  22. /// <summary>
  23. /// Represents a serializer for a class that implements IDictionary.
  24. /// </summary>
  25. /// <typeparam name="TDictionary">The type of the dictionary.</typeparam>
  26. public class DictionaryInterfaceImplementerSerializer<TDictionary> :
  27. DictionarySerializerBase<TDictionary>,
  28. IChildSerializerConfigurable,
  29. IDictionaryRepresentationConfigurable
  30. where TDictionary : class, IDictionary, new()
  31. {
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary}"/> class.
  34. /// </summary>
  35. public DictionaryInterfaceImplementerSerializer()
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary}"/> class.
  40. /// </summary>
  41. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  42. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation)
  43. : base(dictionaryRepresentation)
  44. {
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary}"/> class.
  48. /// </summary>
  49. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  50. /// <param name="keySerializer">The key serializer.</param>
  51. /// <param name="valueSerializer">The value serializer.</param>
  52. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer)
  53. : base(dictionaryRepresentation, keySerializer, valueSerializer)
  54. {
  55. }
  56. // public methods
  57. /// <summary>
  58. /// Returns a serializer that has been reconfigured with the specified dictionary representation.
  59. /// </summary>
  60. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  61. /// <returns>The reconfigured serializer.</returns>
  62. public DictionaryInterfaceImplementerSerializer<TDictionary> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
  63. {
  64. if (dictionaryRepresentation == DictionaryRepresentation)
  65. {
  66. return this;
  67. }
  68. else
  69. {
  70. return new DictionaryInterfaceImplementerSerializer<TDictionary>(dictionaryRepresentation, KeySerializer, ValueSerializer);
  71. }
  72. }
  73. /// <summary>
  74. /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers.
  75. /// </summary>
  76. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  77. /// <param name="keySerializer">The key serializer.</param>
  78. /// <param name="valueSerializer">The value serializer.</param>
  79. /// <returns>The reconfigured serializer.</returns>
  80. public DictionaryInterfaceImplementerSerializer<TDictionary> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer)
  81. {
  82. if (dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer)
  83. {
  84. return this;
  85. }
  86. else
  87. {
  88. return new DictionaryInterfaceImplementerSerializer<TDictionary>(dictionaryRepresentation, keySerializer, valueSerializer);
  89. }
  90. }
  91. /// <summary>
  92. /// Returns a serializer that has been reconfigured with the specified key serializer.
  93. /// </summary>
  94. /// <param name="keySerializer">The key serializer.</param>
  95. /// <returns>The reconfigured serializer.</returns>
  96. public DictionaryInterfaceImplementerSerializer<TDictionary> WithKeySerializer(IBsonSerializer keySerializer)
  97. {
  98. if (keySerializer == KeySerializer)
  99. {
  100. return this;
  101. }
  102. else
  103. {
  104. return new DictionaryInterfaceImplementerSerializer<TDictionary>(DictionaryRepresentation, keySerializer, ValueSerializer);
  105. }
  106. }
  107. /// <summary>
  108. /// Returns a serializer that has been reconfigured with the specified value serializer.
  109. /// </summary>
  110. /// <param name="valueSerializer">The value serializer.</param>
  111. /// <returns>The reconfigured serializer.</returns>
  112. public DictionaryInterfaceImplementerSerializer<TDictionary> WithValueSerializer(IBsonSerializer valueSerializer)
  113. {
  114. if (valueSerializer == ValueSerializer)
  115. {
  116. return this;
  117. }
  118. else
  119. {
  120. return new DictionaryInterfaceImplementerSerializer<TDictionary>(DictionaryRepresentation, KeySerializer, valueSerializer);
  121. }
  122. }
  123. // protected methods
  124. /// <summary>
  125. /// Creates the instance.
  126. /// </summary>
  127. /// <returns>The instance.</returns>
  128. protected override TDictionary CreateInstance()
  129. {
  130. return new TDictionary();
  131. }
  132. // explicit interface implementations
  133. IBsonSerializer IChildSerializerConfigurable.ChildSerializer
  134. {
  135. get { return ValueSerializer; }
  136. }
  137. IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
  138. {
  139. return WithValueSerializer(childSerializer);
  140. }
  141. IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
  142. {
  143. return WithDictionaryRepresentation(dictionaryRepresentation);
  144. }
  145. }
  146. /// <summary>
  147. /// Represents a serializer for a class that implements <see cref="IDictionary{TKey, TValue}"/>.
  148. /// </summary>
  149. /// <typeparam name="TDictionary">The type of the dictionary.</typeparam>
  150. /// <typeparam name="TKey">The type of the key.</typeparam>
  151. /// <typeparam name="TValue">The type of the value.</typeparam>
  152. public class DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> :
  153. DictionarySerializerBase<TDictionary, TKey, TValue>,
  154. IChildSerializerConfigurable,
  155. IDictionaryRepresentationConfigurable<DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>>
  156. where TDictionary : class, IDictionary<TKey, TValue>
  157. {
  158. /// <summary>
  159. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary, TKey, TValue}"/> class.
  160. /// </summary>
  161. public DictionaryInterfaceImplementerSerializer()
  162. {
  163. }
  164. /// <summary>
  165. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary, TKey, TValue}"/> class.
  166. /// </summary>
  167. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  168. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation)
  169. : base(dictionaryRepresentation)
  170. {
  171. }
  172. /// <summary>
  173. /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary, TKey, TValue}"/> class.
  174. /// </summary>
  175. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  176. /// <param name="keySerializer">The key serializer.</param>
  177. /// <param name="valueSerializer">The value serializer.</param>
  178. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer<TKey> keySerializer, IBsonSerializer<TValue> valueSerializer)
  179. : base(dictionaryRepresentation, keySerializer, valueSerializer)
  180. {
  181. }
  182. // public methods
  183. /// <summary>
  184. /// Returns a serializer that has been reconfigured with the specified dictionary representation.
  185. /// </summary>
  186. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  187. /// <returns>The reconfigured serializer.</returns>
  188. public DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
  189. {
  190. if (dictionaryRepresentation == DictionaryRepresentation)
  191. {
  192. return this;
  193. }
  194. else
  195. {
  196. return new DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>(dictionaryRepresentation, KeySerializer, ValueSerializer);
  197. }
  198. }
  199. /// <summary>
  200. /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers.
  201. /// </summary>
  202. /// <param name="dictionaryRepresentation">The dictionary representation.</param>
  203. /// <param name="keySerializer">The key serializer.</param>
  204. /// <param name="valueSerializer">The value serializer.</param>
  205. /// <returns>The reconfigured serializer.</returns>
  206. public DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer<TKey> keySerializer, IBsonSerializer<TValue> valueSerializer)
  207. {
  208. if (dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer)
  209. {
  210. return this;
  211. }
  212. else
  213. {
  214. return new DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>(dictionaryRepresentation, keySerializer, valueSerializer);
  215. }
  216. }
  217. /// <summary>
  218. /// Returns a serializer that has been reconfigured with the specified key serializer.
  219. /// </summary>
  220. /// <param name="keySerializer">The key serializer.</param>
  221. /// <returns>The reconfigured serializer.</returns>
  222. public DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> WithKeySerializer(IBsonSerializer<TKey> keySerializer)
  223. {
  224. if (keySerializer == KeySerializer)
  225. {
  226. return this;
  227. }
  228. else
  229. {
  230. return new DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>(DictionaryRepresentation, keySerializer, ValueSerializer);
  231. }
  232. }
  233. /// <summary>
  234. /// Returns a serializer that has been reconfigured with the specified value serializer.
  235. /// </summary>
  236. /// <param name="valueSerializer">The value serializer.</param>
  237. /// <returns>The reconfigured serializer.</returns>
  238. public DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> WithValueSerializer(IBsonSerializer<TValue> valueSerializer)
  239. {
  240. if (valueSerializer == ValueSerializer)
  241. {
  242. return this;
  243. }
  244. else
  245. {
  246. return new DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>(DictionaryRepresentation, KeySerializer, valueSerializer);
  247. }
  248. }
  249. // explicit interface implementations
  250. IBsonSerializer IChildSerializerConfigurable.ChildSerializer
  251. {
  252. get { return ValueSerializer; }
  253. }
  254. IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
  255. {
  256. return WithValueSerializer((IBsonSerializer<TValue>)childSerializer);
  257. }
  258. IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
  259. {
  260. return WithDictionaryRepresentation(dictionaryRepresentation);
  261. }
  262. /// <inheritdoc/>
  263. protected override ICollection<KeyValuePair<TKey, TValue>> CreateAccumulator()
  264. {
  265. return Activator.CreateInstance<TDictionary>();
  266. }
  267. }
  268. }