|
| 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 | + |
| 16 | +using System; |
| 17 | +using System.Linq.Expressions; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Bson.Serialization.Serializers; |
| 20 | +using MongoDB.Driver.Core.Misc; |
| 21 | + |
| 22 | +namespace MongoDB.Driver |
| 23 | +{ |
| 24 | + internal interface IClientSideProjectionDeserializer |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + internal static class ClientSideProjectionDeserializer |
| 29 | + { |
| 30 | + public static IBsonSerializer Create( |
| 31 | + IBsonSerializer inputSerializer, |
| 32 | + LambdaExpression projector) |
| 33 | + { |
| 34 | + var inputType = inputSerializer.ValueType; |
| 35 | + var projectionType = projector.ReturnType; |
| 36 | + var serializerType = typeof(ClientSideProjectionDeserializer<,>).MakeGenericType(inputType, projectionType); |
| 37 | + var projectorDelegate = projector.Compile(); |
| 38 | + return (IBsonSerializer)Activator.CreateInstance(serializerType, inputSerializer, projectorDelegate); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// A deserializer for doing client side projections. |
| 44 | + /// </summary> |
| 45 | + /// <typeparam name="TInput">The type of the input.</typeparam> |
| 46 | + /// <typeparam name="TProjection">The type of the projection.</typeparam> |
| 47 | + public sealed class ClientSideProjectionDeserializer<TInput, TProjection> : SerializerBase<TProjection>, IClientSideProjectionDeserializer |
| 48 | + { |
| 49 | + private readonly Func<TInput, TProjection> _projector; |
| 50 | + private readonly IBsonSerializer<TInput> _inputSerializer; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="ClientSideProjectionDeserializer{TInput, TProjection}"/> class. |
| 54 | + /// </summary> |
| 55 | + /// <param name="inputSerializer">The input serializer.</param> |
| 56 | + /// <param name="projector">The client side projector.</param> |
| 57 | + public ClientSideProjectionDeserializer( |
| 58 | + IBsonSerializer<TInput> inputSerializer, |
| 59 | + Func<TInput, TProjection> projector) |
| 60 | + { |
| 61 | + _inputSerializer = Ensure.IsNotNull(inputSerializer, nameof(inputSerializer)); |
| 62 | + _projector = Ensure.IsNotNull(projector, nameof(projector)); |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc/> |
| 66 | + public override TProjection Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
| 67 | + { |
| 68 | + var document = _inputSerializer.Deserialize(context); |
| 69 | + return _projector(document); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments