BsonSerializerRegistry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Concurrent;
  17. using System.Reflection;
  18. namespace MongoDB.Bson.Serialization
  19. {
  20. /// <summary>
  21. /// Default, global implementation of an <see cref="IBsonSerializerRegistry"/>.
  22. /// </summary>
  23. public sealed class BsonSerializerRegistry : IBsonSerializerRegistry
  24. {
  25. // private fields
  26. private readonly ConcurrentDictionary<Type, IBsonSerializer> _cache;
  27. private readonly ConcurrentStack<IBsonSerializationProvider> _serializationProviders;
  28. // constructors
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="BsonSerializerRegistry"/> class.
  31. /// </summary>
  32. public BsonSerializerRegistry()
  33. {
  34. _cache = new ConcurrentDictionary<Type,IBsonSerializer>();
  35. _serializationProviders = new ConcurrentStack<IBsonSerializationProvider>();
  36. }
  37. // public methods
  38. /// <summary>
  39. /// Gets the serializer for the specified <paramref name="type" />.
  40. /// </summary>
  41. /// <param name="type">The type.</param>
  42. /// <returns>
  43. /// The serializer.
  44. /// </returns>
  45. public IBsonSerializer GetSerializer(Type type)
  46. {
  47. if (type == null)
  48. {
  49. throw new ArgumentNullException("type");
  50. }
  51. var typeInfo = type.GetTypeInfo();
  52. if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
  53. {
  54. var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
  55. throw new ArgumentException(message, "type");
  56. }
  57. return _cache.GetOrAdd(type, CreateSerializer);
  58. }
  59. /// <summary>
  60. /// Gets the serializer for the specified <typeparamref name="T" />.
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <returns>
  64. /// The serializer.
  65. /// </returns>
  66. public IBsonSerializer<T> GetSerializer<T>()
  67. {
  68. return (IBsonSerializer<T>)GetSerializer(typeof(T));
  69. }
  70. /// <summary>
  71. /// Registers the serializer.
  72. /// </summary>
  73. /// <param name="type">The type.</param>
  74. /// <param name="serializer">The serializer.</param>
  75. public void RegisterSerializer(Type type, IBsonSerializer serializer)
  76. {
  77. if (type == null)
  78. {
  79. throw new ArgumentNullException("type");
  80. }
  81. if (serializer == null)
  82. {
  83. throw new ArgumentNullException("serializer");
  84. }
  85. var typeInfo = type.GetTypeInfo();
  86. if (typeof(BsonValue).GetTypeInfo().IsAssignableFrom(type))
  87. {
  88. var message = string.Format("A serializer cannot be registered for type {0} because it is a subclass of BsonValue.", BsonUtils.GetFriendlyTypeName(type));
  89. throw new BsonSerializationException(message);
  90. }
  91. if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
  92. {
  93. var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
  94. throw new ArgumentException(message, "type");
  95. }
  96. if (!_cache.TryAdd(type, serializer))
  97. {
  98. var message = string.Format("There is already a serializer registered for type {0}.", BsonUtils.GetFriendlyTypeName(type));
  99. throw new BsonSerializationException(message);
  100. }
  101. }
  102. /// <summary>
  103. /// Registers the serialization provider. This behaves like a stack, so the
  104. /// last provider registered is the first provider consulted.
  105. /// </summary>
  106. /// <param name="serializationProvider">The serialization provider.</param>
  107. public void RegisterSerializationProvider(IBsonSerializationProvider serializationProvider)
  108. {
  109. if (serializationProvider == null)
  110. {
  111. throw new ArgumentNullException("serializationProvider");
  112. }
  113. _serializationProviders.Push(serializationProvider);
  114. }
  115. // private methods
  116. private IBsonSerializer CreateSerializer(Type type)
  117. {
  118. foreach (var serializationProvider in _serializationProviders)
  119. {
  120. IBsonSerializer serializer;
  121. var registryAwareSerializationProvider = serializationProvider as IRegistryAwareBsonSerializationProvider;
  122. if (registryAwareSerializationProvider != null)
  123. {
  124. serializer = registryAwareSerializationProvider.GetSerializer(type, this);
  125. }
  126. else
  127. {
  128. serializer = serializationProvider.GetSerializer(type);
  129. }
  130. if (serializer != null)
  131. {
  132. return serializer;
  133. }
  134. }
  135. var message = string.Format("No serializer found for type {0}.", type.FullName);
  136. throw new BsonSerializationException(message);
  137. }
  138. }
  139. }