diff --git a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs b/samples/apis/Backet.Api/Domain/AggregatesModels/BacketAggregate/Backet.cs similarity index 100% rename from samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs rename to samples/apis/Backet.Api/Domain/AggregatesModels/BacketAggregate/Backet.cs diff --git a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs b/samples/apis/Backet.Api/Domain/AggregatesModels/BacketAggregate/BacketItem.cs similarity index 100% rename from samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs rename to samples/apis/Backet.Api/Domain/AggregatesModels/BacketAggregate/BacketItem.cs diff --git a/samples/apis/Backet.Api/Domain/Event/BacketCreatedEvent.cs b/samples/apis/Backet.Api/Domain/Events/BacketCreatedEvent.cs similarity index 84% rename from samples/apis/Backet.Api/Domain/Event/BacketCreatedEvent.cs rename to samples/apis/Backet.Api/Domain/Events/BacketCreatedEvent.cs index 6ec6345..c667e3b 100644 --- a/samples/apis/Backet.Api/Domain/Event/BacketCreatedEvent.cs +++ b/samples/apis/Backet.Api/Domain/Events/BacketCreatedEvent.cs @@ -2,11 +2,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading.Tasks; namespace Backet.Api.Domain.Event { - public class BacketCreatedEvent:IEvent + [EventInfo(EventName = "Backet")] + public class BacketCreatedEvent : IEvent { public string BacketId { get; set; } } diff --git a/src/Pole.Core/EventBus/Event/EventAttribute.cs b/src/Pole.Core/EventBus/Event/EventInfoAttribute.cs similarity index 90% rename from src/Pole.Core/EventBus/Event/EventAttribute.cs rename to src/Pole.Core/EventBus/Event/EventInfoAttribute.cs index 62e5063..b5e08a3 100644 --- a/src/Pole.Core/EventBus/Event/EventAttribute.cs +++ b/src/Pole.Core/EventBus/Event/EventInfoAttribute.cs @@ -5,7 +5,7 @@ using System.Text; namespace Pole.Core.EventBus.Event { [AttributeUsage(AttributeTargets.Class)] - public class EventAttribute: Attribute + public class EventInfoAttribute: Attribute { public string EventName { get; set; } } diff --git a/src/Pole.Core/EventBus/ObserverUnitContainer.cs b/src/Pole.Core/EventBus/ObserverUnitContainer.cs index b78771f..2af2997 100644 --- a/src/Pole.Core/EventBus/ObserverUnitContainer.cs +++ b/src/Pole.Core/EventBus/ObserverUnitContainer.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Pole.Core.EventBus.EventHandler; using System.Linq; using Pole.Core.Exceptions; +using Pole.Core.EventBus.Event; namespace Pole.Core.EventBus { @@ -16,17 +17,28 @@ namespace Pole.Core.EventBus readonly ConcurrentDictionary> unitDict = new ConcurrentDictionary>(); public ObserverUnitContainer(IServiceProvider serviceProvider) { - var eventHandlerList = new List<(Type, EventHandlerAttribute)>(); + var eventHandlerList = new List<(Type, EventInfoAttribute)>(); foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService>())) { foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract && !typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m))) { - var attribute = type.GetCustomAttributes(typeof(EventHandlerAttribute), false).FirstOrDefault(); var eventHandlerInterface = type.GetInterfaces().FirstOrDefault(type => typeof(IPoleEventHandler).IsAssignableFrom(type) && !type.IsGenericType); + var basePoleEventHandlerInterface= eventHandlerInterface.GetInterfaces().FirstOrDefault(m=>m.IsGenericType); + + if (basePoleEventHandlerInterface == null) + { + throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler"); + } + var eventType= basePoleEventHandlerInterface.GetGenericArguments().FirstOrDefault(); + if (eventType == null) + { + throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler"); + } + var attribute = eventType.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault(); if (attribute != null) { - eventHandlerList.Add((eventHandlerInterface, (EventHandlerAttribute)attribute)); + eventHandlerList.Add((eventHandlerInterface, (EventInfoAttribute)attribute)); } else { diff --git a/src/Pole.Core/Serialization/EventTypeFinder.cs b/src/Pole.Core/Serialization/EventTypeFinder.cs index a5d71f0..079ee66 100644 --- a/src/Pole.Core/Serialization/EventTypeFinder.cs +++ b/src/Pole.Core/Serialization/EventTypeFinder.cs @@ -25,8 +25,8 @@ namespace Pole.Core.Serialization foreach (var type in assembly.GetTypes().Where(m => baseEventType.IsAssignableFrom(m)&&!m.IsAbstract)) { var eventCode = type.FullName; - var eventAttribute = type.GetCustomAttributes(typeof(EventAttribute),false).FirstOrDefault(); - if (eventAttribute is EventAttribute attribute ) + var eventAttribute = type.GetCustomAttributes(typeof(EventInfoAttribute),false).FirstOrDefault(); + if (eventAttribute is EventInfoAttribute attribute ) { eventCode = attribute.EventName; } diff --git a/src/Pole.EventBus.Rabbitmq/Core/EventBusContainer.cs b/src/Pole.EventBus.Rabbitmq/Core/EventBusContainer.cs index 5c4f72c..63ce567 100644 --- a/src/Pole.EventBus.Rabbitmq/Core/EventBusContainer.cs +++ b/src/Pole.EventBus.Rabbitmq/Core/EventBusContainer.cs @@ -38,8 +38,8 @@ namespace Pole.EventBus.RabbitMQ } public async Task AutoRegister() { - var eventList = new List<(Type type, EventAttribute config)>(); - var evenHandlertList = new List<(Type type, EventHandlerAttribute config)>(); + var eventList = new List<(Type type, EventInfoAttribute config)>(); + var evenHandlertList = new List<(Type type, EventInfoAttribute config)>(); AddEventAndEventHandlerInfoList(eventList, evenHandlertList); foreach (var (type, config) in eventList) { @@ -110,21 +110,21 @@ namespace Pole.EventBus.RabbitMQ #region helpers - private void AddEventAndEventHandlerInfoList(List<(Type type, EventAttribute config)> eventList, List<(Type type, EventHandlerAttribute config)> eventHandlertList) + private void AddEventAndEventHandlerInfoList(List<(Type type, EventInfoAttribute config)> eventList, List<(Type type, EventInfoAttribute config)> eventHandlertList) { foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService>())) { foreach (var type in assembly.GetTypes().Where(m => typeof(IEvent).IsAssignableFrom(m) && m.IsClass)) { - var attribute = type.GetCustomAttributes(typeof(EventAttribute), false).FirstOrDefault(); + var attribute = type.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault(); if (attribute != null) { - eventList.Add((type, (EventAttribute)attribute)); + eventList.Add((type, (EventInfoAttribute)attribute)); } else { - eventList.Add((type, new EventAttribute() { EventName = type.FullName })); + eventList.Add((type, new EventInfoAttribute() { EventName = type.FullName })); } } } @@ -132,13 +132,25 @@ namespace Pole.EventBus.RabbitMQ foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService>())) { - foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract&&!typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m))) + foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract && !typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m))) { - var attribute = type.GetCustomAttributes(typeof(EventHandlerAttribute), false).FirstOrDefault(); + var eventHandlerInterface = type.GetInterfaces().FirstOrDefault(type => typeof(IPoleEventHandler).IsAssignableFrom(type) && !type.IsGenericType); + var basePoleEventHandlerInterface = eventHandlerInterface.GetInterfaces().FirstOrDefault(m => m.IsGenericType); + + if (basePoleEventHandlerInterface == null) + { + throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler"); + } + var eventType = basePoleEventHandlerInterface.GetGenericArguments().FirstOrDefault(); + if (eventType == null) + { + throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler"); + } + var attribute = eventType.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault(); if (attribute != null) { - eventHandlertList.Add((type, (EventHandlerAttribute)attribute)); + eventHandlertList.Add((eventHandlerInterface, (EventInfoAttribute)attribute)); } else {