FieldAsDocumentExpression.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright 2015-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.Serialization;
  18. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver.Linq.Expressions
  20. {
  21. internal sealed class FieldAsDocumentExpression : SerializationExpression, IFieldExpression
  22. {
  23. private readonly Expression _expression;
  24. private readonly string _fieldName;
  25. private readonly IBsonSerializer _serializer;
  26. public FieldAsDocumentExpression(Expression expression, string fieldName, IBsonSerializer serializer)
  27. {
  28. _expression = Ensure.IsNotNull(expression, nameof(expression));
  29. _fieldName = Ensure.IsNotNull(fieldName, nameof(fieldName));
  30. _serializer = Ensure.IsNotNull(serializer, nameof(serializer));
  31. }
  32. public Expression Document
  33. {
  34. get { return null; }
  35. }
  36. public Expression Expression
  37. {
  38. get { return _expression; }
  39. }
  40. public string FieldName
  41. {
  42. get { return _fieldName; }
  43. }
  44. public override ExtensionExpressionType ExtensionType
  45. {
  46. get { return ExtensionExpressionType.FieldAsDocument; }
  47. }
  48. public override IBsonSerializer Serializer
  49. {
  50. get { return _serializer; }
  51. }
  52. public override Type Type
  53. {
  54. get { return _serializer.ValueType; }
  55. }
  56. public override string ToString()
  57. {
  58. return "{" + _fieldName + "}";
  59. }
  60. public FieldAsDocumentExpression Update(Expression expression)
  61. {
  62. if (expression != _expression)
  63. {
  64. return new FieldAsDocumentExpression(expression, _fieldName, _serializer);
  65. }
  66. return this;
  67. }
  68. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  69. {
  70. return visitor.VisitDocumentWrappedField(this);
  71. }
  72. }
  73. }