/// <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); }
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)); }
public void GetType_NonExistingTypeWithNoThrow_NullReturned() { var typeName = "Baaaad.Event, Silverback.Core.Tests"; var type = TypesCache.GetType(typeName, false); type.Should().BeNull(); }
public void GetType_NonExistingType_ExceptionThrown() { var typeName = "Baaaad.Event, Silverback.Core.Tests"; Action act = () => TypesCache.GetType(typeName); act.Should().Throw <TypeLoadException>(); }
public void GetType_ExistingType_TypeReturned() { var typeName = typeof(TestEventOne).AssemblyQualifiedName !; var type = TypesCache.GetType(typeName); type.Should().Be(typeof(TestEventOne)); }
private static Type?GetMessageType(OutboxMessage outboxMessage) { if (outboxMessage.MessageType == null) { return(null); } return(TypesCache.GetType(outboxMessage.MessageType)); }
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); }
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); }
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); }
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)); }
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)); }
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)); }
/// <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); }