/// <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 GetMethod_should_return_null_when_method_not_found() { var sut = new TypesCache(); var result = sut.GetMethod(typeof(TypesCacheTests), "foo"); result.Should().BeNull(); }
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 IComparableOffset InstantiateOffset(string clrType, string key, string value) { var offsetType = TypesCache.GetType(clrType) !; var offset = (IComparableOffset)Activator.CreateInstance( offsetType, key, value); return(offset); }
public void GetGeneric_should_return_typed_generic_from_raw_type() { var baseType = typeof(IEnumerable <>); var expectedType = typeof(IEnumerable <int>); var sut = new TypesCache(); var result = sut.GetGeneric(baseType, typeof(int)); result.Should().Be(expectedType); }
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); }
public static IEnumerable <PropertyAttributeHolder <T> > GetPropertiesWithAttributes <T>(object obj) where T : Attribute { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(GetPropertiesWithAttributes <T>(TypesCache.TypeFor(obj))); }
public void GetMethod_should_return_method_when_name_valid() { var type = typeof(TypesCacheTests); var methodName = nameof(TypesCacheTests.GetMethod_should_return_method_when_name_valid); var expectedMethod = type.GetMethod(methodName); var sut = new TypesCache(); var result = sut.GetMethod(type, methodName); result.Should().BeSameAs(expectedMethod); }
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 IEnumerable <PropertyAttributeHolder <T> > GetPropertiesWithAttributes <T>(Type type) where T : Attribute { if (type == null) { throw new ArgumentNullException(nameof(type)); } var properties = TypesCache.PropertiesFor(type); // TODO cache return(from propertyInfo in properties let attr = propertyInfo.GetCustomAttribute <T>() where attr != null select new PropertyAttributeHolder <T>(propertyInfo, attr)); }
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); }
/// <summary> /// Tries the automatic registration. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> /// <exception cref="TooManyImplementationsException">Thrown when more than one implementation is available for the same type</exception> private static object TryAutoRegistration(Type type) { var types = TypesCache .Where(t => !t.IsAbstract && type.IsAssignableFrom(t) && t.GetConstructors().Any(c => !c.GetParameters().Any())) .ToList(); if (types.Count == 0) { return(null); } if (types.Count > 1) { throw new TooManyImplementationsException(type); } var instance = GetInstance(types.Single()); Registrations.AddOrUpdate(type, () => instance, (key, existingVal) => () => instance); return(instance); }
/// <summary> /// Get all instances for a contract /// </summary> /// <param name="contract"></param> /// <returns></returns> private static IEnumerable <object> GetAllInstances(Type contract) { if (!Instances.ContainsKey(contract)) { lock (Locks.GetOrAdd(contract.FullName ?? contract.Name, new object())) { if (!Instances.ContainsKey(contract)) { Instances.TryAdd(contract, TypesCache.Where(t => contract.IsAssignableFrom(t) && !t.IsAbstract).ToList()); } } } if (!Instances.TryGetValue(contract, out var types)) { yield break; } foreach (var type in types) { yield return(GetInstance(type)); } }
public static ConstructorInfo[] FastGetConstructors(this Type type) => TypesCache.Get(type).ConstructorInfos;
public static PropertyInfo[] FastGetProperties(this Type type) => TypesCache.Get(type).PropertyInfos;
public static FieldInfo[] FastGetFields(this Type type) => TypesCache.Get(type).FieldInfos;
public static MethodInfo[] FastGetMethods(this Type type) => TypesCache.Get(type).MethodInfos;
public static Attribute[] FastGetCustomAttributes(this Type type) => TypesCache.Get(type).Attributes;
public static CustomAttributeData[] FastGetCustomAttributeData(this Type type) => TypesCache.Get(type).CustomAttributeData;