AggregateFacet.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright 2016-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 MongoDB.Bson;
  17. using MongoDB.Bson.Serialization;
  18. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver
  20. {
  21. /// <summary>
  22. /// Represents static methods for creating facets.
  23. /// </summary>
  24. public static class AggregateFacet
  25. {
  26. /// <summary>
  27. /// Creates a new instance of the <see cref="AggregateFacet{TInput, TOutput}" /> class.
  28. /// </summary>
  29. /// <typeparam name="TInput">The type of the input documents.</typeparam>
  30. /// <typeparam name="TOutput">The type of the output documents.</typeparam>
  31. /// <param name="name">The facet name.</param>
  32. /// <param name="pipeline">The facet pipeline.</param>
  33. /// <returns>
  34. /// A new instance of the <see cref="AggregateFacet{TInput, TOutput}" /> class
  35. /// </returns>
  36. public static AggregateFacet<TInput, TOutput> Create<TInput, TOutput>(string name, PipelineDefinition<TInput, TOutput> pipeline)
  37. {
  38. return new AggregateFacet<TInput, TOutput>(name, pipeline);
  39. }
  40. }
  41. /// <summary>
  42. /// Represents a facet to be passed to the Facet method.
  43. /// </summary>
  44. /// <typeparam name="TInput">The type of the input documents.</typeparam>
  45. public abstract class AggregateFacet<TInput>
  46. {
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="AggregateFacet{TInput}"/> class.
  49. /// </summary>
  50. /// <param name="name">The facet name.</param>
  51. protected AggregateFacet(string name)
  52. {
  53. Name = Ensure.IsNotNull(name, nameof(name));
  54. }
  55. /// <summary>
  56. /// Gets the facet name.
  57. /// </summary>
  58. public string Name { get; private set; }
  59. /// <summary>
  60. /// Gets the output serializer.
  61. /// </summary>
  62. public abstract IBsonSerializer OutputSerializer { get; }
  63. /// <summary>
  64. /// Gets the type of the output documents.
  65. /// </summary>
  66. public abstract Type OutputType { get; }
  67. /// <summary>
  68. /// Renders the facet pipeline.
  69. /// </summary>
  70. /// <param name="inputSerializer">The input serializer.</param>
  71. /// <param name="serializerRegistry">The serializer registry.</param>
  72. /// <returns>The rendered pipeline.</returns>
  73. public abstract BsonArray RenderPipeline(IBsonSerializer<TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry);
  74. }
  75. /// <summary>
  76. /// Represents a facet to be passed to the Facet method.
  77. /// </summary>
  78. /// <typeparam name="TInput">The type of the input documents.</typeparam>
  79. /// <typeparam name="TOutput">The type of the otuput documents.</typeparam>
  80. public class AggregateFacet<TInput, TOutput> : AggregateFacet<TInput>
  81. {
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="AggregateFacet{TInput, TOutput}"/> class.
  84. /// </summary>
  85. /// <param name="name">The facet name.</param>
  86. /// <param name="pipeline">The facet pipeline.</param>
  87. public AggregateFacet(string name, PipelineDefinition<TInput, TOutput> pipeline)
  88. : base(name)
  89. {
  90. Pipeline = Ensure.IsNotNull(pipeline, nameof(pipeline));
  91. }
  92. /// <inheritdoc/>
  93. public override IBsonSerializer OutputSerializer => Pipeline.OutputSerializer;
  94. /// <inheritdoc/>
  95. public override Type OutputType => typeof(TOutput);
  96. /// <summary>
  97. /// Gets the facet pipeline.
  98. /// </summary>
  99. public PipelineDefinition<TInput, TOutput> Pipeline { get; private set; }
  100. /// <inheritdoc/>
  101. public override BsonArray RenderPipeline(IBsonSerializer<TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry)
  102. {
  103. var renderedPipeline = Pipeline.Render(inputSerializer, serializerRegistry);
  104. return new BsonArray(renderedPipeline.Documents);
  105. }
  106. }
  107. }