diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEventNameAttribute.cs b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEventNameAttribute.cs new file mode 100644 index 000000000..f485f1b81 --- /dev/null +++ b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEventNameAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class IntegrationEventNameAttribute : Attribute + { + public string EventName { get; private set; } + + public IntegrationEventNameAttribute(string eventName) + { + EventName = eventName; + } + } +} \ No newline at end of file diff --git a/src/BuildingBlocks/EventBus/EventBus/Extensions/IntegrationEventNameExtensions.cs b/src/BuildingBlocks/EventBus/EventBus/Extensions/IntegrationEventNameExtensions.cs new file mode 100644 index 000000000..cf6a01f17 --- /dev/null +++ b/src/BuildingBlocks/EventBus/EventBus/Extensions/IntegrationEventNameExtensions.cs @@ -0,0 +1,21 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions +{ + public static class IntegrationEventNameExtensions + { + public static string GetEventName(this IntegrationEvent @event) + { + var type = @event.GetType(); + var attributes = type.GetCustomAttributes(false); + foreach (var attr in attributes) + { + //if class contains IntegrationEventNameAttribute take event name from attribute + if (attr is IntegrationEventNameAttribute eventInfoAttribute) + return eventInfoAttribute.EventName; + } + + return type.Name; //otherwise event name is class name + } + } +} \ No newline at end of file diff --git a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs index ea1aca61e..ce982b8e9 100644 --- a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs @@ -156,7 +156,16 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus public string GetEventKey() { - return typeof(T).Name; + var type = typeof(T); + var attributes = type.GetCustomAttributes(false); + foreach (var attr in attributes) + { + //if class contains IntegrationEventNameAttribute take event name from attribute + if (attr is IntegrationEventNameAttribute eventInfoAttribute) + return eventInfoAttribute.EventName; + } + + return type.Name; //otherwise event name is class name } } }