Command.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 System.Linq.Expressions;
  17. using MongoDB.Bson;
  18. using MongoDB.Bson.Serialization;
  19. using MongoDB.Driver.Core.Misc;
  20. using MongoDB.Driver.Linq.Translators;
  21. namespace MongoDB.Driver
  22. {
  23. /// <summary>
  24. /// A rendered command.
  25. /// </summary>
  26. /// <typeparam name="TResult">The type of the result.</typeparam>
  27. public sealed class RenderedCommand<TResult>
  28. {
  29. private readonly BsonDocument _document;
  30. private readonly IBsonSerializer<TResult> _resultSerializer;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="RenderedCommand{TResult}" /> class.
  33. /// </summary>
  34. /// <param name="document">The document.</param>
  35. /// <param name="resultSerializer">The result serializer.</param>
  36. public RenderedCommand(BsonDocument document, IBsonSerializer<TResult> resultSerializer)
  37. {
  38. _document = Ensure.IsNotNull(document, nameof(document));
  39. _resultSerializer = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
  40. }
  41. /// <summary>
  42. /// Gets the document.
  43. /// </summary>
  44. public BsonDocument Document
  45. {
  46. get { return _document; }
  47. }
  48. /// <summary>
  49. /// Gets the result serializer.
  50. /// </summary>
  51. public IBsonSerializer<TResult> ResultSerializer
  52. {
  53. get { return _resultSerializer; }
  54. }
  55. }
  56. /// <summary>
  57. /// Base class for commands.
  58. /// </summary>
  59. /// <typeparam name="TResult">The type of the result.</typeparam>
  60. public abstract class Command<TResult>
  61. {
  62. /// <summary>
  63. /// Renders the command to a <see cref="RenderedCommand{TResult}" />.
  64. /// </summary>
  65. /// <param name="serializerRegistry">The serializer registry.</param>
  66. /// <returns>A <see cref="RenderedCommand{TResult}" />.</returns>
  67. public abstract RenderedCommand<TResult> Render(IBsonSerializerRegistry serializerRegistry);
  68. /// <summary>
  69. /// Performs an implicit conversion from <see cref="BsonDocument"/> to <see cref="Command{TResult}"/>.
  70. /// </summary>
  71. /// <param name="document">The document.</param>
  72. /// <returns>
  73. /// The result of the conversion.
  74. /// </returns>
  75. public static implicit operator Command<TResult>(BsonDocument document)
  76. {
  77. return new BsonDocumentCommand<TResult>(document);
  78. }
  79. /// <summary>
  80. /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="Command{TResult}"/>.
  81. /// </summary>
  82. /// <param name="json">The JSON string.</param>
  83. /// <returns>
  84. /// The result of the conversion.
  85. /// </returns>
  86. public static implicit operator Command<TResult>(string json)
  87. {
  88. return new JsonCommand<TResult>(json);
  89. }
  90. }
  91. /// <summary>
  92. /// A <see cref="BsonDocument" /> based command.
  93. /// </summary>
  94. /// <typeparam name="TResult">The type of the result.</typeparam>
  95. public sealed class BsonDocumentCommand<TResult> : Command<TResult>
  96. {
  97. private readonly BsonDocument _document;
  98. private readonly IBsonSerializer<TResult> _resultSerializer;
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="BsonDocumentCommand{TResult}"/> class.
  101. /// </summary>
  102. /// <param name="document">The document.</param>
  103. /// <param name="resultSerializer">The result serializer.</param>
  104. public BsonDocumentCommand(BsonDocument document, IBsonSerializer<TResult> resultSerializer = null)
  105. {
  106. _document = Ensure.IsNotNull(document, nameof(document));
  107. _resultSerializer = resultSerializer;
  108. }
  109. /// <summary>
  110. /// Gets the document.
  111. /// </summary>
  112. public BsonDocument Document
  113. {
  114. get { return _document; }
  115. }
  116. /// <summary>
  117. /// Gets the result serializer.
  118. /// </summary>
  119. public IBsonSerializer<TResult> ResultSerializer
  120. {
  121. get { return _resultSerializer; }
  122. }
  123. /// <inheritdoc />
  124. public override RenderedCommand<TResult> Render(IBsonSerializerRegistry serializerRegistry)
  125. {
  126. return new RenderedCommand<TResult>(
  127. _document,
  128. _resultSerializer ?? serializerRegistry.GetSerializer<TResult>());
  129. }
  130. }
  131. /// <summary>
  132. /// A JSON <see cref="String" /> based command.
  133. /// </summary>
  134. /// <typeparam name="TResult">The type of the result.</typeparam>
  135. public sealed class JsonCommand<TResult> : Command<TResult>
  136. {
  137. private readonly string _json;
  138. private readonly IBsonSerializer<TResult> _resultSerializer;
  139. /// <summary>
  140. /// Initializes a new instance of the <see cref="JsonCommand{TResult}"/> class.
  141. /// </summary>
  142. /// <param name="json">The json.</param>
  143. /// <param name="resultSerializer">The result serializer.</param>
  144. public JsonCommand(string json, IBsonSerializer<TResult> resultSerializer = null)
  145. {
  146. _json = Ensure.IsNotNullOrEmpty(json, nameof(json));
  147. _resultSerializer = resultSerializer; // can be null
  148. }
  149. /// <summary>
  150. /// Gets the json.
  151. /// </summary>
  152. public string Json
  153. {
  154. get { return _json; }
  155. }
  156. /// <summary>
  157. /// Gets the result serializer.
  158. /// </summary>
  159. public IBsonSerializer<TResult> ResultSerializer
  160. {
  161. get { return _resultSerializer; }
  162. }
  163. /// <inheritdoc />
  164. public override RenderedCommand<TResult> Render(IBsonSerializerRegistry serializerRegistry)
  165. {
  166. return new RenderedCommand<TResult>(
  167. BsonDocument.Parse(_json),
  168. _resultSerializer ?? serializerRegistry.GetSerializer<TResult>());
  169. }
  170. }
  171. /// <summary>
  172. /// An <see cref="Object" /> based command.
  173. /// </summary>
  174. /// <typeparam name="TResult">The type of the result.</typeparam>
  175. public sealed class ObjectCommand<TResult> : Command<TResult>
  176. {
  177. private readonly object _obj;
  178. private readonly IBsonSerializer<TResult> _resultSerializer;
  179. /// <summary>
  180. /// Initializes a new instance of the <see cref="ObjectCommand{TResult}"/> class.
  181. /// </summary>
  182. /// <param name="obj">The object.</param>
  183. /// <param name="resultSerializer">The result serializer.</param>
  184. public ObjectCommand(object obj, IBsonSerializer<TResult> resultSerializer = null)
  185. {
  186. _obj = Ensure.IsNotNull(obj, nameof(obj));
  187. _resultSerializer = resultSerializer;
  188. }
  189. /// <summary>
  190. /// Gets the object.
  191. /// </summary>
  192. public object Object
  193. {
  194. get { return _obj; }
  195. }
  196. /// <summary>
  197. /// Gets the result serializer.
  198. /// </summary>
  199. public IBsonSerializer<TResult> ResultSerializer
  200. {
  201. get { return _resultSerializer; }
  202. }
  203. /// <inheritdoc />
  204. public override RenderedCommand<TResult> Render(IBsonSerializerRegistry serializerRegistry)
  205. {
  206. var serializer = serializerRegistry.GetSerializer(_obj.GetType());
  207. return new RenderedCommand<TResult>(
  208. new BsonDocumentWrapper(_obj, serializer),
  209. _resultSerializer ?? serializerRegistry.GetSerializer<TResult>());
  210. }
  211. }
  212. }