| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /* 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
- {
- /// <summary>
- /// Base class for an index keys definition.
- /// </summary>
- /// <typeparam name="TDocument">The type of the document.</typeparam>
- public abstract class IndexKeysDefinition<TDocument>
- {
- /// <summary>
- /// Renders the index keys definition to a <see cref="BsonDocument"/>.
- /// </summary>
- /// <param name="documentSerializer">The document serializer.</param>
- /// <param name="serializerRegistry">The serializer registry.</param>
- /// <returns>A <see cref="BsonDocument"/>.</returns>
- public abstract BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry);
- /// <summary>
- /// Performs an implicit conversion from <see cref="BsonDocument"/> to <see cref="IndexKeysDefinition{TDocument}"/>.
- /// </summary>
- /// <param name="document">The document.</param>
- /// <returns>
- /// The result of the conversion.
- /// </returns>
- public static implicit operator IndexKeysDefinition<TDocument>(BsonDocument document)
- {
- if (document == null)
- {
- return null;
- }
- return new BsonDocumentIndexKeysDefinition<TDocument>(document);
- }
- /// <summary>
- /// Performs an implicit conversion from <see cref="System.String" /> to <see cref="IndexKeysDefinition{TDocument}" />.
- /// </summary>
- /// <param name="json">The JSON string.</param>
- /// <returns>
- /// The result of the conversion.
- /// </returns>
- public static implicit operator IndexKeysDefinition<TDocument>(string json)
- {
- if (json == null)
- {
- return null;
- }
- return new JsonIndexKeysDefinition<TDocument>(json);
- }
- }
- /// <summary>
- /// A <see cref="BsonDocument"/> based index keys definition.
- /// </summary>
- /// <typeparam name="TDocument">The type of the document.</typeparam>
- public sealed class BsonDocumentIndexKeysDefinition<TDocument> : IndexKeysDefinition<TDocument>
- {
- private readonly BsonDocument _document;
- /// <summary>
- /// Initializes a new instance of the <see cref="BsonDocumentIndexKeysDefinition{TDocument}"/> class.
- /// </summary>
- /// <param name="document">The document.</param>
- public BsonDocumentIndexKeysDefinition(BsonDocument document)
- {
- _document = Ensure.IsNotNull(document, nameof(document));
- }
- /// <summary>
- /// Gets the document.
- /// </summary>
- public BsonDocument Document
- {
- get { return _document; }
- }
- /// <inheritdoc />
- public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
- {
- return _document;
- }
- }
- /// <summary>
- /// A JSON <see cref="String" /> based index keys definition.
- /// </summary>
- /// <typeparam name="TDocument">The type of the document.</typeparam>
- public sealed class JsonIndexKeysDefinition<TDocument> : IndexKeysDefinition<TDocument>
- {
- private readonly string _json;
- /// <summary>
- /// Initializes a new instance of the <see cref="JsonIndexKeysDefinition{TDocument}"/> class.
- /// </summary>
- /// <param name="json">The json.</param>
- public JsonIndexKeysDefinition(string json)
- {
- _json = Ensure.IsNotNull(json, nameof(json));
- }
- /// <summary>
- /// Gets the json.
- /// </summary>
- public string Json
- {
- get { return _json; }
- }
- /// <inheritdoc />
- public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
- {
- return BsonDocument.Parse(_json);
- }
- }
- }
|