public PipelineBindingContext(IBsonSerializerRegistry serializerRegistry)
 {
     _serializerRegistry = Ensure.IsNotNull(serializerRegistry, nameof(serializerRegistry));
     _correlationMapping = new Dictionary<Expression, Guid>();
     _expressionMapping = new Dictionary<Expression, Expression>();
     _memberMapping = new Dictionary<MemberInfo, Expression>();
 }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            #if NETSTANDARD1_6
            var serializerAttributes = typeInfo.GetCustomAttributes(typeof(BsonSerializerAttribute), false).ToArray(); // don't inherit
            #else
            var serializerAttributes = typeInfo.GetCustomAttributes(typeof(BsonSerializerAttribute), false); // don't inherit
            #endif
            if (serializerAttributes.Length == 1)
            {
                var serializerAttribute = (BsonSerializerAttribute)serializerAttributes[0];
                return serializerAttribute.CreateSerializer(type);
            }

            return null;
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            Type serializerType;
            if (__serializersTypes.TryGetValue(type, out serializerType))
            {
                return CreateSerializer(serializerType, serializerRegistry);
            }

            if (typeInfo.IsGenericType && !typeInfo.ContainsGenericParameters)
            {
                Type serializerTypeDefinition;
                if (__serializersTypes.TryGetValue(type.GetGenericTypeDefinition(), out serializerTypeDefinition))
                {
                    return CreateGenericSerializer(serializerTypeDefinition, type.GetGenericArguments(), serializerRegistry);
                }
            }

            if (typeInfo.IsEnum)
            {
                return CreateGenericSerializer(typeof(EnumSerializer<>), new[] { type }, serializerRegistry);
            }

            return null;
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            if ((typeInfo.IsClass || (typeInfo.IsValueType && !typeInfo.IsPrimitive)) &&
                !typeof(Array).IsAssignableFrom(type) &&
                !typeof(Enum).IsAssignableFrom(type))
            {
                var classMap = BsonClassMap.LookupClassMap(type);
                var classMapSerializerDefinition = typeof(BsonClassMapSerializer<>);
                var classMapSerializerType = classMapSerializerDefinition.MakeGenericType(type);
                return (IBsonSerializer)Activator.CreateInstance(classMapSerializerType, classMap);
            }

            return null;
        }
        public static QueryableExecutionModel Build(Expression node, IBsonSerializerRegistry serializerRegistry)
        {
            var builder = new QueryableExecutionModelBuilder(serializerRegistry);
            builder.Visit(node);

            return (QueryableExecutionModel)Activator.CreateInstance(
                typeof(AggregateQueryableExecutionModel<>).MakeGenericType(builder._serializer.ValueType),
                builder._stages,
                builder._serializer);
        }
        public static QueryableTranslation Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
        {
            var translator = new QueryableTranslator(serializerRegistry);
            translator.Translate(node);

            var model = (QueryableExecutionModel)Activator.CreateInstance(
                typeof(AggregateQueryableExecutionModel<>).MakeGenericType(translator._outputSerializer.ValueType),
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new object[] { translator._stages, translator._outputSerializer },
                null);

            return new QueryableTranslation(model, translator._resultTransformer);
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            Type serializerType;
            if (__serializerTypes.TryGetValue(type, out serializerType))
            {
                return CreateSerializer(serializerType, serializerRegistry);
            }

            if (typeInfo.IsGenericType && !typeInfo.ContainsGenericParameters)
            {
                Type serializerTypeDefinition;
                if (__serializerTypes.TryGetValue(type.GetGenericTypeDefinition(), out serializerTypeDefinition))
                {
                    return CreateGenericSerializer(serializerTypeDefinition, type.GetGenericArguments(), serializerRegistry);
                }
            }

            if (type.IsArray)
            {
                var elementType = type.GetElementType();
                switch (type.GetArrayRank())
                {
                    case 1:
                        var arraySerializerDefinition = typeof(ArraySerializer<>);
                        return CreateGenericSerializer(arraySerializerDefinition, new[] { elementType }, serializerRegistry);
                    case 2:
                        var twoDimensionalArraySerializerDefinition = typeof(TwoDimensionalArraySerializer<>);
                        return CreateGenericSerializer(twoDimensionalArraySerializerDefinition, new[] { elementType }, serializerRegistry);
                    case 3:
                        var threeDimensionalArraySerializerDefinition = typeof(ThreeDimensionalArraySerializer<>);
                        return CreateGenericSerializer(threeDimensionalArraySerializerDefinition, new[] { elementType }, serializerRegistry);
                    default:
                        var message = string.Format("No serializer found for array for rank {0}.", type.GetArrayRank());
                        throw new BsonSerializationException(message);
                }
            }

            return GetCollectionSerializer(type, serializerRegistry);
        }
Exemplo n.º 8
0
        public static QueryableTranslation Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
        {
            var translator = new QueryableTranslator(serializerRegistry);
            translator.Translate(node);

            var outputType = translator._outputSerializer.ValueType;
            var modelType = typeof(AggregateQueryableExecutionModel<>).MakeGenericType(outputType);
            var modelTypeInfo = modelType.GetTypeInfo();
            var outputSerializerInterfaceType = typeof(IBsonSerializer<>).MakeGenericType(new[] { outputType });
            var constructorParameterTypes = new Type[] { typeof(IEnumerable<BsonDocument>), outputSerializerInterfaceType };
            var constructorInfo = modelTypeInfo.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
                .Where(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(constructorParameterTypes))
                .Single();
            var constructorParameters = new object[] { translator._stages, translator._outputSerializer };
            var model = (QueryableExecutionModel)constructorInfo.Invoke(constructorParameters);

            return new QueryableTranslation(model, translator._resultTransformer);
        }
        /// <summary>
        /// Creates the serializer.
        /// </summary>
        /// <param name="serializerType">The serializer type.</param>
        /// <param name="serializerRegistry">The serializer registry.</param>
        /// <returns>
        /// A serializer.
        /// </returns>
        protected virtual IBsonSerializer CreateSerializer(Type serializerType, IBsonSerializerRegistry serializerRegistry)
        {
            var constructorInfo = serializerType.GetConstructor(new[] { typeof(IBsonSerializerRegistry) });
            if (constructorInfo != null)
            {
                return (IBsonSerializer)constructorInfo.Invoke(new object[] { serializerRegistry });
            }

            constructorInfo = serializerType.GetConstructor(new Type[0]);
            if (constructorInfo != null)
            {
                return (IBsonSerializer)constructorInfo.Invoke(new object[0]);
            }

            throw new MissingMethodException(string.Format(
                "No suitable constructor found for serializer type: '{0}'.",
                serializerType.FullName));
        }
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (type.IsGenericType && type.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            IBsonSerializer serializer;
            if (__serializers.TryGetValue(type, out serializer))
            {
                return serializer;
            }

            return null;
        }
        /// <inheritdoc/>
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (type.IsGenericType && type.ContainsGenericParameters)
            {
                var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
                throw new ArgumentException(message, "type");
            }

            if (type.IsInterface)
            {
                var serializerTypeDefinition = typeof(DiscriminatedInterfaceSerializer<>);
                return CreateGenericSerializer(serializerTypeDefinition, new[] { type }, serializerRegistry);
            }

            return null;
        }
 /// <inheritdoc />
 public override BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(BsonDocument.Parse(_json));
 }
        public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
        {
            var renderedField = _field.Render(sourceSerializer, serializerRegistry, linqProvider);

            return(new BsonDocument(renderedField.FieldName, _value));
        }
 public static Expression Rewrite(Expression node, IBsonSerializerRegistry serializerRegistry)
 {
     var rewriter = new CorrelatedGroupRewriter(serializerRegistry);
     return rewriter.Visit(node);
 }
Exemplo n.º 15
0
 /// <inheritdoc/>
 public override RenderedProjectionDefinition <TProjection> Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(new RenderedProjectionDefinition <TProjection>(
                null,
                _projectionSerializer ?? (sourceSerializer as IBsonSerializer <TProjection>) ?? serializerRegistry.GetSerializer <TProjection>()));
 }
        public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
        {
            var document = new BsonDocument();

            foreach (var projection in _projections)
            {
                var renderedProjection = projection.Render(sourceSerializer, serializerRegistry, linqProvider);

                foreach (var element in renderedProjection.Elements)
                {
                    // last one wins
                    document.Remove(element.Name);
                    document.Add(element);
                }
            }

            return(document);
        }
 public static Expression BuildAsync(Expression node, Expression provider, Expression cancellationToken, IBsonSerializerRegistry serializerRegistry)
 {
     return new QueryableExecutionBuilder(provider, cancellationToken, serializerRegistry).Visit(node);
 }
Exemplo n.º 18
0
 /// <inheritdoc />
 public override RenderedProjectionDefinition <TProjection> Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(FindProjectionTranslator.Translate <TSource, TProjection>(_expression, sourceSerializer, serializerRegistry));
 }
 /// <inheritdoc />
 IRenderedPipelineStageDefinition IPipelineStageDefinition.Render(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(Render((IBsonSerializer <TInput>)inputSerializer, serializerRegistry));
 }
 /// <inheritdoc />
 public override RenderedPipelineStageDefinition <TOutput> Render(IBsonSerializer <TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(new RenderedPipelineStageDefinition <TOutput>(
                OperatorName,
                BsonDocument.Parse(_json),
                _outputSerializer ?? (inputSerializer as IBsonSerializer <TOutput>) ?? serializerRegistry.GetSerializer <TOutput>()));
 }
 /// <summary>
 /// Creates the serializer from a serializer type definition and type arguments.
 /// </summary>
 /// <param name="serializerTypeDefinition">The serializer type definition.</param>
 /// <param name="typeArguments">The type arguments.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>
 /// A serializer.
 /// </returns>
 protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, Type[] typeArguments, IBsonSerializerRegistry serializerRegistry)
 {
     var serializerType = serializerTypeDefinition.MakeGenericType(typeArguments);
     return CreateSerializer(serializerType, serializerRegistry);
 }
 /// <summary>
 /// Renders the specified document serializer.
 /// </summary>
 /// <param name="inputSerializer">The input serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>A <see cref="RenderedPipelineStageDefinition{TOutput}" /></returns>
 public abstract RenderedPipelineStageDefinition <TOutput> Render(IBsonSerializer <TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry);
 /// <inheritdoc/>
 public abstract IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry);
 public AccumulatorBinder(IBsonSerializerRegistry serializerRegistry)
     : base(serializerRegistry)
 {
     _isPotentialAccumulatorMethod = true;
 }
 private CorrelatedGroupRewriter(IBsonSerializerRegistry serializerRegistry)
 {
     _serializerRegistry = serializerRegistry;
     _map = new Dictionary<CorrelatedAccumulatorExpression, Expression>();
 }
 /// <summary>
 /// Renders the sort to a <see cref="BsonDocument"/>.
 /// </summary>
 /// <param name="documentSerializer">The document serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>A <see cref="BsonDocument"/>.</returns>
 public abstract BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry);
 public override RenderedPipelineStageDefinition <TOutput> Render(IBsonSerializer <TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(_renderer(inputSerializer, serializerRegistry));
 }
Exemplo n.º 28
0
        public static RenderedProjectionDefinition <TResult> TranslateGroup <TKey, TDocument, TResult>(Expression <Func <TDocument, TKey> > idProjector, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var keyBinder          = new SerializationInfoBinder(serializerRegistry);
            var boundKeyExpression = BindSerializationInfo(keyBinder, idProjector, parameterSerializer);

            if (!(boundKeyExpression is ISerializationExpression))
            {
                var keySerializer = SerializerBuilder.Build(boundKeyExpression, serializerRegistry);
                boundKeyExpression = new SerializationExpression(
                    boundKeyExpression,
                    new BsonSerializationInfo(null, keySerializer, typeof(TKey)));
            }

            var idExpression = new GroupIdExpression(boundKeyExpression, ((ISerializationExpression)boundKeyExpression).SerializationInfo);

            var groupBinder = new AccumulatorBinder(serializerRegistry);

            groupBinder.RegisterMemberReplacement(typeof(IGrouping <TKey, TDocument>).GetProperty("Key"), idExpression);
            var groupSerializer      = new ArraySerializer <TDocument>(parameterSerializer);
            var boundGroupExpression = BindSerializationInfo(groupBinder, groupProjector, groupSerializer);
            var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundGroupExpression, serializerRegistry);
            var projection           = AggregateLanguageTranslator.Translate(boundGroupExpression).AsBsonDocument;

            // must have an "_id" in a group document
            if (!projection.Contains("_id"))
            {
                var idProjection = AggregateLanguageTranslator.Translate(boundKeyExpression);
                projection.InsertAt(0, new BsonElement("_id", idProjection));
            }

            return(new RenderedProjectionDefinition <TResult>(projection, projectionSerializer));
        }
Exemplo n.º 29
0
 /// <inheritdoc />
 public override RenderedProjectionDefinition <TProjection> Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
 {
     return(linqProvider.GetAdapter().TranslateExpressionToFindProjection(_expression, sourceSerializer, serializerRegistry));
 }
 public QueryableTranslator(IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
 {
     _serializerRegistry = Ensure.IsNotNull(serializerRegistry, nameof(serializerRegistry));
     _translationOptions = translationOptions; // can be null
     _stages = new List<BsonDocument>();
 }
Exemplo n.º 31
0
 /// <inheritdoc />
 public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
 {
     return(BsonDocument.Parse(_json));
 }
Exemplo n.º 32
0
        public override RenderedProjectionDefinition <TProjection> Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var document = _projection.Render(sourceSerializer, serializerRegistry);

            return(new RenderedProjectionDefinition <TProjection>(
                       document,
                       _projectionSerializer ?? (sourceSerializer as IBsonSerializer <TProjection>) ?? serializerRegistry.GetSerializer <TProjection>()));
        }
Exemplo n.º 33
0
        /// <inheritdoc />
        public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
        {
            var serializer = serializerRegistry.GetSerializer(_obj.GetType());

            return(new BsonDocumentWrapper(_obj, serializer));
        }
Exemplo n.º 34
0
 /// <summary>
 /// Renders the projection to a <see cref="RenderedProjectionDefinition{TProjection}"/>.
 /// </summary>
 /// <param name="sourceSerializer">The source serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>A <see cref="BsonDocument"/>.</returns>
 public abstract BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry);
Exemplo n.º 35
0
 /// <summary>
 /// Renders the field to a <see cref="String"/>.
 /// </summary>
 /// <param name="documentSerializer">The document serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>A <see cref="String"/>.</returns>
 public abstract RenderedFieldDefinition Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry);
        public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
        {
            var renderedField = _field.Render(sourceSerializer, serializerRegistry, linqProvider);

            IBsonSerializer <TItem> itemSerializer;

            if (renderedField.FieldSerializer != null)
            {
                var arraySerializer = renderedField.FieldSerializer as IBsonArraySerializer;
                BsonSerializationInfo itemSerializationInfo;
                if (arraySerializer == null || !arraySerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
                {
                    var message = string.Format("The serializer for field '{0}' must implement IBsonArraySerializer and provide item serialization info.", renderedField.FieldName);
                    throw new InvalidOperationException(message);
                }
                itemSerializer = (IBsonSerializer <TItem>)itemSerializationInfo.Serializer;
            }
            else
            {
                itemSerializer = serializerRegistry.GetSerializer <TItem>();
            }

            var renderedFilter = _filter.Render(itemSerializer, serializerRegistry, linqProvider);

            return(new BsonDocument(renderedField.FieldName, new BsonDocument("$elemMatch", renderedFilter)));
        }
Exemplo n.º 37
0
        /// <inheritdoc />
        public override RenderedFieldDefinition Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            string          resolvedName;
            IBsonSerializer resolvedSerializer;

            StringFieldDefinitionHelper.Resolve <TDocument>(_fieldName, documentSerializer, out resolvedName, out resolvedSerializer);

            return(new RenderedFieldDefinition(resolvedName, resolvedSerializer));
        }
Exemplo n.º 38
0
 public override RenderedProjectionDefinition <TNewResult> Render(IBsonSerializer <TResult> documentSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(AggregateProjectionTranslator.TranslateGroup <TKey, TResult, TNewResult>(_idExpression, _groupExpression, documentSerializer, serializerRegistry));
 }
 public QueryableExecutionBuilder(Expression provider, Expression cancellationToken, IBsonSerializerRegistry serializerRegistry)
 {
     _provider = provider;
     _cancellationToken = cancellationToken;
     _serializerRegistry = serializerRegistry;
 }
Exemplo n.º 40
0
        /// <inheritdoc />
        public override RenderedFieldDefinition Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var binder = new SerializationInfoBinder(serializerRegistry);
            var parameterSerializationInfo = new BsonSerializationInfo(null, documentSerializer, documentSerializer.ValueType);
            var parameterExpression        = new SerializationExpression(_expression.Parameters[0], parameterSerializationInfo);

            binder.RegisterParameterReplacement(_expression.Parameters[0], parameterExpression);
            var bound = binder.Bind(_expression.Body) as ISerializationExpression;

            if (bound == null)
            {
                var message = string.Format("Unable to determine the serialization information for {0}.", _expression);
                throw new InvalidOperationException(message);
            }

            return(new RenderedFieldDefinition(bound.SerializationInfo.ElementName, bound.SerializationInfo.Serializer));
        }
 public ProjectionBindingContext(IBsonSerializerRegistry serializerRegistry, IMethodCallBinder methodCallBinder)
 {
     _serializerRegistry = Ensure.IsNotNull(serializerRegistry, "serializerRegistry");
     _methodCallBinder = Ensure.IsNotNull(methodCallBinder, "methodCallBinder");
     _groupMap = new CorrelatedGroupMap();
 }
 /// <inheritdoc />
 public override BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(_document);
 }
 public AccumulatorBinder(CorrelatedGroupMap groupMap, IBsonSerializerRegistry serializerRegistry)
     : this(serializerRegistry)
 {
     _groupMap = groupMap;
 }
        /// <inheritdoc />
        public override BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var serializer = serializerRegistry.GetSerializer(_obj.GetType());

            return(new BsonDocumentWrapper(_obj, serializer));
        }
Exemplo n.º 45
0
 public static IBsonSerializer Build(Expression node, IBsonSerializerRegistry serializerRegistry)
 {
     var builder = new SerializerBuilder(serializerRegistry);
     return builder.Build(node);
 }
Exemplo n.º 46
0
        public static RenderedProjectionDefinition <TResult> TranslateProject <TDocument, TResult>(Expression <Func <TDocument, TResult> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var binder          = new SerializationInfoBinder(BsonSerializer.SerializerRegistry);
            var boundExpression = BindSerializationInfo(binder, projector, parameterSerializer);

            var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundExpression, serializerRegistry);
            var projection           = TranslateProject(boundExpression);

            return(new RenderedProjectionDefinition <TResult>(projection, projectionSerializer));
        }
        public static Expression Bind(Expression node, IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            // bind
            var binder = new ProjectionBinder(documentSerializer, serializerRegistry, __methodCallBinder);
            var bound = binder.Bind(node);

            // post-process
            return CorrelatedGroupRewriter.Rewrite(bound, serializerRegistry);
        }
Exemplo n.º 48
0
 /// <summary>
 /// Renders the projection to a <see cref="RenderedProjectionDefinition{TProjection}"/>.
 /// </summary>
 /// <param name="sourceSerializer">The source serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <returns>A <see cref="BsonDocument"/>.</returns>
 public virtual BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(Render(sourceSerializer, serializerRegistry, LinqProvider.V2));
 }
 public static Expression Build(Expression node, Expression provider, IBsonSerializerRegistry serializerRegistry)
 {
     return new QueryableExecutionBuilder(provider, null, serializerRegistry).Visit(node);
 }
 private QueryableExecutionModelBuilder(IBsonSerializerRegistry serializerRegistry)
 {
     _serializerRegistry = serializerRegistry;
     _stages = new List<BsonDocument>();
 }
Exemplo n.º 51
0
 /// <summary>
 /// Renders the projection to a <see cref="RenderedProjectionDefinition{TProjection}"/>.
 /// </summary>
 /// <param name="sourceSerializer">The source serializer.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 /// <param name="linqProvider">The LINQ provider.</param>
 /// <returns>A <see cref="BsonDocument"/>.</returns>
 public abstract BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider);
Exemplo n.º 52
0
        private IBsonSerializer GetCollectionSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            Type implementedGenericDictionaryInterface = null;
            Type implementedGenericEnumerableInterface = null;
            Type implementedGenericSetInterface = null;
            Type implementedDictionaryInterface = null;
            Type implementedEnumerableInterface = null;

            var implementedInterfaces = new List<Type>(type.GetInterfaces());
            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsInterface)
            {
                implementedInterfaces.Add(type);
            }

            foreach (var implementedInterface in implementedInterfaces)
            {
                var implementedInterfaceTypeInfo = implementedInterface.GetTypeInfo();
                if (implementedInterfaceTypeInfo.IsGenericType)
                {
                    var genericInterfaceDefinition = implementedInterface.GetGenericTypeDefinition();
                    if (genericInterfaceDefinition == typeof(IDictionary<,>))
                    {
                        implementedGenericDictionaryInterface = implementedInterface;
                    }
                    if (genericInterfaceDefinition == typeof(IEnumerable<>))
                    {
                        implementedGenericEnumerableInterface = implementedInterface;
                    }
                    if (genericInterfaceDefinition == typeof(ISet<>))
                    {
                        implementedGenericSetInterface = implementedInterface;
                    }
                }
                else
                {
                    if (implementedInterface == typeof(IDictionary))
                    {
                        implementedDictionaryInterface = implementedInterface;
                    }
                    if (implementedInterface == typeof(IEnumerable))
                    {
                        implementedEnumerableInterface = implementedInterface;
                    }
                }
            }

            // the order of the tests is important
            if (implementedGenericDictionaryInterface != null)
            {
                var keyType = implementedGenericDictionaryInterface.GetGenericArguments()[0];
                var valueType = implementedGenericDictionaryInterface.GetGenericArguments()[1];
                if (typeInfo.IsInterface)
                {
                    var dictionaryDefinition = typeof(Dictionary<,>);
                    var dictionaryType = dictionaryDefinition.MakeGenericType(keyType, valueType);
                    var serializerDefinition = typeof(ImpliedImplementationInterfaceSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, dictionaryType }, serializerRegistry);
                }
                else
                {
                    var serializerDefinition = typeof(DictionaryInterfaceImplementerSerializer<,,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, keyType, valueType }, serializerRegistry);
                }
            }
            else if (implementedDictionaryInterface != null)
            {
                if (typeInfo.IsInterface)
                {
                    var dictionaryType = typeof(Hashtable);
                    var serializerDefinition = typeof(ImpliedImplementationInterfaceSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, dictionaryType }, serializerRegistry);
                }
                else
                {
                    var serializerDefinition = typeof(DictionaryInterfaceImplementerSerializer<>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type }, serializerRegistry);
                }
            }
            else if (implementedGenericSetInterface != null)
            {
                var itemType = implementedGenericSetInterface.GetGenericArguments()[0];

                if (typeInfo.IsInterface)
                {
                    var hashSetDefinition = typeof(HashSet<>);
                    var hashSetType = hashSetDefinition.MakeGenericType(itemType);
                    var serializerDefinition = typeof(ImpliedImplementationInterfaceSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, hashSetType }, serializerRegistry);
                }
                else
                {
                    var serializerDefinition = typeof(EnumerableInterfaceImplementerSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, itemType }, serializerRegistry);
                }
            }
            else if (implementedGenericEnumerableInterface != null)
            {
                var itemType = implementedGenericEnumerableInterface.GetGenericArguments()[0];

                var readOnlyCollectionType = typeof(ReadOnlyCollection<>).MakeGenericType(itemType);
                if (type == readOnlyCollectionType)
                {
                    var serializerDefinition = typeof(ReadOnlyCollectionSerializer<>);
                    return CreateGenericSerializer(serializerDefinition, new[] { itemType }, serializerRegistry);
                }
                else if (readOnlyCollectionType.IsAssignableFrom(type))
                {
                    var serializerDefinition = typeof(ReadOnlyCollectionSubclassSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, itemType }, serializerRegistry);
                }
                else if (typeInfo.IsInterface)
                {
                    var listDefinition = typeof(List<>);
                    var listType = listDefinition.MakeGenericType(itemType);
                    var serializerDefinition = typeof(ImpliedImplementationInterfaceSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, listType }, serializerRegistry);
                }
                else
                {
                    var serializerDefinition = typeof(EnumerableInterfaceImplementerSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, itemType }, serializerRegistry);
                }
            }
            else if (implementedEnumerableInterface != null)
            {
                if (typeInfo.IsInterface)
                {
                    var listType = typeof(ArrayList);
                    var serializerDefinition = typeof(ImpliedImplementationInterfaceSerializer<,>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type, listType }, serializerRegistry);
                }
                else
                {
                    var serializerDefinition = typeof(EnumerableInterfaceImplementerSerializer<>);
                    return CreateGenericSerializer(serializerDefinition, new[] { type }, serializerRegistry);
                }
            }

            return null;
        }
Exemplo n.º 53
0
 public static IBsonSerializer GetSerializerForValueType(IBsonSerializer fieldSerializer, IBsonSerializerRegistry serializerRegistry, Type valueType, object value)
 {
     if (!valueType.GetTypeInfo().IsValueType&& value == null)
     {
         return(fieldSerializer);
     }
     else
     {
         return(GetSerializerForValueType(fieldSerializer, serializerRegistry, valueType, allowScalarValueForArrayField: false));
     }
 }
Exemplo n.º 54
0
 public ConvertIfPossibleSerializer(IBsonSerializer <TTo> serializer, IBsonSerializerRegistry serializerRegistry)
 {
     _serializer         = serializer;
     _serializerRegistry = serializerRegistry;
 }
Exemplo n.º 55
0
 public static IBsonSerializer GetSerializerForValueType(IBsonSerializer fieldSerializer, IBsonSerializerRegistry serializerRegistry, Type valueType)
 {
     return(GetSerializerForValueType(fieldSerializer, serializerRegistry, valueType, allowScalarValueForArrayField: false));
 }
Exemplo n.º 56
0
 public QueryableTranslator(IBsonSerializerRegistry serializerRegistry)
 {
     _serializerRegistry = Ensure.IsNotNull(serializerRegistry, nameof(serializerRegistry));
     _stages = new List<BsonDocument>();
 }
Exemplo n.º 57
0
        public static IBsonSerializer GetSerializerForValueType(IBsonSerializer fieldSerializer, IBsonSerializerRegistry serializerRegistry, Type valueType, bool allowScalarValueForArrayField)
        {
            var fieldType = fieldSerializer.ValueType;

            // these will normally be equal unless we've removed some Convert(s) that the compiler put in
            if (fieldType == valueType)
            {
                return(fieldSerializer);
            }

            // serialize numeric values without converting them
            if (fieldType.IsNumeric() && valueType.IsNumeric())
            {
                var valueSerializer = BsonSerializer.SerializerRegistry.GetSerializer(valueType);
                if (HasStringRepresentation(fieldSerializer))
                {
                    valueSerializer = WithStringRepresentation(valueSerializer);
                }
                return(valueSerializer);
            }

            var fieldTypeInfo = fieldType.GetTypeInfo();
            var fieldSerializerInterfaceType = typeof(IBsonSerializer <>).MakeGenericType(fieldType);
            var valueTypeInfo = valueType.GetTypeInfo();

            // synthesize a NullableSerializer using the field serializer
            if (valueType.IsNullable() && valueType.GetNullableUnderlyingType() == fieldType)
            {
                var nullableSerializerType        = typeof(NullableSerializer <>).MakeGenericType(fieldType);
                var nullableSerializerConstructor = nullableSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType });
                return((IBsonSerializer)nullableSerializerConstructor.Invoke(new object[] { fieldSerializer }));
            }

            // synthesize an EnumConvertingSerializer using the field serializer
            if (fieldTypeInfo.IsEnum)
            {
                if (valueType.IsConvertibleToEnum())
                {
                    var enumConvertingSerializerType        = typeof(EnumConvertingSerializer <,>).MakeGenericType(valueType, fieldType);
                    var enumConvertingSerializerConstructor = enumConvertingSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType });
                    return((IBsonSerializer)enumConvertingSerializerConstructor.Invoke(new object[] { fieldSerializer }));
                }

                if (valueType.IsNullable() && valueType.GetNullableUnderlyingType().IsConvertibleToEnum())
                {
                    var underlyingValueType = valueType.GetNullableUnderlyingType();
                    var underlyingValueSerializerInterfaceType = typeof(IBsonSerializer <>).MakeGenericType(underlyingValueType);
                    var enumConvertingSerializerType           = typeof(EnumConvertingSerializer <,>).MakeGenericType(underlyingValueType, fieldType);
                    var enumConvertingSerializerConstructor    = enumConvertingSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType });
                    var enumConvertingSerializer      = enumConvertingSerializerConstructor.Invoke(new object[] { fieldSerializer });
                    var nullableSerializerType        = typeof(NullableSerializer <>).MakeGenericType(underlyingValueType);
                    var nullableSerializerConstructor = nullableSerializerType.GetTypeInfo().GetConstructor(new[] { underlyingValueSerializerInterfaceType });
                    return((IBsonSerializer)nullableSerializerConstructor.Invoke(new object[] { enumConvertingSerializer }));
                }
            }

            // synthesize a NullableEnumConvertingSerializer using the field serializer
            if (fieldType.IsNullableEnum() && valueType.IsNullable())
            {
                var nonNullableFieldType       = fieldType.GetNullableUnderlyingType();
                var nonNullableValueType       = valueType.GetNullableUnderlyingType();
                var nonNullableFieldSerializer = ((IChildSerializerConfigurable)fieldSerializer).ChildSerializer;
                var nonNullableFieldSerializerInterfaceType     = typeof(IBsonSerializer <>).MakeGenericType(nonNullableFieldType);
                var nullableEnumConvertingSerializerType        = typeof(NullableEnumConvertingSerializer <,>).MakeGenericType(nonNullableValueType, nonNullableFieldType);
                var nullableEnumConvertingSerializerConstructor = nullableEnumConvertingSerializerType.GetTypeInfo().GetConstructor(new[] { nonNullableFieldSerializerInterfaceType });
                return((IBsonSerializer)nullableEnumConvertingSerializerConstructor.Invoke(new object[] { nonNullableFieldSerializer }));
            }

            // synthesize an IEnumerableSerializer serializer using the item serializer from the field serializer
            Type fieldIEnumerableInterfaceType;
            Type valueIEnumerableInterfaceType;
            Type itemType;

            if (
                (fieldIEnumerableInterfaceType = fieldType.FindIEnumerable()) != null &&
                (valueIEnumerableInterfaceType = valueType.FindIEnumerable()) != null &&
                (itemType = fieldIEnumerableInterfaceType.GetSequenceElementType()) == valueIEnumerableInterfaceType.GetSequenceElementType() &&
                fieldSerializer is IChildSerializerConfigurable)
            {
                var itemSerializer = ((IChildSerializerConfigurable)fieldSerializer).ChildSerializer;
                var itemSerializerInterfaceType      = typeof(IBsonSerializer <>).MakeGenericType(itemType);
                var ienumerableSerializerType        = typeof(IEnumerableSerializer <>).MakeGenericType(itemType);
                var ienumerableSerializerConstructor = ienumerableSerializerType.GetTypeInfo().GetConstructor(new[] { itemSerializerInterfaceType });
                return((IBsonSerializer)ienumerableSerializerConstructor.Invoke(new object[] { itemSerializer }));
            }

            if (allowScalarValueForArrayField)
            {
                // if the fieldSerializer is an array serializer try to adapt its itemSerializer for valueType
                IBsonArraySerializer arraySerializer;
                if ((arraySerializer = fieldSerializer as IBsonArraySerializer) != null)
                {
                    BsonSerializationInfo itemSerializationInfo;
                    if (arraySerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
                    {
                        var itemSerializer = itemSerializationInfo.Serializer;
                        return(GetSerializerForValueType(itemSerializer, serializerRegistry, valueType, allowScalarValueForArrayField: false));
                    }
                }
            }

            // if we can't return a value serializer based on the field serializer return a converting serializer
            var convertIfPossibleSerializerType        = typeof(ConvertIfPossibleSerializer <,>).MakeGenericType(valueType, fieldType);
            var convertIfPossibleSerializerConstructor = convertIfPossibleSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType, typeof(IBsonSerializerRegistry) });

            return((IBsonSerializer)convertIfPossibleSerializerConstructor.Invoke(new object[] { fieldSerializer, serializerRegistry }));
        }
Exemplo n.º 58
0
 private SerializerBuilder(IBsonSerializerRegistry serializerRegistry)
 {
     _serializerRegistry = serializerRegistry;
 }
 private ProjectionBinder(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry, IMethodCallBinder methodCallBinder)
 {
     _methodCallBinder = Ensure.IsNotNull(methodCallBinder, "methodCallBinder");
     _documentSerializer = Ensure.IsNotNull(documentSerializer, "documentSerializer");
     _context = new ProjectionBindingContext(
         Ensure.IsNotNull(serializerRegistry, "serializerRegistry"),
         _methodCallBinder);
 }