DictionaryInterfaceImplementerSerializer.cs 13 KB

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