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