Пример #1
0
        public static Snapshot ToSnapshot(this BsonDocument doc, IDocumentSerializer serializer)
        {
            if (doc == null)
            {
                return(null);
            }

            BsonDocument id             = doc[MongoFields.Id].AsBsonDocument;
            string       bucketId       = id[MongoFields.BucketId].AsString;
            string       streamId       = id[MongoFields.StreamId].AsString;
            int          streamRevision = id[MongoFields.StreamRevision].AsInt32;
            BsonValue    bsonPayload    = doc[MongoFields.Payload];

            object payload;

            switch (bsonPayload.BsonType)
            {
            case BsonType.Binary:
                payload = serializer.Deserialize <object>(bsonPayload.AsByteArray);
                break;

            case BsonType.Document:
                payload = BsonSerializer.Deserialize <object>(bsonPayload.AsBsonDocument);
                break;

            default:
                payload = BsonTypeMapper.MapToDotNetValue(bsonPayload);
                break;
            }

            return(new Snapshot(bucketId, streamId, streamRevision, payload));
        }
Пример #2
0
        public static Commit ToCommit(this BsonDocument doc, IDocumentSerializer serializer)
        {
            if (doc == null)
            {
                return(null);
            }

            BsonDocument id             = doc[MongoFields.Id].AsBsonDocument;
            string       bucketId       = id[MongoFields.BucketId].AsString;
            string       streamId       = id[MongoFields.StreamId].AsString;
            int          commitSequence = id[MongoFields.CommitSequence].AsInt32;

            List <EventMessage> events = doc[MongoFields.Events]
                                         .AsBsonArray
                                         .Select(e =>
                                                 e.AsBsonDocument[MongoFields.Payload].IsBsonDocument
                        ? BsonSerializer.Deserialize <EventMessage>(e.AsBsonDocument[MongoFields.Payload].AsBsonDocument)
                        : serializer.Deserialize <EventMessage>(e.AsBsonDocument[MongoFields.Payload].AsByteArray))
                                         .ToList();
            int streamRevision = doc[MongoFields.Events].AsBsonArray.Last().AsBsonDocument[MongoFields.StreamRevision].AsInt32;

            return(new Commit(
                       bucketId,
                       streamId,
                       streamRevision,
                       doc[MongoFields.CommitId].AsGuid,
                       commitSequence,
                       doc[MongoFields.CommitStamp].ToUniversalTime(),
                       doc[MongoFields.Headers].AsDictionary <string, object>(),
                       events));
        }
        // private methods
        private IBsonSerializer GetCollectionSerializer(Type type)
        {
            Type implementedGenericDictionaryInterface = null;
            Type implementedGenericEnumerableInterface = null;
            Type implementedDictionaryInterface        = null;
            Type implementedEnumerableInterface        = null;

            var implementedInterfaces = new List <Type>(type.GetInterfaces());

            if (type.IsInterface)
            {
                implementedInterfaces.Add(type);
            }
            foreach (var implementedInterface in implementedInterfaces)
            {
                if (implementedInterface.IsGenericType)
                {
                    var genericInterfaceDefinition = implementedInterface.GetGenericTypeDefinition();
                    if (genericInterfaceDefinition == typeof(IDictionary <,>))
                    {
                        implementedGenericDictionaryInterface = implementedInterface;
                    }
                    if (genericInterfaceDefinition == typeof(IEnumerable <>))
                    {
                        implementedGenericEnumerableInterface = 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];
                var genericSerializerDefinition = typeof(DictionarySerializer <,>);
                var genericSerializerType       = genericSerializerDefinition.MakeGenericType(keyType, valueType);
                return((IBsonSerializer)Activator.CreateInstance(genericSerializerType));
            }
            else if (implementedDictionaryInterface != null)
            {
                return(DictionarySerializer.Instance);
            }
            else if (implementedGenericEnumerableInterface != null)
            {
                var  valueType = implementedGenericEnumerableInterface.GetGenericArguments()[0];
                var  readOnlyCollectionType = typeof(ReadOnlyCollection <>).MakeGenericType(valueType);
                Type genericSerializerDefinition;
                if (readOnlyCollectionType.IsAssignableFrom(type))
                {
                    genericSerializerDefinition = typeof(ReadOnlyCollectionSerializer <>);
                    if (type != readOnlyCollectionType)
                    {
                        BsonSerializer.RegisterDiscriminator(type, type.Name);
                    }
                }
                else
                {
                    genericSerializerDefinition = typeof(EnumerableSerializer <>);
                }
                var genericSerializerType = genericSerializerDefinition.MakeGenericType(valueType);
                return((IBsonSerializer)Activator.CreateInstance(genericSerializerType));
            }
            else if (implementedEnumerableInterface != null)
            {
                return(EnumerableSerializer.Instance);
            }

            return(null);
        }