private void SerializeDiscriminatedValue(BsonSerializationContext context, BsonSerializationArgs args, object value, Type actualType)
        {
            var serializer = BsonSerializer.LookupSerializer(actualType);

            var polymorphicSerializer = serializer as IBsonPolymorphicSerializer;

            if (polymorphicSerializer != null && polymorphicSerializer.IsDiscriminatorCompatibleWithObjectSerializer)
            {
                serializer.Serialize(context, args, value);
            }
            else
            {
                if (context.IsDynamicType != null && context.IsDynamicType(value.GetType()))
                {
                    args.NominalType = actualType;
                    serializer.Serialize(context, args, value);
                }
                else
                {
                    var bsonWriter    = context.Writer;
                    var discriminator = _discriminatorConvention.GetDiscriminator(typeof(object), actualType);

                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteName(_discriminatorConvention.ElementName);
                    BsonValueSerializer.Instance.Serialize(context, discriminator);
                    bsonWriter.WriteName("_v");
                    serializer.Serialize(context, value);
                    bsonWriter.WriteEndDocument();
                }
            }
        }
Пример #2
0
        public async Task <Snapshot> Load <TSnap>(
            string invariantId,
            string causationId
            )
        {
            if (null != causationId)
            {
                if (await _events
                    .AsQueryable()
                    .Where(e => e.InvariantType == typeof(TSnap).Name)
                    .Where(e => e.InvariantId == invariantId)
                    .AnyAsync(e => e.CausationId == causationId))
                {
                    throw new IdempotencyException(causationId);
                }
            }

            var snapshot = await _snapshots.AsQueryable()
                           .Where(d => d.Snap is TSnap)
                           .Where(d => d.Identity == invariantId)
                           .FirstOrDefaultAsync();

            return(snapshot ?? BsonSerializer.Deserialize <Snapshot>(new BsonDocument {
                { "Sequence", -1 },
                { "Identity", invariantId },
                { "Snap", new BsonDocument {
                      { "_t", _discriminatorConvention.GetDiscriminator(typeof(object), typeof(TSnap)) }
                  } }
            }));
        }
 public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         if (nominalType.IsGenericType && nominalType.GetGenericTypeDefinition() == typeof(Nullable <>))
         {
             nominalType = nominalType.GetGenericArguments()[0];
         }
         this.VerifyNominalType(nominalType);
         // Added
         Type type = value == null ? nominalType : ProxyManager.GetUnderlyingType(value);
         // Added
         BsonClassMap bsonClassMap = BsonClassMap.LookupClassMap(type);
         bsonWriter.WriteStartDocument();
         DocumentSerializationOptions serializationOptions = options == null ? DocumentSerializationOptions.Defaults : (DocumentSerializationOptions)options;
         BsonMemberMap memberMap = (BsonMemberMap)null;
         if (serializationOptions.SerializeIdFirst)
         {
             memberMap = bsonClassMap.IdMemberMap;
             if (memberMap != null)
             {
                 this.SerializeMember(bsonWriter, value, memberMap);
             }
         }
         if ((type != nominalType || bsonClassMap.DiscriminatorIsRequired || bsonClassMap.HasRootClass) && !bsonClassMap.IsAnonymous)
         {
             IDiscriminatorConvention discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
             BsonValue discriminator = discriminatorConvention.GetDiscriminator(nominalType, type);
             if (discriminator != (BsonValue)null)
             {
                 bsonWriter.WriteName(discriminatorConvention.ElementName);
                 discriminator.WriteTo(bsonWriter);
             }
         }
         foreach (BsonMemberMap bsonMemberMap in bsonClassMap.MemberMaps)
         {
             if (bsonMemberMap != memberMap)
             {
                 if (bsonMemberMap == bsonClassMap.ExtraElementsMemberMap)
                 {
                     this.SerializeExtraElements(bsonWriter, value, bsonMemberMap);
                 }
                 else
                 {
                     this.SerializeMember(bsonWriter, value, bsonMemberMap);
                 }
             }
         }
         bsonWriter.WriteEndDocument();
     }
 }
Пример #4
0
        private void SerializeDiscriminator(BsonSerializationContext context, Type nominalType, object obj)
        {
            IDiscriminatorConvention discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(_classMap.ClassType);
            BsonValue discriminator = discriminatorConvention?.GetDiscriminator(nominalType, obj.GetType());

            if (discriminator != null)
            {
                context.Writer.WriteName(discriminatorConvention.ElementName);
                BsonValueSerializer.Instance.Serialize(context, discriminator);
            }
        }
Пример #5
0
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="value">The value.</param>
        public override void Serialize(BsonSerializationContext context, TValue value)
        {
            var bsonWriter    = context.Writer;
            var nominalType   = context.NominalType;
            var actualType    = value.GetType();
            var discriminator = _discriminatorConvention.GetDiscriminator(nominalType, actualType);

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName(_discriminatorConvention.ElementName);
            context.SerializeWithChildContext(BsonValueSerializer.Instance, discriminator);
            bsonWriter.WriteName("_v");
            _wrappedSerializer.Serialize(context.CreateChild(actualType), value);
            bsonWriter.WriteEndDocument();
        }
Пример #6
0
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The value.</param>
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
        {
            var bsonWriter    = context.Writer;
            var nominalType   = args.NominalType;
            var actualType    = value.GetType();
            var discriminator = _discriminatorConvention.GetDiscriminator(nominalType, actualType);

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName(_discriminatorConvention.ElementName);
            BsonValueSerializer.Instance.Serialize(context, discriminator);
            bsonWriter.WriteName("_v");
            args.NominalType = actualType;
            _wrappedSerializer.Serialize(context, args, value);
            bsonWriter.WriteEndDocument();
        }
Пример #7
0
 /// <summary>
 /// If the type is not registered, attach it to our discriminator
 /// </summary>
 /// <param name="actualType">the type to examine</param>
 protected void RegisterNewTypesToDiscriminator(Type actualType)
 {
     // we've detected a new concrete type that isn't registered in MongoDB's serializer
     if (actualType != typeof(object) && !actualType.GetTypeInfo().IsInterface&& !BsonSerializer.IsTypeDiscriminated(actualType))
     {
         try
         {
             BsonSerializer.RegisterDiscriminatorConvention(actualType, DiscriminatorConvention);
             BsonSerializer.RegisterDiscriminator(actualType, DiscriminatorConvention.GetDiscriminator(typeof(object), actualType));
         }
         catch (BsonSerializationException)
         {
             // the MongoDB driver library has no nice mechanism for checking if a discriminator convention is registerd.
             // The "Lookup" logic tends to define a default if it doesn't exist.
             // So we're forced to eat the "duplicate registration" exception.
         }
     }
 }