From f440446254b573ed6a642692ddd50b4a46e45dd2 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Tue, 13 Nov 2018 21:49:52 +0300 Subject: [PATCH] Renamed IEventBus to ILocalEventBus and introduced IDistributedEventBus --- .../Events/EntityChangeEventHelper.cs | 11 +- .../RabbitMq/RabbitMqDistributedEventBus.cs | 35 ++++ .../Distributed/IDistributedEventBus.cs | 64 +------ .../Distributed/LocalDistributedEventBus.cs | 59 +++++-- .../EventHandlerFactoryUnregistrar.cs | 2 +- .../Volo/Abp/EventBus/IEventBus.cs | 126 +------------- .../Volo/Abp/EventBus/IEventPublisher.cs | 25 +++ .../Volo/Abp/EventBus/IEventSubscriber.cs | 111 +++++++++++++ .../Volo/Abp/EventBus/Local/ILocalEventBus.cs | 10 ++ .../{EventBus.cs => Local/LocalEventBus.cs} | 156 +++++++++++------- .../Volo/Abp/EventBus/NullEventBus.cs | 90 ---------- .../Volo/Abp/EventBus/NullLocalEventBus.cs | 91 ++++++++++ .../Repositories/MongoDB/MongoDbRepository.cs | 7 +- .../LocalDistributedEventBusTestBase.cs | 17 ++ .../LocalDistributedEventBus_Test.cs | 21 +++ ...ySimpleDistributedTransientEventHandler.cs | 23 +++ .../Volo/Abp/EventBus/InheritanceTest.cs | 48 ------ .../ActionBasedEventHandlerTest.cs | 54 +++--- .../EventBus/{ => Local}/EventBusTestBase.cs | 6 +- .../{ => Local}/EventBus_DI_Services_Test.cs | 10 +- .../{ => Local}/EventBus_Exception_Test.cs | 12 +- .../EventBus_MultipleHandle_Test.cs | 8 +- .../{ => Local}/GenericInheritanceTest.cs | 10 +- .../Abp/EventBus/Local/InheritanceTest.cs | 48 ++++++ .../{ => Local}/MySimpleEventDataHandler.cs | 2 +- .../MySimpleTransientEventHandler.cs | 2 +- .../TransientDisposableEventHandlerTest.cs | 10 +- .../Abp/TestApp/Testing/DomainEvents_Tests.cs | 7 +- .../Testing/EntityChangeEvents_Tests.cs | 13 +- 29 files changed, 604 insertions(+), 474 deletions(-) create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventBus.cs rename framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/{EventBus.cs => Local/LocalEventBus.cs} (63%) delete mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullLocalEventBus.cs create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/MySimpleDistributedTransientEventHandler.cs delete mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/InheritanceTest.cs rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/ActionBasedEventHandlerTest.cs (51%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/EventBusTestBase.cs (67%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/EventBus_DI_Services_Test.cs (51%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/EventBus_Exception_Test.cs (73%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/EventBus_MultipleHandle_Test.cs (82%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/GenericInheritanceTest.cs (74%) create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/InheritanceTest.cs rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/MySimpleEventDataHandler.cs (91%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/MySimpleTransientEventHandler.cs (93%) rename framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/{ => Local}/TransientDisposableEventHandlerTest.cs (52%) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs index 832f511eaa..c557013a03 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs @@ -5,6 +5,7 @@ using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; using Volo.Abp.DynamicProxy; using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Local; using Volo.Abp.Uow; namespace Volo.Abp.Domain.Entities.Events @@ -14,14 +15,14 @@ namespace Volo.Abp.Domain.Entities.Events /// public class EntityChangeEventHelper : IEntityChangeEventHelper, ITransientDependency { - public IEventBus EventBus { get; set; } + public ILocalEventBus LocalEventBus { get; set; } private readonly IUnitOfWorkManager _unitOfWorkManager; public EntityChangeEventHelper(IUnitOfWorkManager unitOfWorkManager) { _unitOfWorkManager = unitOfWorkManager; - EventBus = NullEventBus.Instance; + LocalEventBus = NullLocalEventBus.Instance; } public async Task TriggerEventsAsync(EntityChangeReport changeReport) @@ -115,7 +116,7 @@ namespace Volo.Abp.Domain.Entities.Events { foreach (var domainEvent in domainEvents) { - await EventBus.TriggerAsync(domainEvent.EventData.GetType(), domainEvent.EventData); + await LocalEventBus.PublishAsync(domainEvent.EventData.GetType(), domainEvent.EventData); } } @@ -126,11 +127,11 @@ namespace Volo.Abp.Domain.Entities.Events if (triggerInCurrentUnitOfWork || _unitOfWorkManager.Current == null) { - await EventBus.TriggerAsync(eventType, Activator.CreateInstance(eventType, entity)); + await LocalEventBus.PublishAsync(eventType, Activator.CreateInstance(eventType, entity)); return; } - _unitOfWorkManager.Current.OnCompleted(() => EventBus.TriggerAsync(eventType, Activator.CreateInstance(eventType, entity))); + _unitOfWorkManager.Current.OnCompleted(() => LocalEventBus.PublishAsync(eventType, Activator.CreateInstance(eventType, entity))); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs index 0976be74f4..fa83254bad 100644 --- a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs @@ -57,6 +57,41 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq throw new NotImplementedException(); } + public void Unsubscribe(Func action) where TEvent : class + { + throw new NotImplementedException(); + } + + public void Unsubscribe(IEventHandler handler) where TEvent : class + { + throw new NotImplementedException(); + } + + public void Unsubscribe(Type eventType, IEventHandler handler) + { + throw new NotImplementedException(); + } + + public void Unsubscribe(IEventHandlerFactory factory) where TEvent : class + { + throw new NotImplementedException(); + } + + public void Unsubscribe(Type eventType, IEventHandlerFactory factory) + { + throw new NotImplementedException(); + } + + public void UnsubscribeAll() where TEvent : class + { + throw new NotImplementedException(); + } + + public void UnsubscribeAll(Type eventType) + { + throw new NotImplementedException(); + } + public Task PublishAsync(TEvent eventData) where TEvent : class { diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/IDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/IDistributedEventBus.cs index 26bbe9cb1c..88cbf721b3 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/IDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/IDistributedEventBus.cs @@ -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 { - /// - /// Subscribes to an event. - /// Given action is called for all event occurrences. - /// - /// Action to handle events - /// Event type - IDisposable Subscribe(Func action) - where TEvent : class; - - /// - /// Subscribes to an event. - /// Same (given) instance of the handler is used for all event occurrences. - /// - /// Event type - /// Object to handle the event - IDisposable Subscribe(IEventHandler handler) - where TEvent : class; - - /// - /// Subscribes to an event. - /// A new instance of object is created for every event occurrence. - /// - /// Event type - /// Type of the event handler - IDisposable Subscribe() - where TEvent : class - where THandler : IEventHandler, new(); - - /// - /// Subscribes to an event. - /// Same (given) instance of the handler is used for all event occurrences. - /// - /// Event type - /// Object to handle the event - IDisposable Subscribe(Type eventType, IEventHandler handler); - - /// - /// Subscribes to an event. - /// Given factory is used to create/release handlers - /// - /// Event type - /// A factory to create/release handlers - IDisposable Subscribe(IEventHandlerFactory factory) - where TEvent : class; - - /// - /// Subscribes to an event. - /// - /// Event type - /// A factory to create/release handlers - IDisposable Subscribe(Type eventType, IEventHandlerFactory factory); - - Task PublishAsync(TEvent eventData) - where TEvent : class; - - Task PublishAsync(Type eventType, object eventData); + } } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index 38c3687e48..ed5b0027cc 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -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(Func action) where TEvent : class { - return _eventBus.Register(action); + return _localEventBus.Subscribe(action); } public IDisposable Subscribe(IEventHandler handler) where TEvent : class { - return _eventBus.Register(handler); + return _localEventBus.Subscribe(handler); } public IDisposable Subscribe() where TEvent : class where THandler : IEventHandler, new() { - return _eventBus.Register(); + return _localEventBus.Subscribe(); } public IDisposable Subscribe(Type eventType, IEventHandler handler) { - return _eventBus.Register(eventType, handler); + return _localEventBus.Subscribe(eventType, handler); } public IDisposable Subscribe(IEventHandlerFactory factory) where TEvent : class { - return _eventBus.Register(factory); + return _localEventBus.Subscribe(factory); } public IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) { - return _eventBus.Register(eventType, factory); + return _localEventBus.Subscribe(eventType, factory); + } + + public void Unsubscribe(Func action) where TEvent : class + { + _localEventBus.Unsubscribe(action); + } + + public void Unsubscribe(IEventHandler handler) where TEvent : class + { + _localEventBus.Unsubscribe(handler); + } + + public void Unsubscribe(Type eventType, IEventHandler handler) + { + _localEventBus.Unsubscribe(eventType, handler); + } + + public void Unsubscribe(IEventHandlerFactory factory) where TEvent : class + { + _localEventBus.Unsubscribe(factory); + } + + public void Unsubscribe(Type eventType, IEventHandlerFactory factory) + { + _localEventBus.Unsubscribe(eventType, factory); + } + + public void UnsubscribeAll() where TEvent : class + { + _localEventBus.UnsubscribeAll(); + } + + public void UnsubscribeAll(Type eventType) + { + _localEventBus.UnsubscribeAll(eventType); } public Task PublishAsync(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); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerFactoryUnregistrar.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerFactoryUnregistrar.cs index 671d8e4705..0a07db67f6 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerFactoryUnregistrar.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerFactoryUnregistrar.cs @@ -20,7 +20,7 @@ namespace Volo.Abp.EventBus public void Dispose() { - _eventBus.Unregister(_eventType, _factory); + _eventBus.Unsubscribe(_eventType, _factory); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs index 1ca7c4ba50..a5d1c27a15 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs @@ -1,131 +1,7 @@ -using System; -using System.Threading.Tasks; - namespace Volo.Abp.EventBus { - /// - /// Defines interface of the event bus. - /// - public interface IEventBus + public interface IEventBus : IEventSubscriber, IEventPublisher { - /// - /// Registers to an event. - /// Given action is called for all event occurrences. - /// - /// Action to handle events - /// Event type - IDisposable Register(Func action) - where TEvent : class; - - /// - /// Registers to an event. - /// Same (given) instance of the handler is used for all event occurrences. - /// - /// Event type - /// Object to handle the event - IDisposable Register(IEventHandler handler) - where TEvent : class; - - /// - /// Registers to an event. - /// A new instance of object is created for every event occurrence. - /// - /// Event type - /// Type of the event handler - IDisposable Register() - where TEvent : class - where THandler : IEventHandler, new(); - - /// - /// Registers to an event. - /// Same (given) instance of the handler is used for all event occurrences. - /// - /// Event type - /// Object to handle the event - IDisposable Register(Type eventType, IEventHandler handler); - - /// - /// Registers to an event. - /// Given factory is used to create/release handlers - /// - /// Event type - /// A factory to create/release handlers - IDisposable Register(IEventHandlerFactory factory) - where TEvent : class; - - /// - /// Registers to an event. - /// - /// Event type - /// A factory to create/release handlers - IDisposable Register(Type eventType, IEventHandlerFactory factory); - - /// - /// Unregisters from an event. - /// - /// Event type - /// - void Unregister(Func action) - where TEvent : class; - - /// - /// Unregisters from an event. - /// - /// Event type - /// Handler object that is registered before - void Unregister(IEventHandler handler) - where TEvent : class; - - /// - /// Unregisters from an event. - /// - /// Event type - /// Handler object that is registered before - void Unregister(Type eventType, IEventHandler handler); - - /// - /// Unregisters from an event. - /// - /// Event type - /// Factory object that is registered before - void Unregister(IEventHandlerFactory factory) - where TEvent : class; - - /// - /// Unregisters from an event. - /// - /// Event type - /// Factory object that is registered before - void Unregister(Type eventType, IEventHandlerFactory factory); - - /// - /// Unregisters all event handlers of given event type. - /// - /// Event type - void UnregisterAll() - where TEvent : class; - - /// - /// Unregisters all event handlers of given event type. - /// - /// Event type - void UnregisterAll(Type eventType); - - /// - /// Triggers an event asynchronously. - /// - /// Event type - /// Related data for the event - /// The task to handle async operation - Task TriggerAsync(TEvent eventData) - where TEvent : class; - /// - /// Triggers an event asynchronously. - /// - /// Event type - /// Related data for the event - /// The task to handle async operation - Task TriggerAsync(Type eventType, object eventData); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs new file mode 100644 index 0000000000..0192522304 --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.EventBus +{ + public interface IEventPublisher + { + /// + /// Triggers an event asynchronously. + /// + /// Event type + /// Related data for the event + /// The task to handle async operation + Task PublishAsync(TEvent eventData) + where TEvent : class; + + /// + /// Triggers an event asynchronously. + /// + /// Event type + /// Related data for the event + /// The task to handle async operation + Task PublishAsync(Type eventType, object eventData); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs new file mode 100644 index 0000000000..86330433ea --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs @@ -0,0 +1,111 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.EventBus +{ + public interface IEventSubscriber + { + /// + /// Registers to an event. + /// Given action is called for all event occurrences. + /// + /// Action to handle events + /// Event type + IDisposable Subscribe(Func action) + where TEvent : class; + + /// + /// Registers to an event. + /// Same (given) instance of the handler is used for all event occurrences. + /// + /// Event type + /// Object to handle the event + IDisposable Subscribe(IEventHandler handler) + where TEvent : class; + + /// + /// Registers to an event. + /// A new instance of object is created for every event occurrence. + /// + /// Event type + /// Type of the event handler + IDisposable Subscribe() + where TEvent : class + where THandler : IEventHandler, new(); + + /// + /// Registers to an event. + /// Same (given) instance of the handler is used for all event occurrences. + /// + /// Event type + /// Object to handle the event + IDisposable Subscribe(Type eventType, IEventHandler handler); + + /// + /// Registers to an event. + /// Given factory is used to create/release handlers + /// + /// Event type + /// A factory to create/release handlers + IDisposable Subscribe(IEventHandlerFactory factory) + where TEvent : class; + + /// + /// Registers to an event. + /// + /// Event type + /// A factory to create/release handlers + IDisposable Subscribe(Type eventType, IEventHandlerFactory factory); + + /// + /// Unregisters from an event. + /// + /// Event type + /// + void Unsubscribe(Func action) + where TEvent : class; + + /// + /// Unregisters from an event. + /// + /// Event type + /// Handler object that is registered before + void Unsubscribe(IEventHandler handler) + where TEvent : class; + + /// + /// Unregisters from an event. + /// + /// Event type + /// Handler object that is registered before + void Unsubscribe(Type eventType, IEventHandler handler); + + /// + /// Unregisters from an event. + /// + /// Event type + /// Factory object that is registered before + void Unsubscribe(IEventHandlerFactory factory) + where TEvent : class; + + /// + /// Unregisters from an event. + /// + /// Event type + /// Factory object that is registered before + void Unsubscribe(Type eventType, IEventHandlerFactory factory); + + /// + /// Unregisters all event handlers of given event type. + /// + /// Event type + void UnsubscribeAll() + where TEvent : class; + + /// + /// Unregisters all event handlers of given event type. + /// + /// Event type + void UnsubscribeAll(Type eventType); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventBus.cs new file mode 100644 index 0000000000..7e81a363af --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventBus.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.EventBus.Local +{ + /// + /// Defines interface of the event bus. + /// + public interface ILocalEventBus : IEventBus + { + + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs similarity index 63% rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBus.cs rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index 3c7243c70e..8f4cc5265c 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -1,3 +1,6 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -6,47 +9,46 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; +using Volo.Abp.Collections; using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Reflection; using Volo.Abp.Threading; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { /// /// Implements EventBus as Singleton pattern. /// - public class EventBus : IEventBus, ISingletonDependency + [ExposeServices(typeof(ILocalEventBus), typeof(LocalEventBus))] + public class LocalEventBus : ILocalEventBus, ISingletonDependency { /// /// Reference to the Logger. /// - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } + + protected EventBusOptions Options { get; } - /// - /// All registered handler factories. - /// Key: Type of the event - /// Value: List of handler factories - /// protected ConcurrentDictionary> HandlerFactories { get; } - protected EventBusOptions Options { get; } + protected IServiceProvider ServiceProvider { get; } - public EventBus( - IOptions options, + public LocalEventBus( + IOptions options, IServiceProvider serviceProvider) { + ServiceProvider = serviceProvider; Options = options.Value; - Logger = NullLogger.Instance; - HandlerFactories = new ConcurrentDictionary>(); + Logger = NullLogger.Instance; - RegisterHandlersInOptions(serviceProvider); + HandlerFactories = new ConcurrentDictionary>(); + Subscribe(Options.Handlers); } - protected virtual void RegisterHandlersInOptions(IServiceProvider serviceProvider) + public virtual void Subscribe(ITypeList handlers) { - foreach (var handler in Options.Handlers) + foreach (var handler in handlers) { var interfaces = handler.GetInterfaces(); foreach (var @interface in interfaces) @@ -59,46 +61,46 @@ namespace Volo.Abp.EventBus var genericArgs = @interface.GetGenericArguments(); if (genericArgs.Length == 1) { - Register(genericArgs[0], new IocEventHandlerFactory(serviceProvider, handler)); + Subscribe(genericArgs[0], new IocEventHandlerFactory(ServiceProvider, handler)); } } } } /// - public IDisposable Register(Func action) where TEvent : class + public IDisposable Subscribe(Func action) where TEvent : class { - return Register(typeof(TEvent), new ActionEventHandler(action)); + return Subscribe(typeof(TEvent), new ActionEventHandler(action)); } /// - public IDisposable Register(IEventHandler handler) where TEvent : class + public IDisposable Subscribe(IEventHandler handler) where TEvent : class { - return Register(typeof(TEvent), handler); + return Subscribe(typeof(TEvent), handler); } /// - public IDisposable Register() + public IDisposable Subscribe() where TEvent : class where THandler : IEventHandler, new() { - return Register(typeof(TEvent), new TransientEventHandlerFactory()); + return Subscribe(typeof(TEvent), new TransientEventHandlerFactory()); } /// - public IDisposable Register(Type eventType, IEventHandler handler) + public IDisposable Subscribe(Type eventType, IEventHandler handler) { - return Register(eventType, new SingleInstanceHandlerFactory(handler)); + return Subscribe(eventType, new SingleInstanceHandlerFactory(handler)); } /// - public IDisposable Register(IEventHandlerFactory factory) where TEvent : class + public IDisposable Subscribe(IEventHandlerFactory factory) where TEvent : class { - return Register(typeof(TEvent), factory); + return Subscribe(typeof(TEvent), factory); } /// - public IDisposable Register(Type eventType, IEventHandlerFactory factory) + public IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) { GetOrCreateHandlerFactories(eventType) .Locking(factories => factories.Add(factory)); @@ -107,7 +109,7 @@ namespace Volo.Abp.EventBus } /// - public void Unregister(Func action) where TEvent : class + public void Unsubscribe(Func action) where TEvent : class { Check.NotNull(action, nameof(action)); @@ -135,13 +137,13 @@ namespace Volo.Abp.EventBus } /// - public void Unregister(IEventHandler handler) where TEvent : class + public void Unsubscribe(IEventHandler handler) where TEvent : class { - Unregister(typeof(TEvent), handler); + Unsubscribe(typeof(TEvent), handler); } /// - public void Unregister(Type eventType, IEventHandler handler) + public void Unsubscribe(Type eventType, IEventHandler handler) { GetOrCreateHandlerFactories(eventType) .Locking(factories => @@ -150,43 +152,45 @@ namespace Volo.Abp.EventBus factory => factory is SingleInstanceHandlerFactory && (factory as SingleInstanceHandlerFactory).HandlerInstance == handler - ); + ); }); } /// - public void Unregister(IEventHandlerFactory factory) where TEvent : class + public void Unsubscribe(IEventHandlerFactory factory) where TEvent : class { - Unregister(typeof(TEvent), factory); + Unsubscribe(typeof(TEvent), factory); } /// - public void Unregister(Type eventType, IEventHandlerFactory factory) + public void Unsubscribe(Type eventType, IEventHandlerFactory factory) { GetOrCreateHandlerFactories(eventType).Locking(factories => factories.Remove(factory)); } /// - public void UnregisterAll() where TEvent : class + public void UnsubscribeAll() where TEvent : class { - UnregisterAll(typeof(TEvent)); + UnsubscribeAll(typeof(TEvent)); } /// - public void UnregisterAll(Type eventType) + public void UnsubscribeAll(Type eventType) { GetOrCreateHandlerFactories(eventType).Locking(factories => factories.Clear()); } /// - public Task TriggerAsync(TEvent eventData) where TEvent : class + public Task PublishAsync(TEvent eventData) where TEvent : class { - return TriggerAsync(typeof(TEvent), eventData); + return PublishAsync(typeof(TEvent), eventData); } /// - public async Task TriggerAsync(Type eventType, object eventData) + public async Task PublishAsync(Type eventType, object eventData) { + //TODO: Aggregate all exceptions (including the recursive call)! + var exceptions = new List(); await new SynchronizationContextRemover(); @@ -199,7 +203,7 @@ namespace Volo.Abp.EventBus } } - //Implements generic argument inheritance. See classWithInheritableGenericArgument + //Implements generic argument inheritance. See IEventDataWithInheritableGenericArgument if (eventType.GetTypeInfo().IsGenericType && eventType.GetGenericArguments().Length == 1 && typeof(IEventDataWithInheritableGenericArgument).IsAssignableFrom(eventType)) @@ -211,7 +215,7 @@ namespace Volo.Abp.EventBus var baseEventType = eventType.GetGenericTypeDefinition().MakeGenericType(baseArg); var constructorArgs = ((IEventDataWithInheritableGenericArgument)eventData).GetConstructorArgs(); var baseEventData = Activator.CreateInstance(baseEventType, constructorArgs); - await TriggerAsync(baseEventType, baseEventData); + await PublishAsync(baseEventType, baseEventData); } } @@ -232,14 +236,38 @@ namespace Volo.Abp.EventBus { try { - var asyncHandlerType = typeof(IEventHandler<>).MakeGenericType(eventType); - - var method = asyncHandlerType.GetMethod( - "HandleEventAsync", - new[] { eventType } - ); + var handlerType = eventHandlerWrapper.EventHandler.GetType(); - await (Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData }); + if (ReflectionHelper.IsAssignableToGenericType( + handlerType, + typeof(IEventHandler<>))) + { + var method = typeof(IEventHandler<>) //TODO: to a static field + .MakeGenericType(eventType) + .GetMethod( + nameof(IEventHandler.HandleEventAsync), + new[] {eventType} + ); + + await (Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData }); + } + else if (ReflectionHelper.IsAssignableToGenericType( + handlerType, + typeof(IDistributedEventHandler<>))) + { + var method = typeof(IDistributedEventHandler<>) //TODO: to a static field + .MakeGenericType(eventType) + .GetMethod( + nameof(IDistributedEventHandler.HandleEventAsync), + new[] {eventType} + ); + + await (Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData }); + } + else + { + throw new AbpException("The object instance is not an event handler. Object type: " + handlerType.AssemblyQualifiedName); + } } catch (TargetInvocationException ex) { @@ -252,7 +280,7 @@ namespace Volo.Abp.EventBus } } - private IEnumerable GetHandlerFactories(Type eventType) + public virtual IEnumerable GetHandlerFactories(Type eventType) { var handlerFactoryList = new List(); @@ -264,16 +292,21 @@ namespace Volo.Abp.EventBus return handlerFactoryList.ToArray(); } - private static bool ShouldTriggerEventForHandler(Type eventType, Type handlerType) + private List GetOrCreateHandlerFactories(Type eventType) + { + return HandlerFactories.GetOrAdd(eventType, (type) => new List()); + } + + private static bool ShouldTriggerEventForHandler(Type targetEventType, Type handlerEventType) { //Should trigger same type - if (handlerType == eventType) + if (handlerEventType == targetEventType) { return true; } //Should trigger for inherited types - if (handlerType.IsAssignableFrom(eventType)) + if (handlerEventType.IsAssignableFrom(targetEventType)) { return true; } @@ -281,12 +314,7 @@ namespace Volo.Abp.EventBus return false; } - private List GetOrCreateHandlerFactories(Type eventType) - { - return HandlerFactories.GetOrAdd(eventType, (type) => new List()); - } - - private class EventTypeWithEventHandlerFactories + public class EventTypeWithEventHandlerFactories { public Type EventType { get; } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs deleted file mode 100644 index ed55f4217f..0000000000 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs +++ /dev/null @@ -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(Func action) where TEvent : class - { - return NullDisposable.Instance; - } - - public IDisposable Register(IEventHandler handler) where TEvent : class - { - return NullDisposable.Instance; - } - - public IDisposable Register() where TEvent : class where THandler : IEventHandler, new() - { - return NullDisposable.Instance; - } - - public IDisposable Register(Type eventType, IEventHandler handler) - { - return NullDisposable.Instance; - } - - public IDisposable Register(IEventHandlerFactory factory) where TEvent : class - { - return NullDisposable.Instance; - } - - public IDisposable Register(Type eventType, IEventHandlerFactory factory) - { - return NullDisposable.Instance; - } - - public void Unregister(Func action) where TEvent : class - { - - } - - public void Unregister(IEventHandler handler) where TEvent : class - { - - } - - public void Unregister(Type eventType, IEventHandler handler) - { - - } - - public void Unregister(IEventHandlerFactory factory) where TEvent : class - { - - } - - public void Unregister(Type eventType, IEventHandlerFactory factory) - { - - } - - public void UnregisterAll() where TEvent : class - { - - } - - public void UnregisterAll(Type eventType) - { - - } - - public Task TriggerAsync(TEvent eventData) where TEvent : class - { - return Task.CompletedTask; - } - - public Task TriggerAsync(Type eventType, object eventData) - { - return Task.CompletedTask; - } - } -} diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullLocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullLocalEventBus.cs new file mode 100644 index 0000000000..f02365d8fa --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullLocalEventBus.cs @@ -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(Func action) where TEvent : class + { + return NullDisposable.Instance; + } + + public IDisposable Subscribe(IEventHandler handler) where TEvent : class + { + return NullDisposable.Instance; + } + + public IDisposable Subscribe() where TEvent : class where THandler : IEventHandler, new() + { + return NullDisposable.Instance; + } + + public IDisposable Subscribe(Type eventType, IEventHandler handler) + { + return NullDisposable.Instance; + } + + public IDisposable Subscribe(IEventHandlerFactory factory) where TEvent : class + { + return NullDisposable.Instance; + } + + public IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) + { + return NullDisposable.Instance; + } + + public void Unsubscribe(Func action) where TEvent : class + { + + } + + public void Unsubscribe(IEventHandler handler) where TEvent : class + { + + } + + public void Unsubscribe(Type eventType, IEventHandler handler) + { + + } + + public void Unsubscribe(IEventHandlerFactory factory) where TEvent : class + { + + } + + public void Unsubscribe(Type eventType, IEventHandlerFactory factory) + { + + } + + public void UnsubscribeAll() where TEvent : class + { + + } + + public void UnsubscribeAll(Type eventType) + { + + } + + public Task PublishAsync(TEvent eventData) where TEvent : class + { + return Task.CompletedTask; + } + + public Task PublishAsync(Type eventType, object eventData) + { + return Task.CompletedTask; + } + } +} diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index b7e2cf2a16..17bc43ad14 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -10,6 +10,7 @@ using Volo.Abp.Auditing; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities.Events; using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Local; using Volo.Abp.Guids; using Volo.Abp.MongoDB; using Volo.Abp.MultiTenancy; @@ -31,7 +32,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB protected IMongoDbContextProvider DbContextProvider { get; } - public IEventBus EventBus { get; set; } + public ILocalEventBus LocalEventBus { get; set; } public IEntityChangeEventHelper EntityChangeEventHelper { get; set; } @@ -43,7 +44,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB { DbContextProvider = dbContextProvider; - EventBus = NullEventBus.Instance; + LocalEventBus = NullLocalEventBus.Instance; EntityChangeEventHelper = NullEntityChangeEventHelper.Instance; } @@ -313,7 +314,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB foreach (var entityEvent in entityEvents) { - await EventBus.TriggerAsync(entityEvent.GetType(), entityEvent); + await LocalEventBus.PublishAsync(entityEvent.GetType(), entityEvent); } generatesDomainEventsEntity.ClearDomainEvents(); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs new file mode 100644 index 0000000000..45671715a3 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs @@ -0,0 +1,17 @@ +namespace Volo.Abp.EventBus.Distributed +{ + public abstract class LocalDistributedEventBusTestBase : AbpIntegratedTest + { + protected LocalDistributedEventBus LocalEventBus; + + protected LocalDistributedEventBusTestBase() + { + LocalEventBus = GetRequiredService(); + } + + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs new file mode 100644 index 0000000000..24dfbf357f --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs @@ -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(); + + 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); + } + } +} diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/MySimpleDistributedTransientEventHandler.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/MySimpleDistributedTransientEventHandler.cs new file mode 100644 index 0000000000..178626373d --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/MySimpleDistributedTransientEventHandler.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.EventBus.Distributed +{ + public class MySimpleDistributedTransientEventHandler : IDistributedEventHandler, 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; + } + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/InheritanceTest.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/InheritanceTest.cs deleted file mode 100644 index 5168e11054..0000000000 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/InheritanceTest.cs +++ /dev/null @@ -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( - 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( - 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); - } - } -} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/ActionBasedEventHandlerTest.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/ActionBasedEventHandlerTest.cs similarity index 51% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/ActionBasedEventHandlerTest.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/ActionBasedEventHandlerTest.cs index c2938af979..6e6bae2fae 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/ActionBasedEventHandlerTest.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/ActionBasedEventHandlerTest.cs @@ -3,7 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Xunit; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class ActionBasedEventHandlerTest : EventBusTestBase { @@ -12,17 +12,17 @@ namespace Volo.Abp.EventBus { var totalData = 0; - EventBus.Register( + LocalEventBus.Subscribe( eventData => { totalData += eventData.Value; return Task.CompletedTask; }); - await EventBus.TriggerAsync(new MySimpleEventData(1)); - await EventBus.TriggerAsync(new MySimpleEventData(2)); - await EventBus.TriggerAsync(new MySimpleEventData(3)); - await EventBus.TriggerAsync(new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(new MySimpleEventData(2)); + await LocalEventBus.PublishAsync(new MySimpleEventData(3)); + await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(10, totalData); } @@ -32,17 +32,17 @@ namespace Volo.Abp.EventBus { var totalData = 0; - EventBus.Register( + LocalEventBus.Subscribe( eventData => { totalData += eventData.Value; return Task.CompletedTask; }); - await EventBus.TriggerAsync(typeof(MySimpleEventData), new MySimpleEventData(1)); - await EventBus.TriggerAsync(typeof(MySimpleEventData), new MySimpleEventData(2)); - await EventBus.TriggerAsync(typeof(MySimpleEventData), new MySimpleEventData(3)); - await EventBus.TriggerAsync(typeof(MySimpleEventData), new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(2)); + await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(3)); + await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(4)); Assert.Equal(10, totalData); } @@ -52,20 +52,20 @@ namespace Volo.Abp.EventBus { var totalData = 0; - var registerDisposer = EventBus.Register( + var registerDisposer = LocalEventBus.Subscribe( eventData => { totalData += eventData.Value; return Task.CompletedTask; }); - 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)); registerDisposer.Dispose(); - await EventBus.TriggerAsync(new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(6, totalData); } @@ -82,15 +82,15 @@ namespace Volo.Abp.EventBus return Task.CompletedTask; }); - EventBus.Register(action); + LocalEventBus.Subscribe(action); - 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)); - EventBus.Unregister(action); + LocalEventBus.Unsubscribe(action); - await EventBus.TriggerAsync(new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(6, totalData); } @@ -100,7 +100,7 @@ namespace Volo.Abp.EventBus { int totalData = 0; - EventBus.Register( + LocalEventBus.Subscribe( async eventData => { await Task.Delay(20); @@ -108,10 +108,10 @@ namespace Volo.Abp.EventBus await Task.Delay(20); }); - await EventBus.TriggerAsync(new MySimpleEventData(1)); - await EventBus.TriggerAsync(new MySimpleEventData(2)); - await EventBus.TriggerAsync(new MySimpleEventData(3)); - await EventBus.TriggerAsync(new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(new MySimpleEventData(2)); + await LocalEventBus.PublishAsync(new MySimpleEventData(3)); + await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(10, totalData); } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBusTestBase.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBusTestBase.cs similarity index 67% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBusTestBase.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBusTestBase.cs index eca0970da4..b3a55c340b 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBusTestBase.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBusTestBase.cs @@ -1,12 +1,12 @@ -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public abstract class EventBusTestBase : AbpIntegratedTest { - protected IEventBus EventBus; + protected ILocalEventBus LocalEventBus; protected EventBusTestBase() { - EventBus = GetRequiredService(); + LocalEventBus = GetRequiredService(); } protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_DI_Services_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_DI_Services_Test.cs similarity index 51% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_DI_Services_Test.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_DI_Services_Test.cs index 0567522d68..e66deb0e1c 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_DI_Services_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_DI_Services_Test.cs @@ -2,17 +2,17 @@ using Shouldly; using Xunit; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class EventBus_DI_Services_Test : EventBusTestBase { [Fact] public async Task Should_Automatically_Register_EventHandlers_From_Services() { - await EventBus.TriggerAsync(new MySimpleEventData(1)); - await EventBus.TriggerAsync(new MySimpleEventData(2)); - await EventBus.TriggerAsync(new MySimpleEventData(3)); - await EventBus.TriggerAsync(new MySimpleEventData(4)); + await LocalEventBus.PublishAsync(new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(new MySimpleEventData(2)); + await LocalEventBus.PublishAsync(new MySimpleEventData(3)); + await LocalEventBus.PublishAsync(new MySimpleEventData(4)); GetRequiredService().TotalData.ShouldBe(10); } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_Exception_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Exception_Test.cs similarity index 73% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_Exception_Test.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Exception_Test.cs index 9613580b6b..446825c697 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_Exception_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Exception_Test.cs @@ -3,18 +3,18 @@ using System.Threading.Tasks; using Shouldly; using Xunit; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class EventBus_Exception_Test : EventBusTestBase { [Fact] public async Task Should_Throw_Single_Exception_If_Only_One_Of_Handlers_Fails() { - EventBus.Register(eventData => throw new Exception("This exception is intentionally thrown!")); + LocalEventBus.Subscribe(eventData => throw new Exception("This exception is intentionally thrown!")); var appException = await Assert.ThrowsAsync(async () => { - await EventBus.TriggerAsync(new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(new MySimpleEventData(1)); }); appException.Message.ShouldBe("This exception is intentionally thrown!"); @@ -23,15 +23,15 @@ namespace Volo.Abp.EventBus [Fact] public async Task Should_Throw_Aggregate_Exception_If_More_Than_One_Of_Handlers_Fail() { - EventBus.Register( + LocalEventBus.Subscribe( eventData => throw new Exception("This exception is intentionally thrown #1!")); - EventBus.Register( + LocalEventBus.Subscribe( eventData => throw new Exception("This exception is intentionally thrown #2!")); var aggrException = await Assert.ThrowsAsync(async () => { - await EventBus.TriggerAsync(new MySimpleEventData(1)); + await LocalEventBus.PublishAsync(new MySimpleEventData(1)); }); aggrException.InnerExceptions.Count.ShouldBe(2); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_MultipleHandle_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs similarity index 82% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_MultipleHandle_Test.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs index 6892891f90..b8dfe43e17 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_MultipleHandle_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs @@ -4,7 +4,7 @@ using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities.Events; using Xunit; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class EventBus_EntityEvents_Test : EventBusTestBase { @@ -13,10 +13,10 @@ namespace Volo.Abp.EventBus { var handler = new MyEventHandler(); - EventBus.Register>(handler); - EventBus.Register>(handler); + LocalEventBus.Subscribe>(handler); + LocalEventBus.Subscribe>(handler); - await EventBus.TriggerAsync(new EntityCreatedEventData(new MyEntity())); + await LocalEventBus.PublishAsync(new EntityCreatedEventData(new MyEntity())); handler.EntityCreatedEventCount.ShouldBe(1); handler.EntityChangedEventCount.ShouldBe(1); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericInheritanceTest.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/GenericInheritanceTest.cs similarity index 74% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericInheritanceTest.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/GenericInheritanceTest.cs index 4a3fbccce8..d770a81325 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/GenericInheritanceTest.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/GenericInheritanceTest.cs @@ -4,7 +4,7 @@ using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities.Events; using Xunit; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class GenericInheritanceTest : EventBusTestBase { @@ -13,7 +13,7 @@ namespace Volo.Abp.EventBus { var triggeredEvent = false; - EventBus.Register>( + LocalEventBus.Subscribe>( eventData => { eventData.Entity.Id.ShouldBe(42); @@ -21,7 +21,7 @@ namespace Volo.Abp.EventBus return Task.CompletedTask; }); - await EventBus.TriggerAsync(new EntityUpdatedEventData(new Person { Id = 42 })); + await LocalEventBus.PublishAsync(new EntityUpdatedEventData(new Person { Id = 42 })); triggeredEvent.ShouldBe(true); } @@ -31,7 +31,7 @@ namespace Volo.Abp.EventBus { var triggeredEvent = false; - EventBus.Register>( + LocalEventBus.Subscribe>( eventData => { eventData.Entity.Id.ShouldBe(42); @@ -39,7 +39,7 @@ namespace Volo.Abp.EventBus return Task.CompletedTask; }); - await EventBus.TriggerAsync(new EntityChangedEventData(new Student { Id = 42 })); + await LocalEventBus.PublishAsync(new EntityChangedEventData(new Student { Id = 42 })); triggeredEvent.ShouldBe(true); } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/InheritanceTest.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/InheritanceTest.cs new file mode 100644 index 0000000000..c3b1dd4283 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/InheritanceTest.cs @@ -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( + 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( + 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); + } + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleEventDataHandler.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs similarity index 91% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleEventDataHandler.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs index 700a68ef05..ac725f2ae3 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleEventDataHandler.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs @@ -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, ISingletonDependency { diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientEventHandler.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs similarity index 93% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientEventHandler.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs index be2b98026f..6adf34b51b 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientEventHandler.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace Volo.Abp.EventBus +namespace Volo.Abp.EventBus.Local { public class MySimpleTransientEventHandler : IEventHandler, IDisposable { diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/TransientDisposableEventHandlerTest.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/TransientDisposableEventHandlerTest.cs similarity index 52% rename from framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/TransientDisposableEventHandlerTest.cs rename to framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/TransientDisposableEventHandlerTest.cs index 3250128ea8..5ab7ed51af 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/TransientDisposableEventHandlerTest.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/TransientDisposableEventHandlerTest.cs @@ -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(); + LocalEventBus.Subscribe(); - 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); diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs index 38e219677f..ed62e14ecd 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Shouldly; using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Local; using Volo.Abp.Modularity; using Volo.Abp.TestApp.Domain; using Xunit; @@ -14,12 +15,12 @@ namespace Volo.Abp.TestApp.Testing where TStartupModule : IAbpModule { protected readonly IRepository PersonRepository; - protected readonly IEventBus EventBus; + protected readonly ILocalEventBus LocalEventBus; protected DomainEvents_Tests() { PersonRepository = GetRequiredService>(); - EventBus = GetRequiredService(); + LocalEventBus = GetRequiredService(); } [Fact] @@ -29,7 +30,7 @@ namespace Volo.Abp.TestApp.Testing var isTriggered = false; - EventBus.Register(data => + LocalEventBus.Subscribe(data => { data.OldName.ShouldBe("Douglas"); data.Person.Name.ShouldBe("Douglas-Changed"); diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs index a945120a43..0fd6a72a04 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityChangeEvents_Tests.cs @@ -4,6 +4,7 @@ using Shouldly; using Volo.Abp.Domain.Entities.Events; using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Local; using Volo.Abp.Modularity; using Volo.Abp.TestApp.Domain; using Volo.Abp.Uow; @@ -15,12 +16,12 @@ namespace Volo.Abp.TestApp.Testing where TStartupModule : IAbpModule { protected IRepository PersonRepository { get; } - protected IEventBus EventBus { get; } + protected ILocalEventBus LocalEventBus { get; } protected EntityChangeEvents_Tests() { PersonRepository = GetRequiredService>(); - EventBus = GetRequiredService(); + LocalEventBus = GetRequiredService(); } [Fact] @@ -35,7 +36,7 @@ namespace Volo.Abp.TestApp.Testing using (var uow = GetRequiredService().Begin()) { - EventBus.Register>(data => + LocalEventBus.Subscribe>(data => { creatingEventTriggered.ShouldBeFalse(); createdEventTriggered.ShouldBeFalse(); @@ -54,7 +55,7 @@ namespace Volo.Abp.TestApp.Testing return Task.CompletedTask; }); - EventBus.Register>(data => + LocalEventBus.Subscribe>(data => { creatingEventTriggered.ShouldBeTrue(); createdEventTriggered.ShouldBeFalse(); @@ -69,7 +70,7 @@ namespace Volo.Abp.TestApp.Testing return Task.CompletedTask; }); - EventBus.Register>(data => + LocalEventBus.Subscribe>(data => { creatingEventTriggered.ShouldBeTrue(); createdEventTriggered.ShouldBeFalse(); @@ -84,7 +85,7 @@ namespace Volo.Abp.TestApp.Testing return Task.CompletedTask; }); - EventBus.Register>(data => + LocalEventBus.Subscribe>(data => { creatingEventTriggered.ShouldBeTrue(); createdEventTriggered.ShouldBeTrue();