/* Copyright 2010-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
{
///
/// A rendered command.
///
/// The type of the result.
public sealed class RenderedCommand
{
private readonly BsonDocument _document;
private readonly IBsonSerializer _resultSerializer;
///
/// Initializes a new instance of the class.
///
/// The document.
/// The result serializer.
public RenderedCommand(BsonDocument document, IBsonSerializer resultSerializer)
{
_document = Ensure.IsNotNull(document, nameof(document));
_resultSerializer = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
}
///
/// Gets the document.
///
public BsonDocument Document
{
get { return _document; }
}
///
/// Gets the result serializer.
///
public IBsonSerializer ResultSerializer
{
get { return _resultSerializer; }
}
}
///
/// Base class for commands.
///
/// The type of the result.
public abstract class Command
{
///
/// Renders the command to a .
///
/// The serializer registry.
/// A .
public abstract RenderedCommand Render(IBsonSerializerRegistry serializerRegistry);
///
/// Performs an implicit conversion from to .
///
/// The document.
///
/// The result of the conversion.
///
public static implicit operator Command(BsonDocument document)
{
return new BsonDocumentCommand(document);
}
///
/// Performs an implicit conversion from to .
///
/// The JSON string.
///
/// The result of the conversion.
///
public static implicit operator Command(string json)
{
return new JsonCommand(json);
}
}
///
/// A based command.
///
/// The type of the result.
public sealed class BsonDocumentCommand : Command
{
private readonly BsonDocument _document;
private readonly IBsonSerializer _resultSerializer;
///
/// Initializes a new instance of the class.
///
/// The document.
/// The result serializer.
public BsonDocumentCommand(BsonDocument document, IBsonSerializer resultSerializer = null)
{
_document = Ensure.IsNotNull(document, nameof(document));
_resultSerializer = resultSerializer;
}
///
/// Gets the document.
///
public BsonDocument Document
{
get { return _document; }
}
///
/// Gets the result serializer.
///
public IBsonSerializer ResultSerializer
{
get { return _resultSerializer; }
}
///
public override RenderedCommand Render(IBsonSerializerRegistry serializerRegistry)
{
return new RenderedCommand(
_document,
_resultSerializer ?? serializerRegistry.GetSerializer());
}
}
///
/// A JSON based command.
///
/// The type of the result.
public sealed class JsonCommand : Command
{
private readonly string _json;
private readonly IBsonSerializer _resultSerializer;
///
/// Initializes a new instance of the class.
///
/// The json.
/// The result serializer.
public JsonCommand(string json, IBsonSerializer resultSerializer = null)
{
_json = Ensure.IsNotNullOrEmpty(json, nameof(json));
_resultSerializer = resultSerializer; // can be null
}
///
/// Gets the json.
///
public string Json
{
get { return _json; }
}
///
/// Gets the result serializer.
///
public IBsonSerializer ResultSerializer
{
get { return _resultSerializer; }
}
///
public override RenderedCommand Render(IBsonSerializerRegistry serializerRegistry)
{
return new RenderedCommand(
BsonDocument.Parse(_json),
_resultSerializer ?? serializerRegistry.GetSerializer());
}
}
///
/// An based command.
///
/// The type of the result.
public sealed class ObjectCommand : Command
{
private readonly object _obj;
private readonly IBsonSerializer _resultSerializer;
///
/// Initializes a new instance of the class.
///
/// The object.
/// The result serializer.
public ObjectCommand(object obj, IBsonSerializer resultSerializer = null)
{
_obj = Ensure.IsNotNull(obj, nameof(obj));
_resultSerializer = resultSerializer;
}
///
/// Gets the object.
///
public object Object
{
get { return _obj; }
}
///
/// Gets the result serializer.
///
public IBsonSerializer ResultSerializer
{
get { return _resultSerializer; }
}
///
public override RenderedCommand Render(IBsonSerializerRegistry serializerRegistry)
{
var serializer = serializerRegistry.GetSerializer(_obj.GetType());
return new RenderedCommand(
new BsonDocumentWrapper(_obj, serializer),
_resultSerializer ?? serializerRegistry.GetSerializer());
}
}
}