AggregateExpressionDefinition.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright 2016-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. /// An aggregation expression.
  25. /// </summary>
  26. /// <typeparam name="TSource">The type of the source.</typeparam>
  27. /// <typeparam name="TResult">The type of the result.</typeparam>
  28. public abstract class AggregateExpressionDefinition<TSource, TResult>
  29. {
  30. #region static
  31. // public implicit conversions
  32. /// <summary>
  33. /// Performs an implicit conversion from <see cref="BsonValue"/> to <see cref="AggregateExpressionDefinition{TSource, TResult}"/>.
  34. /// </summary>
  35. /// <param name="expression">The expression.</param>
  36. /// <returns>
  37. /// The result of the conversion.
  38. /// </returns>
  39. public static implicit operator AggregateExpressionDefinition<TSource, TResult>(BsonValue expression)
  40. {
  41. Ensure.IsNotNull(expression, nameof(expression));
  42. return new BsonValueAggregateExpressionDefinition<TSource, TResult>(expression);
  43. }
  44. /// <summary>
  45. /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="AggregateExpressionDefinition{TSource, TResult}"/>.
  46. /// </summary>
  47. /// <param name="expression">The expression.</param>
  48. /// <returns>
  49. /// The result of the conversion.
  50. /// </returns>
  51. public static implicit operator AggregateExpressionDefinition<TSource, TResult>(string expression)
  52. {
  53. Ensure.IsNotNullOrEmpty(expression, nameof(expression));
  54. if (expression[0] == '{')
  55. {
  56. return new BsonValueAggregateExpressionDefinition<TSource, TResult>(BsonDocument.Parse(expression));
  57. }
  58. else
  59. {
  60. return new BsonValueAggregateExpressionDefinition<TSource, TResult>(new BsonString(expression));
  61. }
  62. }
  63. #endregion
  64. /// <summary>
  65. /// Renders the aggregation expression.
  66. /// </summary>
  67. /// <param name="sourceSerializer">The source serializer.</param>
  68. /// <param name="serializerRegistry">The serializer registry.</param>
  69. /// <returns>The rendered aggregation expression.</returns>
  70. public abstract BsonValue Render(IBsonSerializer<TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry);
  71. }
  72. /// <summary>
  73. /// A <see cref="BsonValue"/> based aggregate expression.
  74. /// </summary>
  75. /// <typeparam name="TSource">The type of the source.</typeparam>
  76. /// <typeparam name="TResult">The type of the result.</typeparam>
  77. /// <seealso cref="MongoDB.Driver.AggregateExpressionDefinition{TSource, TResult}" />
  78. public sealed class BsonValueAggregateExpressionDefinition<TSource, TResult> : AggregateExpressionDefinition<TSource, TResult>
  79. {
  80. // private fields
  81. private readonly BsonValue _expression;
  82. // constructors
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="BsonValueAggregateExpressionDefinition{TSource, TResult}"/> class.
  85. /// </summary>
  86. /// <param name="expression">The expression.</param>
  87. public BsonValueAggregateExpressionDefinition(BsonValue expression)
  88. {
  89. _expression = Ensure.IsNotNull(expression, nameof(expression));
  90. }
  91. // public methods
  92. /// <inheritdoc/>
  93. public override BsonValue Render(IBsonSerializer<TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
  94. {
  95. return _expression;
  96. }
  97. }
  98. /// <summary>
  99. /// A <see cref="BsonValue"/> based aggregate expression.
  100. /// </summary>
  101. /// <typeparam name="TSource">The type of the source.</typeparam>
  102. /// <typeparam name="TResult">The type of the result.</typeparam>
  103. /// <seealso cref="MongoDB.Driver.AggregateExpressionDefinition{TSource, TResult}" />
  104. public sealed class ExpressionAggregateExpressionDefinition<TSource, TResult> : AggregateExpressionDefinition<TSource, TResult>
  105. {
  106. // private fields
  107. private readonly Expression<Func<TSource, TResult>> _expression;
  108. private readonly ExpressionTranslationOptions _translationOptions;
  109. // constructors
  110. /// <summary>
  111. /// Initializes a new instance of the <see cref="ExpressionAggregateExpressionDefinition{TSource, TResult}" /> class.
  112. /// </summary>
  113. /// <param name="expression">The expression.</param>
  114. /// <param name="translationOptions">The translation options.</param>
  115. public ExpressionAggregateExpressionDefinition(Expression<Func<TSource, TResult>> expression, ExpressionTranslationOptions translationOptions)
  116. {
  117. _expression = Ensure.IsNotNull(expression, nameof(expression));
  118. _translationOptions = translationOptions; // can be null
  119. }
  120. // public methods
  121. /// <inheritdoc/>
  122. public override BsonValue Render(IBsonSerializer<TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
  123. {
  124. return AggregateExpressionTranslator.Translate(_expression, sourceSerializer, serializerRegistry, _translationOptions);
  125. }
  126. }
  127. }