add IntegrationEventNameAttribute
This commit is contained in:
		
							parent
							
								
									313879e52b
								
							
						
					
					
						commit
						08e28f24e9
					
				@ -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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -156,7 +156,16 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public string GetEventKey<T>()
 | 
					        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
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user