ConventionPack.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. namespace MongoDB.Bson.Serialization.Conventions
  19. {
  20. /// <summary>
  21. /// A mutable pack of conventions.
  22. /// </summary>
  23. public class ConventionPack : IConventionPack, IEnumerable<IConvention>
  24. {
  25. // private fields
  26. private readonly List<IConvention> _conventions;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ConventionPack" /> class.
  30. /// </summary>
  31. public ConventionPack()
  32. {
  33. _conventions = new List<IConvention>();
  34. }
  35. // public properties
  36. /// <summary>
  37. /// Gets the conventions.
  38. /// </summary>
  39. public IEnumerable<IConvention> Conventions
  40. {
  41. get { return _conventions; }
  42. }
  43. // public methods
  44. /// <summary>
  45. /// Adds the specified convention.
  46. /// </summary>
  47. /// <param name="convention">The convention.</param>
  48. /// <exception cref="System.ArgumentNullException"></exception>
  49. public void Add(IConvention convention)
  50. {
  51. if (convention == null)
  52. {
  53. throw new ArgumentNullException("convention");
  54. }
  55. _conventions.Add(convention);
  56. }
  57. /// <summary>
  58. /// Adds a class map convention created using the specified action upon a class map.
  59. /// </summary>
  60. /// <param name="name">The name of the convention.</param>
  61. /// <param name="action">The action the convention should take upon the class map.</param>
  62. public void AddClassMapConvention(string name, Action<BsonClassMap> action)
  63. {
  64. if (name == null)
  65. {
  66. throw new ArgumentNullException("name");
  67. }
  68. Add(new DelegateClassMapConvention(name, action));
  69. }
  70. /// <summary>
  71. /// Adds a member map convention created using the specified action upon a member map.
  72. /// </summary>
  73. /// <param name="name">The name of the convention.</param>
  74. /// <param name="action">The action the convention should take upon the member map.</param>
  75. public void AddMemberMapConvention(string name, Action<BsonMemberMap> action)
  76. {
  77. if (name == null)
  78. {
  79. throw new ArgumentNullException("name");
  80. }
  81. Add(new DelegateMemberMapConvention(name, action));
  82. }
  83. /// <summary>
  84. /// Adds a post processing convention created using the specified action upon a class map.
  85. /// </summary>
  86. /// <param name="name">The name of the convention.</param>
  87. /// <param name="action">The action the convention should take upon the class map.</param>
  88. public void AddPostProcessingConvention(string name, Action<BsonClassMap> action)
  89. {
  90. if (name == null)
  91. {
  92. throw new ArgumentNullException("name");
  93. }
  94. Add(new DelegatePostProcessingConvention(name, action));
  95. }
  96. /// <summary>
  97. /// Adds a range of conventions.
  98. /// </summary>
  99. /// <param name="conventions">The conventions.</param>
  100. /// <exception cref="System.ArgumentNullException"></exception>
  101. public void AddRange(IEnumerable<IConvention> conventions)
  102. {
  103. if (conventions == null)
  104. {
  105. throw new ArgumentNullException("conventions");
  106. }
  107. _conventions.AddRange(conventions);
  108. }
  109. /// <summary>
  110. /// Appends the conventions in another pack to the end of this pack.
  111. /// </summary>
  112. /// <param name="other">The other pack.</param>
  113. public void Append(IConventionPack other)
  114. {
  115. if (other == null)
  116. {
  117. throw new ArgumentNullException("other");
  118. }
  119. AddRange(other.Conventions);
  120. }
  121. /// <summary>
  122. /// Gets an enumerator for the conventions.
  123. /// </summary>
  124. /// <returns>An enumerator.</returns>
  125. public IEnumerator<IConvention> GetEnumerator()
  126. {
  127. return _conventions.GetEnumerator();
  128. }
  129. /// <summary>
  130. /// Inserts the convention after another convention specified by the name.
  131. /// </summary>
  132. /// <param name="name">The name.</param>
  133. /// <param name="convention">The convention.</param>
  134. public void InsertAfter(string name, IConvention convention)
  135. {
  136. if (name == null)
  137. {
  138. throw new ArgumentNullException("name");
  139. }
  140. if (convention == null)
  141. {
  142. throw new ArgumentNullException("convention");
  143. }
  144. var index = _conventions.FindIndex(x => x.Name == name);
  145. if (index == -1)
  146. {
  147. var message = string.Format("Unable to find a convention by the name of '{0}'.", name);
  148. throw new ArgumentOutOfRangeException("name", message);
  149. }
  150. _conventions.Insert(index + 1, convention);
  151. }
  152. /// <summary>
  153. /// Inserts the convention before another convention specified by the name.
  154. /// </summary>
  155. /// <param name="name">The name.</param>
  156. /// <param name="convention">The convention.</param>
  157. public void InsertBefore(string name, IConvention convention)
  158. {
  159. if (name == null)
  160. {
  161. throw new ArgumentNullException("name");
  162. }
  163. if (convention == null)
  164. {
  165. throw new ArgumentNullException("convention");
  166. }
  167. var index = _conventions.FindIndex(x => x.Name == name);
  168. if (index == -1)
  169. {
  170. var message = string.Format("Unable to find a convention by the name of '{0}'.", name);
  171. throw new ArgumentOutOfRangeException("name", message);
  172. }
  173. _conventions.Insert(index, convention);
  174. }
  175. /// <summary>
  176. /// Removes the named convention.
  177. /// </summary>
  178. /// <param name="name">The name of the convention.</param>
  179. public void Remove(string name)
  180. {
  181. if (name == null)
  182. {
  183. throw new ArgumentNullException("name");
  184. }
  185. _conventions.RemoveAll(x => x.Name == name);
  186. }
  187. // explicit interface implementations
  188. IEnumerator IEnumerable.GetEnumerator()
  189. {
  190. return _conventions.GetEnumerator();
  191. }
  192. }
  193. }