Add `LocalEventHandlerOrderAttribute`.

Resolve #16361
pull/16544/head
maliming 3 years ago
parent 499b0783d3
commit 0cb497371c
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

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

@ -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;
@ -140,9 +141,21 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
{
var handlerFactoryList = new List<EventTypeWithEventHandlerFactories>();
var handlerWithTypes = 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)
{
handlerWithTypes.Add(new Tuple<IEventHandlerFactory, Type, int>(
factory,
handlerFactory.Key,
ReflectionHelper.GetAttributesOfMemberOrDeclaringType<LocalEventHandlerOrderAttribute>(factory.GetHandler().EventHandler.GetType()).FirstOrDefault()?.Order ?? 0));
}
}
foreach (var handlerWithType in handlerWithTypes.OrderBy(x => x.Item3))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerWithType.Item2, new List<IEventHandlerFactory>{ handlerWithType.Item1 }));
}
return handlerFactoryList.ToArray();

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