Remove duplicated async event handlers.

pull/279/head
Halil İbrahim Kalkan 8 years ago
parent c3d59e76b0
commit fbee774eba

@ -8,7 +8,7 @@ namespace Volo.Abp.EventBus
/// This event handler is an adapter to be able to use an action as <see cref="IAsyncEventHandler{TEventData}"/> implementation.
/// </summary>
/// <typeparam name="TEvent">Event type</typeparam>
internal class AsyncActionEventHandler<TEvent> :
internal class ActionEventHandler<TEvent> :
IAsyncEventHandler<TEvent>,
ITransientDependency
{
@ -18,10 +18,10 @@ namespace Volo.Abp.EventBus
public Func<TEvent, Task> Action { get; }
/// <summary>
/// Creates a new instance of <see cref="AsyncActionEventHandler{TEventData}"/>.
/// Creates a new instance of <see cref="ActionEventHandler{TEvent}"/>.
/// </summary>
/// <param name="handler">Action to handle the event</param>
public AsyncActionEventHandler(Func<TEvent, Task> handler)
public ActionEventHandler(Func<TEvent, Task> handler)
{
Action = handler;
}

@ -66,7 +66,7 @@ namespace Volo.Abp.EventBus
/// <inheritdoc/>
public IDisposable Register<TEvent>(Func<TEvent, Task> action) where TEvent : class
{
return Register(typeof(TEvent), new AsyncActionEventHandler<TEvent>(action));
return Register(typeof(TEvent), new ActionEventHandler<TEvent>(action));
}
/// <inheritdoc/>
@ -121,7 +121,7 @@ namespace Volo.Abp.EventBus
return false;
}
var actionHandler = singleInstanceFactory.HandlerInstance as AsyncActionEventHandler<TEvent>;
var actionHandler = singleInstanceFactory.HandlerInstance as ActionEventHandler<TEvent>;
if (actionHandler == null)
{
return false;

@ -15,7 +15,6 @@ namespace Volo.Abp.EventBus
await EventBus.TriggerAsync(new MySimpleEventData(4));
GetRequiredService<MySimpleEventDataHandler>().TotalData.ShouldBe(10);
GetRequiredService<MySimpleAsyncEventDataHandler>().TotalData.ShouldBe(10);
}
}
}

@ -16,18 +16,10 @@ namespace Volo.Abp.EventBus
EventBus.Register<EntityChangedEventData<MyEntity>>(handler);
EventBus.Register<EntityCreatedEventData<MyEntity>>(handler);
var asyncHandler = new MyAsyncEventHandler();
EventBus.Register<EntityChangedEventData<MyEntity>>(asyncHandler);
EventBus.Register<EntityCreatedEventData<MyEntity>>(asyncHandler);
await EventBus.TriggerAsync(new EntityCreatedEventData<MyEntity>(new MyEntity()));
handler.EntityCreatedEventCount.ShouldBe(1);
handler.EntityChangedEventCount.ShouldBe(1);
asyncHandler.EntityCreatedEventCount.ShouldBe(1);
asyncHandler.EntityChangedEventCount.ShouldBe(1);
}
public class MyEntity : Entity
@ -54,25 +46,5 @@ namespace Volo.Abp.EventBus
return Task.CompletedTask;
}
}
public class MyAsyncEventHandler :
IAsyncEventHandler<EntityChangedEventData<MyEntity>>,
IAsyncEventHandler<EntityCreatedEventData<MyEntity>>
{
public int EntityChangedEventCount { get; set; }
public int EntityCreatedEventCount { get; set; }
public Task HandleEventAsync(EntityChangedEventData<MyEntity> eventData)
{
EntityChangedEventCount++;
return Task.FromResult(0);
}
public Task HandleEventAsync(EntityCreatedEventData<MyEntity> eventData)
{
EntityCreatedEventCount++;
return Task.FromResult(0);
}
}
}
}

@ -1,16 +0,0 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EventBus
{
public class MySimpleAsyncEventDataHandler : IAsyncEventHandler<MySimpleEventData>, ISingletonDependency
{
public int TotalData { get; private set; }
public Task HandleEventAsync(MySimpleEventData eventData)
{
TotalData += eventData.Value;
return Task.CompletedTask;
}
}
}

@ -1,23 +0,0 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.EventBus
{
public class MySimpleTransientAsyncEventHandler : IAsyncEventHandler<MySimpleEventData>, IDisposable
{
public static int HandleCount { get; set; }
public static int DisposeCount { get; set; }
public Task HandleEventAsync(MySimpleEventData eventData)
{
++HandleCount;
return Task.FromResult(0);
}
public void Dispose()
{
++DisposeCount;
}
}
}

@ -9,7 +9,6 @@ namespace Volo.Abp.EventBus
public async Task Should_Call_Handler_AndDispose()
{
EventBus.Register<MySimpleEventData, MySimpleTransientEventHandler>();
EventBus.Register<MySimpleEventData, MySimpleTransientAsyncEventHandler>();
await EventBus.TriggerAsync(new MySimpleEventData(1));
await EventBus.TriggerAsync(new MySimpleEventData(2));
@ -17,9 +16,6 @@ namespace Volo.Abp.EventBus
Assert.Equal(3, MySimpleTransientEventHandler.HandleCount);
Assert.Equal(3, MySimpleTransientEventHandler.DisposeCount);
Assert.Equal(3, MySimpleTransientAsyncEventHandler.HandleCount);
Assert.Equal(3, MySimpleTransientAsyncEventHandler.DisposeCount);
}
}
}
Loading…
Cancel
Save