From 7783cd0126962ac0b7ba51c519c562919f831c22 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 15 Nov 2018 16:16:49 +0300 Subject: [PATCH] Introduce GenericEventNameAttribute. --- .../RabbitMq/RabbitMqDistributedEventBus.cs | 6 ++--- .../{EventName.cs => EventNameAttribute.cs} | 18 ++++++------- .../Abp/EventBus/GenericEventNameAttribute.cs | 24 +++++++++++++++++ .../Volo/Abp/EventBus/IEventNameProvider.cs | 9 +++++++ .../GenericEventNameAttribute_Tests.cs | 26 +++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) rename framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/{EventName.cs => EventNameAttribute.cs} (65%) create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs diff --git a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs index 053b41dc5b..9f56304959 100644 --- a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs @@ -132,7 +132,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq if (handlerFactories.Count == 1) //TODO: Multi-threading! { - var eventName = EventNameAttribute.GetName(eventType); + var eventName = EventNameAttribute.GetNameOrDefault(eventType); using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event! { @@ -203,7 +203,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq public override Task PublishAsync(Type eventType, object eventData) { - var eventName = EventNameAttribute.GetName(eventType); + var eventName = EventNameAttribute.GetNameOrDefault(eventType); var body = Serializer.Serialize(eventData); using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event! @@ -232,7 +232,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq eventType, type => { - var eventName = EventNameAttribute.GetName(type); + var eventName = EventNameAttribute.GetNameOrDefault(type); EventTypes[eventName] = type; return new List(); } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventNameAttribute.cs similarity index 65% rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventNameAttribute.cs index 3d634c76f5..7dfc0a434e 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventNameAttribute.cs @@ -7,19 +7,19 @@ namespace Volo.Abp.EventBus [AttributeUsage(AttributeTargets.Class)] public class EventNameAttribute : Attribute, IEventNameProvider { - public string Name { get; } + public virtual string Name { get; } public EventNameAttribute([NotNull] string name) { Name = Check.NotNullOrWhiteSpace(name, nameof(name)); } - public static string GetName() + public static string GetNameOrDefault() { - return GetName(typeof(TEvent)); + return GetNameOrDefault(typeof(TEvent)); } - public static string GetName([NotNull] Type eventType) + public static string GetNameOrDefault([NotNull] Type eventType) { Check.NotNull(eventType, nameof(eventType)); @@ -27,13 +27,13 @@ namespace Volo.Abp.EventBus .GetCustomAttributes(true) .OfType() .FirstOrDefault() - ?.Name + ?.GetName(eventType) ?? eventType.FullName; } - } - public interface IEventNameProvider - { - string Name { get; } + public string GetName(Type eventType) + { + return Name; + } } } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs new file mode 100644 index 0000000000..638a47af0a --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/GenericEventNameAttribute.cs @@ -0,0 +1,24 @@ +using System; + +namespace Volo.Abp.EventBus +{ + [AttributeUsage(AttributeTargets.Class)] + public class GenericEventNameAttribute : Attribute, IEventNameProvider + { + public string GetName(Type eventType) + { + if (!eventType.IsGenericType) + { + throw new AbpException($"Given type is not generic: {eventType.AssemblyQualifiedName}"); + } + + var genericArguments = eventType.GetGenericArguments(); + if (genericArguments.Length > 1) + { + throw new AbpException($"Given type has more than one generic argument: {eventType.AssemblyQualifiedName}"); + } + + return EventNameAttribute.GetNameOrDefault(genericArguments[0]); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs new file mode 100644 index 0000000000..25d95c8329 --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventNameProvider.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.Abp.EventBus +{ + public interface IEventNameProvider + { + string GetName(Type eventType); + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs new file mode 100644 index 0000000000..640801ad58 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericEventNameAttribute_Tests.cs @@ -0,0 +1,26 @@ +using Shouldly; +using Xunit; + +namespace Volo.Abp.EventBus +{ + public class GenericEventNameAttribute_Tests + { + [Fact] + public void Should_Properly_Get_EventName() + { + new GenericEventNameAttribute() + .GetName(typeof(MyGenericType)) + .ShouldBe(typeof(MyInnerType).FullName); + } + + public class MyInnerType + { + + } + + public class MyGenericType + { + + } + } +}