Ejemplo n.º 1
0
        /// <summary>
        ///     Maps the persisted entity back to the <see cref="IEntityEvent" />.
        /// </summary>
        /// <param name="eventEntity">
        ///     The stored event entity to be mapped.
        /// </param>
        /// <returns>
        ///     The <see cref="IEntityEvent" />.
        /// </returns>
        protected virtual IEntityEvent MapEvent(TEventEntity eventEntity)
        {
            Check.NotNull(eventEntity, nameof(eventEntity));

            IEntityEvent?entityEvent;

            if (eventEntity.ClrType != null)
            {
                var eventType = TypesCache.GetType(eventEntity.ClrType);
                entityEvent = (IEntityEvent?)JsonSerializer.Deserialize(
                    eventEntity.SerializedEvent,
                    eventType);
            }
            else
            {
                entityEvent =
                    (IEntityEvent?)PolymorphicJsonSerializer.Deserialize(eventEntity.SerializedEvent);
            }

            if (entityEvent == null)
            {
                throw new InvalidOperationException("Failed to deserialize the persisted event.");
            }

            entityEvent.Sequence  = eventEntity.Sequence;
            entityEvent.Timestamp = eventEntity.Timestamp;

            return(entityEvent);
        }
Ejemplo n.º 2
0
        public void GetType_IncompleteTypeName_TypeReturned()
        {
            var typeName = "Silverback.Tests.Core.TestTypes.Messages.TestEventOne, Silverback.Core.Tests";

            var type = TypesCache.GetType(typeName);

            type.Should().Be(typeof(TestEventOne));
        }
Ejemplo n.º 3
0
        public void GetType_NonExistingTypeWithNoThrow_NullReturned()
        {
            var typeName = "Baaaad.Event, Silverback.Core.Tests";

            var type = TypesCache.GetType(typeName, false);

            type.Should().BeNull();
        }
Ejemplo n.º 4
0
        public void GetType_NonExistingType_ExceptionThrown()
        {
            var typeName = "Baaaad.Event, Silverback.Core.Tests";

            Action act = () => TypesCache.GetType(typeName);

            act.Should().Throw <TypeLoadException>();
        }
Ejemplo n.º 5
0
        public void GetType_ExistingType_TypeReturned()
        {
            var typeName = typeof(TestEventOne).AssemblyQualifiedName !;

            var type = TypesCache.GetType(typeName);

            type.Should().Be(typeof(TestEventOne));
        }
Ejemplo n.º 6
0
        private static Type?GetMessageType(OutboxMessage outboxMessage)
        {
            if (outboxMessage.MessageType == null)
            {
                return(null);
            }

            return(TypesCache.GetType(outboxMessage.MessageType));
        }
Ejemplo n.º 7
0
        private static IBrokerMessageOffset InstantiateOffset(string clrType, string key, string value)
        {
            var offsetType = TypesCache.GetType(clrType);
            var offset     = (IBrokerMessageOffset)Activator.CreateInstance(
                offsetType,
                key,
                value);

            return(offset);
        }
Ejemplo n.º 8
0
        public void GetType_WrongAssemblyVersion_TypeReturned()
        {
            var typeName =
                "Silverback.Tests.Core.TestTypes.Messages.TestEventOne, " +
                "Silverback.Core.Tests, Version=123.123.123.123";

            var type = TypesCache.GetType(typeName);

            type.AssemblyQualifiedName.Should().Be(typeof(TestEventOne).AssemblyQualifiedName);
        }
Ejemplo n.º 9
0
        private static IComparableOffset InstantiateOffset(string clrType, string key, string value)
        {
            var offsetType = TypesCache.GetType(clrType) !;
            var offset     = (IComparableOffset)Activator.CreateInstance(
                offsetType,
                key,
                value);

            return(offset);
        }
Ejemplo n.º 10
0
        public static Type?GetTypeFromHeaders(MessageHeaderCollection messageHeaders, bool throwOnError = true)
        {
            var typeName = messageHeaders.GetValue(DefaultMessageHeaders.MessageType);

            if (string.IsNullOrEmpty(typeName))
            {
                return(null);
            }

            return(TypesCache.GetType(typeName, throwOnError));
        }
Ejemplo n.º 11
0
        public static object Deserialize(string json, JsonSerializerOptions?options = null)
        {
            var typeInformation = JsonSerializer.Deserialize <TypeInformation>(json, options);

            if (string.IsNullOrEmpty(typeInformation?.TypeName))
            {
                throw new JsonException($"No {TypePropertyName} property found in JSON.");
            }

            var type = TypesCache.GetType(typeInformation.TypeName);

            return(JsonSerializer.Deserialize(json, type, options));
        }
Ejemplo n.º 12
0
        public static Type?GetTypeFromHeaders(MessageHeaderCollection messageHeaders, bool throwOnError = true)
        {
            var typeName = messageHeaders.GetValue(DefaultMessageHeaders.MessageType);

            if (string.IsNullOrEmpty(typeName))
            {
                if (throwOnError)
                {
                    throw new MessageSerializerException("Missing type header.");
                }

                return(null);
            }

            return(TypesCache.GetType(typeName, throwOnError));
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Maps the persisted entity back to the <see cref="IEntityEvent" />.
        /// </summary>
        /// <param name="eventEntity">
        ///     The stored event entity to be mapped.
        /// </param>
        /// <returns>
        ///     The <see cref="IEntityEvent" />.
        /// </returns>
        protected virtual IEntityEvent MapEvent(TEventEntity eventEntity)
        {
            Check.NotNull(eventEntity, nameof(eventEntity));

            IEntityEvent entityEvent;

            if (eventEntity.ClrType != null)
            {
                var eventType = TypesCache.GetType(eventEntity.ClrType);
                entityEvent = (IEntityEvent)JsonSerializer.Deserialize(eventEntity.SerializedEvent, eventType);
            }
            else
            {
                entityEvent = (IEntityEvent)PolymorphicJsonSerializer.Deserialize(eventEntity.SerializedEvent);
            }

            entityEvent.Sequence  = eventEntity.Sequence;
            entityEvent.Timestamp = eventEntity.Timestamp;

            return(entityEvent);
        }