add IntegrationEventNameAttribute

This commit is contained in:
Musa Demir 2020-09-01 06:16:13 +03:00
parent 313879e52b
commit 08e28f24e9
3 changed files with 46 additions and 1 deletions

View File

@ -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;
}
}
}

View File

@ -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
}
}
}

View File

@ -156,7 +156,16 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
public string GetEventKey<T>()
{
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
}
}
}