IndexKeysDefinition.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2010-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. /// Base class for an index keys definition.
  23. /// </summary>
  24. /// <typeparam name="TDocument">The type of the document.</typeparam>
  25. public abstract class IndexKeysDefinition<TDocument>
  26. {
  27. /// <summary>
  28. /// Renders the index keys definition to a <see cref="BsonDocument"/>.
  29. /// </summary>
  30. /// <param name="documentSerializer">The document serializer.</param>
  31. /// <param name="serializerRegistry">The serializer registry.</param>
  32. /// <returns>A <see cref="BsonDocument"/>.</returns>
  33. public abstract BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry);
  34. /// <summary>
  35. /// Performs an implicit conversion from <see cref="BsonDocument"/> to <see cref="IndexKeysDefinition{TDocument}"/>.
  36. /// </summary>
  37. /// <param name="document">The document.</param>
  38. /// <returns>
  39. /// The result of the conversion.
  40. /// </returns>
  41. public static implicit operator IndexKeysDefinition<TDocument>(BsonDocument document)
  42. {
  43. if (document == null)
  44. {
  45. return null;
  46. }
  47. return new BsonDocumentIndexKeysDefinition<TDocument>(document);
  48. }
  49. /// <summary>
  50. /// Performs an implicit conversion from <see cref="System.String" /> to <see cref="IndexKeysDefinition{TDocument}" />.
  51. /// </summary>
  52. /// <param name="json">The JSON string.</param>
  53. /// <returns>
  54. /// The result of the conversion.
  55. /// </returns>
  56. public static implicit operator IndexKeysDefinition<TDocument>(string json)
  57. {
  58. if (json == null)
  59. {
  60. return null;
  61. }
  62. return new JsonIndexKeysDefinition<TDocument>(json);
  63. }
  64. }
  65. /// <summary>
  66. /// A <see cref="BsonDocument"/> based index keys definition.
  67. /// </summary>
  68. /// <typeparam name="TDocument">The type of the document.</typeparam>
  69. public sealed class BsonDocumentIndexKeysDefinition<TDocument> : IndexKeysDefinition<TDocument>
  70. {
  71. private readonly BsonDocument _document;
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="BsonDocumentIndexKeysDefinition{TDocument}"/> class.
  74. /// </summary>
  75. /// <param name="document">The document.</param>
  76. public BsonDocumentIndexKeysDefinition(BsonDocument document)
  77. {
  78. _document = Ensure.IsNotNull(document, nameof(document));
  79. }
  80. /// <summary>
  81. /// Gets the document.
  82. /// </summary>
  83. public BsonDocument Document
  84. {
  85. get { return _document; }
  86. }
  87. /// <inheritdoc />
  88. public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
  89. {
  90. return _document;
  91. }
  92. }
  93. /// <summary>
  94. /// A JSON <see cref="String" /> based index keys definition.
  95. /// </summary>
  96. /// <typeparam name="TDocument">The type of the document.</typeparam>
  97. public sealed class JsonIndexKeysDefinition<TDocument> : IndexKeysDefinition<TDocument>
  98. {
  99. private readonly string _json;
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="JsonIndexKeysDefinition{TDocument}"/> class.
  102. /// </summary>
  103. /// <param name="json">The json.</param>
  104. public JsonIndexKeysDefinition(string json)
  105. {
  106. _json = Ensure.IsNotNull(json, nameof(json));
  107. }
  108. /// <summary>
  109. /// Gets the json.
  110. /// </summary>
  111. public string Json
  112. {
  113. get { return _json; }
  114. }
  115. /// <inheritdoc />
  116. public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
  117. {
  118. return BsonDocument.Parse(_json);
  119. }
  120. }
  121. }