/* Copyright 2016-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver
{
///
/// Represents static methods for creating facets.
///
public static class AggregateFacet
{
///
/// Creates a new instance of the class.
///
/// The type of the input documents.
/// The type of the output documents.
/// The facet name.
/// The facet pipeline.
///
/// A new instance of the class
///
public static AggregateFacet Create(string name, PipelineDefinition pipeline)
{
return new AggregateFacet(name, pipeline);
}
}
///
/// Represents a facet to be passed to the Facet method.
///
/// The type of the input documents.
public abstract class AggregateFacet
{
///
/// Initializes a new instance of the class.
///
/// The facet name.
protected AggregateFacet(string name)
{
Name = Ensure.IsNotNull(name, nameof(name));
}
///
/// Gets the facet name.
///
public string Name { get; private set; }
///
/// Gets the output serializer.
///
public abstract IBsonSerializer OutputSerializer { get; }
///
/// Gets the type of the output documents.
///
public abstract Type OutputType { get; }
///
/// Renders the facet pipeline.
///
/// The input serializer.
/// The serializer registry.
/// The rendered pipeline.
public abstract BsonArray RenderPipeline(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry);
}
///
/// Represents a facet to be passed to the Facet method.
///
/// The type of the input documents.
/// The type of the otuput documents.
public class AggregateFacet : AggregateFacet
{
///
/// Initializes a new instance of the class.
///
/// The facet name.
/// The facet pipeline.
public AggregateFacet(string name, PipelineDefinition pipeline)
: base(name)
{
Pipeline = Ensure.IsNotNull(pipeline, nameof(pipeline));
}
///
public override IBsonSerializer OutputSerializer => Pipeline.OutputSerializer;
///
public override Type OutputType => typeof(TOutput);
///
/// Gets the facet pipeline.
///
public PipelineDefinition Pipeline { get; private set; }
///
public override BsonArray RenderPipeline(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry)
{
var renderedPipeline = Pipeline.Render(inputSerializer, serializerRegistry);
return new BsonArray(renderedPipeline.Documents);
}
}
}