Exemplo n.º 1
0
        /// <inheritdoc />
        public override RenderedCommand <TResult> Render(IBsonSerializerRegistry serializerRegistry)
        {
            var serializer = serializerRegistry.GetSerializer(_obj.GetType());

            return(new RenderedCommand <TResult>(
                       new BsonDocumentWrapper(_obj, serializer),
                       _resultSerializer ?? serializerRegistry.GetSerializer <TResult>()));
        }
Exemplo n.º 2
0
        public ValueTupleSerializer(IBsonSerializerRegistry serializerRegistry)
        {
            if (serializerRegistry == null)
            {
                throw new ArgumentNullException("serializerRegistry");
            }

            _lazyItem1Serializer = new Lazy <IBsonSerializer <T1> >(() => serializerRegistry.GetSerializer <T1>());
            _lazyItem2Serializer = new Lazy <IBsonSerializer <T2> >(() => serializerRegistry.GetSerializer <T2>());
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyValuePairSerializer{TKey, TValue}" /> class.
 /// </summary>
 /// <param name="representation">The representation.</param>
 /// <param name="serializerRegistry">The serializer registry.</param>
 public KeyValuePairSerializer(BsonType representation, IBsonSerializerRegistry serializerRegistry)
     : this(
         representation,
         new Lazy <IBsonSerializer <TKey> >(() => serializerRegistry.GetSerializer <TKey>()),
         new Lazy <IBsonSerializer <TValue> >(() => serializerRegistry.GetSerializer <TValue>()))
 {
     if (serializerRegistry == null)
     {
         throw new ArgumentNullException("serializerRegistry");
     }
 }
        public static Task <BsonDocument> ExplainAggregation <TDocument, TResult>(
            this IMongoCollection <TDocument> collection
            , PipelineDefinition <TDocument, TResult> aggregationPipeline, ExplainVerbosity verbosity)
        {
            IBsonSerializerRegistry     serializerRegistry = collection.Settings.SerializerRegistry;
            IBsonSerializer <TDocument> serializer         = serializerRegistry.GetSerializer <TDocument>();
            MessageEncoderSettings      encoderSettings    = collection.GetMessageEncoderSettings();

            RenderedPipelineDefinition <TResult> renderedDefinition = aggregationPipeline.Render(serializer, serializerRegistry);

            IReadOperation <BsonDocument> explainOperation = new AggregateOperation <TResult>(
                collection.CollectionNamespace,
                renderedDefinition.Documents,
                renderedDefinition.OutputSerializer,
                encoderSettings)
                                                             .ToExplainOperation(verbosity);

            ICluster           cluster = GetCluster(collection);
            ICoreSessionHandle session = NoCoreSession.NewHandle();

            using (IReadBinding binding = new ReadPreferenceBinding(cluster, collection.Settings.ReadPreference, session))
            {
                var cancelToken = new CancellationToken();
                return(explainOperation.ExecuteAsync(binding, cancelToken));
            }
        }
Exemplo n.º 5
0
        public IBsonSerializer GetSerializer(Type type, Expression node)
        {
            Ensure.IsNotNull(type, nameof(type));

            IBsonSerializer serializer;

            if (node != null && PreviouslyUsedSerializerFinder.TryFindSerializer(node, type, out serializer))
            {
                return(serializer);
            }
            else if (node == null || type != node.Type)
            {
                serializer = _serializerRegistry.GetSerializer(type);
                if (node != null)
                {
                    var childConfigurableSerializer = serializer as IChildSerializerConfigurable;
                    if (childConfigurableSerializer != null)
                    {
                        var nodeSerializer         = GetSerializer(node.Type, node);
                        var deepestChildSerializer = SerializerHelper.GetDeepestChildSerializer(childConfigurableSerializer);

                        if (nodeSerializer.ValueType == deepestChildSerializer.ValueType)
                        {
                            serializer = SerializerHelper.RecursiveConfigureChildSerializer(childConfigurableSerializer, nodeSerializer);
                        }
                    }
                }
            }
            else
            {
                serializer = SerializerBuilder.Build(node, _serializerRegistry);
            }

            return(serializer);
        }
Exemplo n.º 6
0
        private IBsonSerializer <TField> GetValueSerializerForDistinct <TField>(RenderedFieldDefinition <TField> renderedField, IBsonSerializerRegistry serializerRegistry)
        {
            if (renderedField.UnderlyingSerializer != null)
            {
                if (renderedField.UnderlyingSerializer.ValueType == typeof(TField))
                {
                    return((IBsonSerializer <TField>)renderedField.UnderlyingSerializer);
                }

                var arraySerializer = renderedField.UnderlyingSerializer as IBsonArraySerializer;
                if (arraySerializer != null)
                {
                    BsonSerializationInfo itemSerializationInfo;
                    if (arraySerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
                    {
                        if (itemSerializationInfo.Serializer.ValueType == typeof(TField))
                        {
                            return((IBsonSerializer <TField>)itemSerializationInfo.Serializer);
                        }
                    }
                }
            }

            return(serializerRegistry.GetSerializer <TField>());
        }
Exemplo n.º 7
0
        public override BsonDocument Render(IBsonSerializer <TSource> sourceSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var renderedField = _field.Render(sourceSerializer, serializerRegistry);

            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);

            return(new BsonDocument(renderedField.FieldName, new BsonDocument("$elemMatch", renderedFilter)));
        }
        protected override Expression VisitUnary(UnaryExpression node)
        {
            var newNode         = base.VisitUnary(node);
            var unaryExpression = newNode as UnaryExpression;

            if (node != newNode &&
                unaryExpression != null &&
                (newNode.NodeType == ExpressionType.Convert || newNode.NodeType == ExpressionType.ConvertChecked))
            {
                var serializationExpression = unaryExpression.Operand as ISerializationExpression;
                if (serializationExpression != null)
                {
                    BsonSerializationInfo serializationInfo;
                    var operandType = unaryExpression.Operand.Type;
                    if (!unaryExpression.Operand.Type.IsEnum &&
                        !operandType.IsNullableEnum() &&
                        !unaryExpression.Type.IsAssignableFrom(unaryExpression.Operand.Type))
                    {
                        // only lookup a new serializer if the cast is "unnecessary"
                        var serializer = _serializerRegistry.GetSerializer(node.Type);
                        serializationInfo = new BsonSerializationInfo(serializationExpression.SerializationInfo.ElementName, serializer, node.Type);
                    }
                    else
                    {
                        serializationInfo = serializationExpression.SerializationInfo;
                    }
                    return(new SerializationExpression(unaryExpression, serializationInfo));
                }
            }

            return(newNode);
        }
Exemplo n.º 9
0
        protected override Expression VisitUnary(UnaryExpression node)
        {
            var newNode         = base.VisitUnary(node);
            var unaryExpression = newNode as UnaryExpression;

            if (node != newNode &&
                unaryExpression != null &&
                !unaryExpression.Operand.Type.IsEnum && // enums are weird, so we skip them
                (newNode.NodeType == ExpressionType.Convert || newNode.NodeType == ExpressionType.ConvertChecked))
            {
                if (unaryExpression.Operand.Type.IsGenericType && unaryExpression.Operand.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    var underlyingType = Nullable.GetUnderlyingType(node.Operand.Type);
                    if (underlyingType.IsEnum)
                    {
                        // we skip enums because they are weird
                        return(newNode);
                    }
                }

                var serializationExpression = unaryExpression.Operand as ISerializationExpression;
                if (serializationExpression != null)
                {
                    var serializer        = _serializerRegistry.GetSerializer(node.Type);
                    var serializationInfo = new BsonSerializationInfo(serializationExpression.SerializationInfo.ElementName, serializer, node.Type);
                    return(new SerializationExpression(unaryExpression, serializationInfo));
                }
            }

            return(newNode);
        }
Exemplo n.º 10
0
 public MongoRepositoryBase(IOptions <MongoConnection> setting)
 {
     mongoConnection = setting.Value;
     Connect(mongoConnection);
     _serializerRegistry = BsonSerializer.SerializerRegistry;
     _documentSerializer = _serializerRegistry.GetSerializer <TEntity>();
 }
 /// <summary>
 /// 根據查詢表達式刪除一筆或多筆紀錄
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="self"></param>
 /// <param name="exp"></param>
 /// <returns></returns>
 public static async ETTask DeleteJson <T>(this DBProxyComponent self, Expression <Func <T, bool> > exp) where T : ComponentWithId
 {
     ExpressionFilterDefinition <T> filter             = new ExpressionFilterDefinition <T>(exp);
     IBsonSerializerRegistry        serializerRegistry = BsonSerializer.SerializerRegistry;
     IBsonSerializer <T>            documentSerializer = serializerRegistry.GetSerializer <T>();
     string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
     await self.DeleteJson <T>(json);
 }
Exemplo n.º 12
0
        //Expression表达式转换时monongb语句
        private static string ExpressionConversionJson <T>(Expression <Func <T, bool> > exp)
        {
            ExpressionFilterDefinition <T> filter             = new ExpressionFilterDefinition <T>(exp);
            IBsonSerializerRegistry        serializerRegistry = BsonSerializer.SerializerRegistry;
            IBsonSerializer <T>            documentSerializer = serializerRegistry.GetSerializer <T>();

            return(filter.Render(documentSerializer, serializerRegistry).ToJson());
        }
Exemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ImpliedImplementationInterfaceSerializer{TInterface, TImplementation}" /> class.
 /// </summary>
 /// <param name="serializerRegistry">The serializer registry.</param>
 public ImpliedImplementationInterfaceSerializer(IBsonSerializerRegistry serializerRegistry)
     : this(new Lazy <IBsonSerializer <TImplementation> >(() => serializerRegistry.GetSerializer <TImplementation>()))
 {
     if (serializerRegistry == null)
     {
         throw new ArgumentNullException("serializerRegistry");
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ThreeDimensionalArraySerializer{TItem}" /> class.
        /// </summary>
        /// <param name="serializerRegistry">The serializer registry.</param>
        public ThreeDimensionalArraySerializer(IBsonSerializerRegistry serializerRegistry)
        {
            if (serializerRegistry == null)
            {
                throw new ArgumentNullException("serializerRegistry");
            }

            _lazyItemSerializer = new Lazy <IBsonSerializer <TItem> >(() => serializerRegistry.GetSerializer <TItem>());
        }
Exemplo n.º 15
0
 /// <summary>
 /// 根据表达式删除
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="self"></param>
 /// <param name="exp"></param>
 /// <returns></returns>
 public static async ETTask DeleteAll <T>(this DBProxyComponent self, Expression <Func <T, bool> > exp)
 {
     DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();
     ExpressionFilterDefinition <T> filter             = new ExpressionFilterDefinition <T>(exp);
     IBsonSerializerRegistry        serializerRegistry = BsonSerializer.SerializerRegistry;
     IBsonSerializer <T>            documentSerializer = serializerRegistry.GetSerializer <T>();
     string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
     await dbComponent.GetCollection(typeof(T).Name).FindOneAndDeleteAsync(json);
 }
        /// <summary>
        /// 根據查詢表達式查詢,並約束結果範圍,再取回相應筆數
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="exp"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public static async ETTask <List <ComponentWithId> > Query <T>(this DBProxyComponent self, Expression <Func <T, bool> > exp, long skip, long limit) where T : ComponentWithId
        {
            ExpressionFilterDefinition <T> filter             = new ExpressionFilterDefinition <T>(exp);
            IBsonSerializerRegistry        serializerRegistry = BsonSerializer.SerializerRegistry;
            IBsonSerializer <T>            documentSerializer = serializerRegistry.GetSerializer <T>();
            string json = filter.Render(documentSerializer, serializerRegistry).ToJson();

            return(await self.Query <T>(json, skip, limit));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue}" /> class.
        /// </summary>
        /// <param name="serializerRegistry">The serializer registry.</param>
        protected EnumerableSerializerBase(IBsonSerializerRegistry serializerRegistry)
        {
            if (serializerRegistry == null)
            {
                throw new ArgumentNullException("serializerRegistry");
            }

            _lazyItemSerializer = new Lazy <IBsonSerializer>(() => serializerRegistry.GetSerializer(typeof(object)));
        }
Exemplo n.º 18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NullableSerializer{T}" /> class.
        /// </summary>
        /// <param name="serializerRegistry">The serializer registry.</param>
        public NullableSerializer(IBsonSerializerRegistry serializerRegistry)
        {
            if (serializerRegistry == null)
            {
                throw new ArgumentNullException("serializerRegistry");
            }

            _lazySerializer = new Lazy <IBsonSerializer <T> >(() => serializerRegistry.GetSerializer <T>());
        }
        public static List <BsonDocument> GetAggregateStagesBson <TDocument, TResult>(
            this IMongoCollection <TDocument> collection, IAggregateFluent <TResult> aggregation)
        {
            IBsonSerializerRegistry     serializerRegistry = collection.Settings.SerializerRegistry;
            IBsonSerializer <TDocument> serializer         = serializerRegistry.GetSerializer <TDocument>();

            var pipeline           = new PipelineStagePipelineDefinition <TDocument, TResult>(aggregation.Stages);
            var renderedDefinition = pipeline.Render(serializer, serializerRegistry);

            return(renderedDefinition.Documents.ToList());
        }
Exemplo n.º 20
0
        public IBsonSerializer Build(Expression node)
        {
            if (node is ISerializationExpression)
            {
                return(((ISerializationExpression)node).Serializer);
            }

            IBsonSerializer serializer = null;

            switch (node.NodeType)
            {
            case ExpressionType.MemberInit:
                serializer = BuildMemberInit((MemberInitExpression)node);
                break;

            case ExpressionType.New:
                if (!typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(node.Type))
                {
                    serializer = BuildNew((NewExpression)node);
                }
                break;
            }

            if (serializer == null && !PreviouslyUsedSerializerFinder.TryFindSerializer(node, node.Type, out serializer))
            {
                serializer = _serializerRegistry.GetSerializer(node.Type);
                var childConfigurable = serializer as IChildSerializerConfigurable;
                if (childConfigurable != null)
                {
                    var arraySerializer = serializer as IBsonArraySerializer;
                    BsonSerializationInfo itemSerializationInfo;
                    if (arraySerializer != null && arraySerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
                    {
                        IBsonSerializer itemSerializer;
                        if (PreviouslyUsedSerializerFinder.TryFindSerializer(node, itemSerializationInfo.Serializer.ValueType, out itemSerializer))
                        {
                            serializer = RecursiveConfigureChildSerializer(childConfigurable, itemSerializer);
                        }
                    }
                    else
                    {
                        IBsonSerializer childSerializer;
                        if (PreviouslyUsedSerializerFinder.TryFindSerializer(node, childConfigurable.ChildSerializer.ValueType, out childSerializer))
                        {
                            serializer = childConfigurable.WithChildSerializer(childSerializer);
                        }
                    }
                }
            }

            return(serializer);
        }
Exemplo n.º 21
0
        public static BsonDocument FilterToBsonDocument <TDocument>(IBsonSerializerRegistry registry, object filter)
        {
            var filterExpr = filter as Expression <Func <TDocument, bool> >;

            if (filterExpr != null)
            {
                var helper = new BsonSerializationInfoHelper();
                helper.RegisterExpressionSerializer(filterExpr.Parameters[0], registry.GetSerializer <TDocument>());
                return(new QueryBuilder <TDocument>(helper).Where(filterExpr).ToBsonDocument());
            }

            return(ToBsonDocument(registry, filter));
        }
Exemplo n.º 22
0
            public override BsonDocument Render(
                IBsonSerializer documentSerializer,
                IBsonSerializerRegistry serializerRegistry)
            {
                if (documentSerializer is IBsonSerializer <TDocument> typedSerializer)
                {
                    return(_sort.Render(typedSerializer, serializerRegistry));
                }

                return(_sort.Render(
                           serializerRegistry.GetSerializer <TDocument>(),
                           serializerRegistry));
            }
Exemplo n.º 23
0
            public IBsonSerializer Build(Expression node)
            {
                if (node is IBsonSerializationInfoExpression)
                {
                    return(((IBsonSerializationInfoExpression)node).SerializationInfo.Serializer);
                }

                if (node.NodeType == ExpressionType.New)
                {
                    return(BuildNew((NewExpression)node));
                }

                return(_serializerRegistry.GetSerializer(node.Type));
            }
Exemplo n.º 24
0
            public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value)
            {
                TTo convertedValue;

                if (TryConvertValue(value, out convertedValue))
                {
                    _serializer.Serialize(context, args, convertedValue);
                }
                else
                {
                    var serializer = _serializerRegistry.GetSerializer <TFrom>();
                    serializer.Serialize(context, args, value);
                }
            }
        public IBsonSerializer GetSerializer(Type type, Expression node)
        {
            Ensure.IsNotNull(type, nameof(type));

            IBsonSerializer serializer;

            if (node != null && PreviouslyUsedSerializerFinder.TryFindSerializer(node, type, out serializer))
            {
                return(serializer);
            }
            else if (node == null || type != node.Type)
            {
                return(_serializerRegistry.GetSerializer(type));
            }

            return(SerializerBuilder.Build(node, _serializerRegistry));
        }
Exemplo n.º 26
0
            public IBsonSerializer Build(Expression node)
            {
                if (node is ISerializationExpression)
                {
                    return(((ISerializationExpression)node).SerializationInfo.Serializer);
                }

                switch (node.NodeType)
                {
                case ExpressionType.MemberInit:
                    return(BuildMemberInit((MemberInitExpression)node));

                case ExpressionType.New:
                    return(BuildNew((NewExpression)node));

                default:
                    return(_serializerRegistry.GetSerializer(node.Type));
                }
            }
Exemplo n.º 27
0
        public static BsonDocument ToBsonDocument(IBsonSerializerRegistry registry, object document)
        {
            if (document == null)
            {
                return(null);
            }

            var bsonDocument = document as BsonDocument;

            if (bsonDocument != null)
            {
                return(bsonDocument);
            }

            if (document is string)
            {
                return(BsonDocument.Parse((string)document));
            }

            var serializer = registry.GetSerializer(document.GetType());

            return(new BsonDocumentWrapper(document, serializer));
        }
Exemplo n.º 28
0
 /// <inheritdoc />
 public override RenderedPipelineStageDefinition <TOutput> Render(IBsonSerializer <TInput> inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
 {
     return(new RenderedPipelineStageDefinition <TOutput>(
                OperatorName,
                BsonDocument.Parse(_json),
                _outputSerializer ?? (inputSerializer as IBsonSerializer <TOutput>) ?? serializerRegistry.GetSerializer <TOutput>()));
 }
Exemplo n.º 29
0
        /// <inheritdoc />
        public override BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var serializer = serializerRegistry.GetSerializer(_obj.GetType());

            return(new BsonDocumentWrapper(_obj, serializer));
        }
Exemplo n.º 30
0
 /// <inheritdoc />
 public override RenderedCommand <TResult> Render(IBsonSerializerRegistry serializerRegistry)
 {
     return(new RenderedCommand <TResult>(
                BsonDocument.Parse(_json),
                _resultSerializer ?? serializerRegistry.GetSerializer <TResult>()));
 }