ConventionRunner.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Generic;
  17. using System.Linq;
  18. namespace MongoDB.Bson.Serialization.Conventions
  19. {
  20. /// <summary>
  21. /// Runs the conventions against a BsonClassMap and its BsonMemberMaps.
  22. /// </summary>
  23. public class ConventionRunner
  24. {
  25. // private fields
  26. private readonly IEnumerable<IConvention> _conventions;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ConventionRunner" /> class.
  30. /// </summary>
  31. /// <param name="conventions">The conventions.</param>
  32. public ConventionRunner(IConventionPack conventions)
  33. {
  34. if (conventions == null)
  35. {
  36. throw new ArgumentNullException("conventions");
  37. }
  38. _conventions = conventions.Conventions.ToList();
  39. }
  40. // public methods
  41. /// <summary>
  42. /// Applies a modification to the class map.
  43. /// </summary>
  44. /// <param name="classMap">The class map.</param>
  45. public void Apply(BsonClassMap classMap)
  46. {
  47. foreach (var convention in _conventions.OfType<IClassMapConvention>())
  48. {
  49. convention.Apply(classMap);
  50. }
  51. foreach (var convention in _conventions.OfType<IMemberMapConvention>())
  52. {
  53. foreach (var memberMap in classMap.DeclaredMemberMaps)
  54. {
  55. convention.Apply(memberMap);
  56. }
  57. }
  58. foreach (var convention in _conventions.OfType<ICreatorMapConvention>())
  59. {
  60. foreach (var creatorMap in classMap.CreatorMaps)
  61. {
  62. convention.Apply(creatorMap);
  63. }
  64. }
  65. foreach (var convention in _conventions.OfType<IPostProcessingConvention>())
  66. {
  67. convention.PostProcess(classMap);
  68. }
  69. }
  70. }
  71. }