Browse Source

Merge pull request #16544 from abpframework/LocalEventHandlerOrderAttribute

Add `LocalEventHandlerOrderAttribute`.
pull/16551/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
8789e62fb6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      framework/src/Volo.Abp.EventBus.Abstractions/Volo/Abp/EventBus/Local/LocalEventHandlerOrderAttribute.cs
  2. 14
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
  3. 56
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Order_Test.cs

17
framework/src/Volo.Abp.EventBus.Abstractions/Volo/Abp/EventBus/Local/LocalEventHandlerOrderAttribute.cs

@ -0,0 +1,17 @@
using System;
namespace Volo.Abp.EventBus.Local;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class LocalEventHandlerOrderAttribute : Attribute
{
/// <summary>
/// Handlers execute in ascending numeric value of the Order property.
/// </summary>
public int Order { get; set; }
public LocalEventHandlerOrderAttribute(int order)
{
Order = order;
}
}

14
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs

@ -10,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Reflection;
using Volo.Abp.Threading;
using Volo.Abp.Uow;
@ -138,14 +139,19 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
protected override IEnumerable<EventTypeWithEventHandlerFactories> GetHandlerFactories(Type eventType)
{
var handlerFactoryList = new List<EventTypeWithEventHandlerFactories>();
var handlerFactoryList = new List<Tuple<IEventHandlerFactory, Type, int>>();
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
foreach (var factory in handlerFactory.Value)
{
handlerFactoryList.Add(new Tuple<IEventHandlerFactory, Type, int>(
factory,
handlerFactory.Key,
ReflectionHelper.GetAttributesOfMemberOrDeclaringType<LocalEventHandlerOrderAttribute>(factory.GetHandler().EventHandler.GetType()).FirstOrDefault()?.Order ?? 0));
}
}
return handlerFactoryList.ToArray();
return handlerFactoryList.OrderBy(x => x.Item3).Select(x => new EventTypeWithEventHandlerFactories(x.Item2, new List<IEventHandlerFactory> {x.Item1})).ToArray();
}
private List<IEventHandlerFactory> GetOrCreateHandlerFactories(Type eventType)

56
framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Order_Test.cs

@ -0,0 +1,56 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Events;
using Xunit;
namespace Volo.Abp.EventBus.Local;
public class EventBus_Order_Test : EventBusTestBase
{
public static string HandlerExecuteOrder { get; set; }
[Fact]
public async Task Handler_Should_Execute_By_Order()
{
HandlerExecuteOrder = "";
await LocalEventBus.PublishAsync(new MyOrderEventHandlerEventData());
HandlerExecuteOrder.ShouldBe("321");
}
public class MyOrderEventHandlerEventData
{
}
public class MyOrderEventHandler : ILocalEventHandler<MyOrderEventHandlerEventData>, ITransientDependency
{
public Task HandleEventAsync(MyOrderEventHandlerEventData eventData)
{
HandlerExecuteOrder += "1";
return Task.CompletedTask;
}
}
[LocalEventHandlerOrder(-2)]
public class MyOrderEventHandler2 : ILocalEventHandler<MyOrderEventHandlerEventData>, ITransientDependency
{
public Task HandleEventAsync(MyOrderEventHandlerEventData eventData)
{
HandlerExecuteOrder += "2";
return Task.CompletedTask;
}
}
[LocalEventHandlerOrder(-3)]
public class MyOrderEventHandler3 : ILocalEventHandler<MyOrderEventHandlerEventData>, ITransientDependency
{
public Task HandleEventAsync(MyOrderEventHandlerEventData eventData)
{
HandlerExecuteOrder += "3";
return Task.CompletedTask;
}
}
}
Loading…
Cancel
Save