mirror of https://github.com/abpframework/abp.git
29 changed files with 604 additions and 474 deletions
@ -1,65 +1,7 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public interface IDistributedEventBus |
|||
public interface IDistributedEventBus : IEventBus |
|||
{ |
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// Given action is called for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="action">Action to handle events</param>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
IDisposable Subscribe<TEvent>(Func<TEvent, Task> action) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// A new instance of <see cref="THandler"/> object is created for every event occurrence.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <typeparam name="THandler">Type of the event handler</typeparam>
|
|||
IDisposable Subscribe<TEvent, THandler>() |
|||
where TEvent : class |
|||
where THandler : IEventHandler, new(); |
|||
|
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Subscribe(Type eventType, IEventHandler handler); |
|||
|
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// Given factory is used to create/release handlers
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Subscribe<TEvent>(IEventHandlerFactory factory) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Subscribes to an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Subscribe(Type eventType, IEventHandlerFactory factory); |
|||
|
|||
Task PublishAsync<TEvent>(TEvent eventData) |
|||
where TEvent : class; |
|||
|
|||
Task PublishAsync(Type eventType, object eventData); |
|||
|
|||
} |
|||
} |
|||
|
|||
@ -1,58 +1,95 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.EventBus.Local; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
[Dependency(TryRegister = true)] |
|||
[ExposeServices(typeof(IDistributedEventBus), typeof(LocalDistributedEventBus))] |
|||
public class LocalDistributedEventBus : IDistributedEventBus, ITransientDependency |
|||
{ |
|||
private readonly IEventBus _eventBus; |
|||
private readonly ILocalEventBus _localEventBus; |
|||
|
|||
public LocalDistributedEventBus(IEventBus eventBus) |
|||
public LocalDistributedEventBus(ILocalEventBus localEventBus) |
|||
{ |
|||
_eventBus = eventBus; |
|||
_localEventBus = localEventBus; |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
return _eventBus.Register(action); |
|||
return _localEventBus.Subscribe(action); |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
return _eventBus.Register(handler); |
|||
return _localEventBus.Subscribe(handler); |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent, THandler>() where TEvent : class where THandler : IEventHandler, new() |
|||
{ |
|||
return _eventBus.Register<TEvent, THandler>(); |
|||
return _localEventBus.Subscribe<TEvent, THandler>(); |
|||
} |
|||
|
|||
public IDisposable Subscribe(Type eventType, IEventHandler handler) |
|||
{ |
|||
return _eventBus.Register(eventType, handler); |
|||
return _localEventBus.Subscribe(eventType, handler); |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
return _eventBus.Register<TEvent>(factory); |
|||
return _localEventBus.Subscribe<TEvent>(factory); |
|||
} |
|||
|
|||
public IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
return _eventBus.Register(eventType, factory); |
|||
return _localEventBus.Subscribe(eventType, factory); |
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
_localEventBus.Unsubscribe(action); |
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
_localEventBus.Unsubscribe(handler); |
|||
} |
|||
|
|||
public void Unsubscribe(Type eventType, IEventHandler handler) |
|||
{ |
|||
_localEventBus.Unsubscribe(eventType, handler); |
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
_localEventBus.Unsubscribe<TEvent>(factory); |
|||
} |
|||
|
|||
public void Unsubscribe(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
_localEventBus.Unsubscribe(eventType, factory); |
|||
} |
|||
|
|||
public void UnsubscribeAll<TEvent>() where TEvent : class |
|||
{ |
|||
_localEventBus.UnsubscribeAll<TEvent>(); |
|||
} |
|||
|
|||
public void UnsubscribeAll(Type eventType) |
|||
{ |
|||
_localEventBus.UnsubscribeAll(eventType); |
|||
} |
|||
|
|||
public Task PublishAsync<TEvent>(TEvent eventData) |
|||
where TEvent : class |
|||
{ |
|||
return _eventBus.TriggerAsync(eventData); |
|||
return _localEventBus.PublishAsync(eventData); |
|||
} |
|||
|
|||
public Task PublishAsync(Type eventType, object eventData) |
|||
{ |
|||
return _eventBus.TriggerAsync(eventType, eventData); |
|||
return _localEventBus.PublishAsync(eventType, eventData); |
|||
} |
|||
} |
|||
} |
|||
@ -1,131 +1,7 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
/// <summary>
|
|||
/// Defines interface of the event bus.
|
|||
/// </summary>
|
|||
public interface IEventBus |
|||
public interface IEventBus : IEventSubscriber, IEventPublisher |
|||
{ |
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Given action is called for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="action">Action to handle events</param>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
IDisposable Register<TEvent>(Func<TEvent, Task> action) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Register<TEvent>(IEventHandler<TEvent> handler) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// A new instance of <see cref="THandler"/> object is created for every event occurrence.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <typeparam name="THandler">Type of the event handler</typeparam>
|
|||
IDisposable Register<TEvent, THandler>() |
|||
where TEvent : class |
|||
where THandler : IEventHandler, new(); |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Register(Type eventType, IEventHandler handler); |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Given factory is used to create/release handlers
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Register<TEvent>(IEventHandlerFactory factory) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Register(Type eventType, IEventHandlerFactory factory); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="action"></param>
|
|||
void Unregister<TEvent>(Func<TEvent, Task> action) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="handler">Handler object that is registered before</param>
|
|||
void Unregister<TEvent>(IEventHandler<TEvent> handler) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="handler">Handler object that is registered before</param>
|
|||
void Unregister(Type eventType, IEventHandler handler); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="factory">Factory object that is registered before</param>
|
|||
void Unregister<TEvent>(IEventHandlerFactory factory) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="factory">Factory object that is registered before</param>
|
|||
void Unregister(Type eventType, IEventHandlerFactory factory); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters all event handlers of given event type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
void UnregisterAll<TEvent>() |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters all event handlers of given event type.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
void UnregisterAll(Type eventType); |
|||
|
|||
/// <summary>
|
|||
/// Triggers an event asynchronously.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
/// <returns>The task to handle async operation</returns>
|
|||
Task TriggerAsync<TEvent>(TEvent eventData) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Triggers an event asynchronously.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
/// <returns>The task to handle async operation</returns>
|
|||
Task TriggerAsync(Type eventType, object eventData); |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public interface IEventPublisher |
|||
{ |
|||
/// <summary>
|
|||
/// Triggers an event asynchronously.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
/// <returns>The task to handle async operation</returns>
|
|||
Task PublishAsync<TEvent>(TEvent eventData) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Triggers an event asynchronously.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
/// <returns>The task to handle async operation</returns>
|
|||
Task PublishAsync(Type eventType, object eventData); |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public interface IEventSubscriber |
|||
{ |
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Given action is called for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="action">Action to handle events</param>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
IDisposable Subscribe<TEvent>(Func<TEvent, Task> action) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// A new instance of <see cref="THandler"/> object is created for every event occurrence.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <typeparam name="THandler">Type of the event handler</typeparam>
|
|||
IDisposable Subscribe<TEvent, THandler>() |
|||
where TEvent : class |
|||
where THandler : IEventHandler, new(); |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Same (given) instance of the handler is used for all event occurrences.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="handler">Object to handle the event</param>
|
|||
IDisposable Subscribe(Type eventType, IEventHandler handler); |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// Given factory is used to create/release handlers
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Subscribe<TEvent>(IEventHandlerFactory factory) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Registers to an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="factory">A factory to create/release handlers</param>
|
|||
IDisposable Subscribe(Type eventType, IEventHandlerFactory factory); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="action"></param>
|
|||
void Unsubscribe<TEvent>(Func<TEvent, Task> action) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="handler">Handler object that is registered before</param>
|
|||
void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="handler">Handler object that is registered before</param>
|
|||
void Unsubscribe(Type eventType, IEventHandler handler); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="factory">Factory object that is registered before</param>
|
|||
void Unsubscribe<TEvent>(IEventHandlerFactory factory) |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters from an event.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="factory">Factory object that is registered before</param>
|
|||
void Unsubscribe(Type eventType, IEventHandlerFactory factory); |
|||
|
|||
/// <summary>
|
|||
/// Unregisters all event handlers of given event type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
void UnsubscribeAll<TEvent>() |
|||
where TEvent : class; |
|||
|
|||
/// <summary>
|
|||
/// Unregisters all event handlers of given event type.
|
|||
/// </summary>
|
|||
/// <param name="eventType">Event type</param>
|
|||
void UnsubscribeAll(Type eventType); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
/// <summary>
|
|||
/// Defines interface of the event bus.
|
|||
/// </summary>
|
|||
public interface ILocalEventBus : IEventBus |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -1,90 +0,0 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public sealed class NullEventBus : IEventBus |
|||
{ |
|||
public static NullEventBus Instance { get; } = new NullEventBus(); |
|||
|
|||
private NullEventBus() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public IDisposable Register<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Register<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Register<TEvent, THandler>() where TEvent : class where THandler : IEventHandler, new() |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Register(Type eventType, IEventHandler handler) |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Register<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Register(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public void Unregister<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unregister<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unregister(Type eventType, IEventHandler handler) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unregister<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unregister(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void UnregisterAll<TEvent>() where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void UnregisterAll(Type eventType) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public Task TriggerAsync<TEvent>(TEvent eventData) where TEvent : class |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task TriggerAsync(Type eventType, object eventData) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,91 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.EventBus.Local; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public sealed class NullLocalEventBus : ILocalEventBus |
|||
{ |
|||
public static NullLocalEventBus Instance { get; } = new NullLocalEventBus(); |
|||
|
|||
private NullLocalEventBus() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent, THandler>() where TEvent : class where THandler : IEventHandler, new() |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Subscribe(Type eventType, IEventHandler handler) |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Subscribe<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unsubscribe(Type eventType, IEventHandler handler) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unsubscribe<TEvent>(IEventHandlerFactory factory) where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Unsubscribe(Type eventType, IEventHandlerFactory factory) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void UnsubscribeAll<TEvent>() where TEvent : class |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void UnsubscribeAll(Type eventType) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public Task PublishAsync<TEvent>(TEvent eventData) where TEvent : class |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task PublishAsync(Type eventType, object eventData) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public abstract class LocalDistributedEventBusTestBase : AbpIntegratedTest<EventBusTestModule> |
|||
{ |
|||
protected LocalDistributedEventBus LocalEventBus; |
|||
|
|||
protected LocalDistributedEventBusTestBase() |
|||
{ |
|||
LocalEventBus = GetRequiredService<LocalDistributedEventBus>(); |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Call_Handler_AndDispose() |
|||
{ |
|||
LocalEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>(); |
|||
|
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(1)); |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(2)); |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(3)); |
|||
|
|||
Assert.Equal(3, MySimpleDistributedTransientEventHandler.HandleCount); |
|||
Assert.Equal(3, MySimpleDistributedTransientEventHandler.DisposeCount); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public class MySimpleDistributedTransientEventHandler : IDistributedEventHandler<MySimpleEventData>, IDisposable |
|||
{ |
|||
public static int HandleCount { get; set; } |
|||
|
|||
public static int DisposeCount { get; set; } |
|||
|
|||
public Task HandleEventAsync(MySimpleEventData eventData) |
|||
{ |
|||
++HandleCount; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
++DisposeCount; |
|||
} |
|||
} |
|||
} |
|||
@ -1,48 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public class InheritanceTest : EventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Handle_Events_For_Derived_Classes() |
|||
{ |
|||
var totalData = 0; |
|||
|
|||
EventBus.Register<MySimpleEventData>( |
|||
eventData => |
|||
{ |
|||
totalData += eventData.Value; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
await EventBus.TriggerAsync(new MySimpleEventData(1)); //Should handle directly registered class
|
|||
await EventBus.TriggerAsync(new MySimpleEventData(2)); //Should handle directly registered class
|
|||
await EventBus.TriggerAsync(new MyDerivedEventData(3)); //Should handle derived class too
|
|||
await EventBus.TriggerAsync(new MyDerivedEventData(4)); //Should handle derived class too
|
|||
|
|||
Assert.Equal(10, totalData); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Handle_Events_For_Base_Classes() |
|||
{ |
|||
var totalData = 0; |
|||
|
|||
EventBus.Register<MyDerivedEventData>( |
|||
eventData => |
|||
{ |
|||
totalData += eventData.Value; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
await EventBus.TriggerAsync(new MySimpleEventData(1)); //Should not handle
|
|||
await EventBus.TriggerAsync(new MySimpleEventData(2)); //Should not handle
|
|||
await EventBus.TriggerAsync(new MyDerivedEventData(3)); //Should handle
|
|||
await EventBus.TriggerAsync(new MyDerivedEventData(4)); //Should handle
|
|||
|
|||
Assert.Equal(7, totalData); |
|||
} |
|||
} |
|||
} |
|||
@ -1,12 +1,12 @@ |
|||
namespace Volo.Abp.EventBus |
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public abstract class EventBusTestBase : AbpIntegratedTest<EventBusTestModule> |
|||
{ |
|||
protected IEventBus EventBus; |
|||
protected ILocalEventBus LocalEventBus; |
|||
|
|||
protected EventBusTestBase() |
|||
{ |
|||
EventBus = GetRequiredService<IEventBus>(); |
|||
LocalEventBus = GetRequiredService<ILocalEventBus>(); |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
@ -0,0 +1,48 @@ |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class InheritanceTest : EventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Handle_Events_For_Derived_Classes() |
|||
{ |
|||
var totalData = 0; |
|||
|
|||
LocalEventBus.Subscribe<MySimpleEventData>( |
|||
eventData => |
|||
{ |
|||
totalData += eventData.Value; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(1)); //Should handle directly registered class
|
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(2)); //Should handle directly registered class
|
|||
await LocalEventBus.PublishAsync(new MyDerivedEventData(3)); //Should handle derived class too
|
|||
await LocalEventBus.PublishAsync(new MyDerivedEventData(4)); //Should handle derived class too
|
|||
|
|||
Assert.Equal(10, totalData); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Handle_Events_For_Base_Classes() |
|||
{ |
|||
var totalData = 0; |
|||
|
|||
LocalEventBus.Subscribe<MyDerivedEventData>( |
|||
eventData => |
|||
{ |
|||
totalData += eventData.Value; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(1)); //Should not handle
|
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(2)); //Should not handle
|
|||
await LocalEventBus.PublishAsync(new MyDerivedEventData(3)); //Should handle
|
|||
await LocalEventBus.PublishAsync(new MyDerivedEventData(4)); //Should handle
|
|||
|
|||
Assert.Equal(7, totalData); |
|||
} |
|||
} |
|||
} |
|||
@ -1,7 +1,7 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class MySimpleEventDataHandler : IEventHandler<MySimpleEventData>, ISingletonDependency |
|||
{ |
|||
@ -1,7 +1,7 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class MySimpleTransientEventHandler : IEventHandler<MySimpleEventData>, IDisposable |
|||
{ |
|||
@ -1,18 +1,18 @@ |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class TransientDisposableEventHandlerTest : EventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Call_Handler_AndDispose() |
|||
{ |
|||
EventBus.Register<MySimpleEventData, MySimpleTransientEventHandler>(); |
|||
LocalEventBus.Subscribe<MySimpleEventData, MySimpleTransientEventHandler>(); |
|||
|
|||
await EventBus.TriggerAsync(new MySimpleEventData(1)); |
|||
await EventBus.TriggerAsync(new MySimpleEventData(2)); |
|||
await EventBus.TriggerAsync(new MySimpleEventData(3)); |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(1)); |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(2)); |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(3)); |
|||
|
|||
Assert.Equal(3, MySimpleTransientEventHandler.HandleCount); |
|||
Assert.Equal(3, MySimpleTransientEventHandler.DisposeCount); |
|||
Loading…
Reference in new issue