From d678b5f29db930245c4546904722e0489ca96800 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 3 Dec 2018 10:17:07 +0300 Subject: [PATCH] Refactored eventbus --- .../Events/EntityChangeEventHelper.cs | 2 +- .../Volo/Abp/EventBus/AbpEventBusModule.cs | 12 +- .../Volo/Abp/EventBus/ActionEventHandler.cs | 4 +- ...tions.cs => DistributedEventBusOptions.cs} | 0 .../Distributed/LocalDistributedEventBus.cs | 4 +- ...EventBus.cs => NullDistributedEventBus.cs} | 4 +- .../Volo/Abp/EventBus/EventBusBase.cs | 14 +- .../Volo/Abp/EventBus/IEventBus.cs | 123 +++++++++++++++++- .../Volo/Abp/EventBus/IEventHandler.cs | 17 +-- .../Volo/Abp/EventBus/IEventPublisher.cs | 25 ---- .../Volo/Abp/EventBus/IEventSubscriber.cs | 111 ---------------- .../Abp/EventBus/Local/ILocalEventHandler.cs | 13 ++ .../Abp/EventBus/Local/NullLocalEventBus.cs | 4 +- .../Local/EventBus_MultipleHandle_Test.cs | 4 +- .../Local/MySimpleEventDataHandler.cs | 2 +- .../Local/MySimpleTransientEventHandler.cs | 2 +- 16 files changed, 162 insertions(+), 179 deletions(-) rename framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/{LocalEventBusOptions.cs => DistributedEventBusOptions.cs} (100%) rename framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/{NullLocalEventBus.cs => NullDistributedEventBus.cs} (92%) delete mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs delete mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventHandler.cs 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 b236e6f7e4..dc4edea364 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 @@ -126,7 +126,7 @@ namespace Volo.Abp.Domain.Entities.Events } } - protected virtual async Task TriggerEventWithEntity(IEventPublisher eventPublisher, Type genericEventType, object entity, bool triggerInCurrentUnitOfWork) + protected virtual async Task TriggerEventWithEntity(IEventBus eventPublisher, Type genericEventType, object entity, bool triggerInCurrentUnitOfWork) { var entityType = ProxyHelper.UnProxy(entity).GetType(); var eventType = genericEventType.MakeGenericType(entityType); diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs index f17fd05446..3729b48b61 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; -using System.Linq; using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Local; using Volo.Abp.Modularity; @@ -23,15 +22,14 @@ namespace Volo.Abp.EventBus services.OnRegistred(context => { - if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IEventHandler<>))) + if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(ILocalEventHandler<>))) { localHandlers.Add(context.ImplementationType); } - //TODO: Distrbiuted event bus is disabled since it's not properly working yet for v0.8 release - //else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) - //{ - // distributedHandlers.Add(context.ImplementationType); - //} + else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) + { + distributedHandlers.Add(context.ImplementationType); + } }); services.Configure(options => diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs index 995ce39356..a8763edd89 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs @@ -5,11 +5,11 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.EventBus { /// - /// This event handler is an adapter to be able to use an action as implementation. + /// This event handler is an adapter to be able to use an action as implementation. /// /// Event type public class ActionEventHandler : - IEventHandler, + ILocalEventHandler, ITransientDependency { /// diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/DistributedEventBusOptions.cs similarity index 100% rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/DistributedEventBusOptions.cs 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 0f609c6aaa..cfbf7b0211 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 @@ -53,7 +53,7 @@ namespace Volo.Abp.EventBus.Distributed return _localEventBus.Subscribe(action); } - public IDisposable Subscribe(IEventHandler handler) where TEvent : class + public IDisposable Subscribe(ILocalEventHandler handler) where TEvent : class { return _localEventBus.Subscribe(handler); } @@ -83,7 +83,7 @@ namespace Volo.Abp.EventBus.Distributed _localEventBus.Unsubscribe(action); } - public void Unsubscribe(IEventHandler handler) where TEvent : class + public void Unsubscribe(ILocalEventHandler handler) where TEvent : class { _localEventBus.Unsubscribe(handler); } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullLocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullDistributedEventBus.cs similarity index 92% rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullLocalEventBus.cs rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullDistributedEventBus.cs index cc59b9a24b..8df6455d52 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullLocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullDistributedEventBus.cs @@ -17,7 +17,7 @@ namespace Volo.Abp.EventBus.Distributed return NullDisposable.Instance; } - public IDisposable Subscribe(IEventHandler handler) where TEvent : class + public IDisposable Subscribe(ILocalEventHandler handler) where TEvent : class { return NullDisposable.Instance; } @@ -47,7 +47,7 @@ namespace Volo.Abp.EventBus.Distributed } - public void Unsubscribe(IEventHandler handler) where TEvent : class + public void Unsubscribe(ILocalEventHandler handler) where TEvent : class { } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs index e0c0340f15..04f5b0f79d 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Volo.Abp.EventBus.Distributed; using Volo.Abp.Reflection; -namespace Volo.Abp.EventBus.Local +namespace Volo.Abp.EventBus { public abstract class EventBusBase : IEventBus { @@ -19,7 +19,7 @@ namespace Volo.Abp.EventBus.Local } /// - public virtual IDisposable Subscribe(IEventHandler handler) where TEvent : class + public virtual IDisposable Subscribe(ILocalEventHandler handler) where TEvent : class { return Subscribe(typeof(TEvent), handler); } @@ -49,7 +49,7 @@ namespace Volo.Abp.EventBus.Local public abstract void Unsubscribe(Func action) where TEvent : class; /// - public virtual void Unsubscribe(IEventHandler handler) where TEvent : class + public virtual void Unsubscribe(ILocalEventHandler handler) where TEvent : class { Unsubscribe(typeof(TEvent), handler); } @@ -138,12 +138,12 @@ namespace Volo.Abp.EventBus.Local { var handlerType = eventHandlerWrapper.EventHandler.GetType(); - if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IEventHandler<>))) + if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(ILocalEventHandler<>))) { - var method = typeof(IEventHandler<>) //TODO: to a static field + var method = typeof(ILocalEventHandler<>) .MakeGenericType(eventType) .GetMethod( - nameof(IEventHandler.HandleEventAsync), + nameof(ILocalEventHandler.HandleEventAsync), new[] { eventType } ); @@ -151,7 +151,7 @@ namespace Volo.Abp.EventBus.Local } else if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler<>))) { - var method = typeof(IDistributedEventHandler<>) //TODO: to a static field + var method = typeof(IDistributedEventHandler<>) .MakeGenericType(eventType) .GetMethod( nameof(IDistributedEventHandler.HandleEventAsync), 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 a5d1c27a15..0ffd4cad91 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs @@ -1,7 +1,128 @@ +using System; +using System.Threading.Tasks; + namespace Volo.Abp.EventBus { - public interface IEventBus : IEventSubscriber, IEventPublisher + public interface IEventBus { + /// + /// 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); + + /// + /// 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(ILocalEventHandler 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(ILocalEventHandler 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/IEventHandler.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs index ca3993c6bc..6397813637 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs @@ -1,26 +1,13 @@ -using System.Threading.Tasks; +using Volo.Abp.EventBus.Distributed; namespace Volo.Abp.EventBus { /// /// Undirect base interface for all event handlers. - /// Implement instead of this one. + /// Implement or instead of this one. /// public interface IEventHandler { } - - /// - /// Defines an interface of a class that handles events asynchrounously of type . - /// - /// Event type to handle - public interface IEventHandler : IEventHandler - { - /// - /// Handler handles the event by implementing this method. - /// - /// Event data - Task HandleEventAsync(TEvent 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 deleted file mode 100644 index 0192522304..0000000000 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index 86330433ea..0000000000 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs +++ /dev/null @@ -1,111 +0,0 @@ -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/ILocalEventHandler.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventHandler.cs new file mode 100644 index 0000000000..e3501294be --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventHandler.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.EventBus +{ + public interface ILocalEventHandler : IEventHandler + { + /// + /// Handler handles the event by implementing this method. + /// + /// Event data + Task HandleEventAsync(TEvent eventData); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs index 494db3edd1..5c74af2195 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs @@ -17,7 +17,7 @@ namespace Volo.Abp.EventBus.Local return NullDisposable.Instance; } - public IDisposable Subscribe(IEventHandler handler) where TEvent : class + public IDisposable Subscribe(ILocalEventHandler handler) where TEvent : class { return NullDisposable.Instance; } @@ -47,7 +47,7 @@ namespace Volo.Abp.EventBus.Local } - public void Unsubscribe(IEventHandler handler) where TEvent : class + public void Unsubscribe(ILocalEventHandler handler) where TEvent : class { } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs index b8dfe43e17..d20692fa01 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs @@ -31,8 +31,8 @@ namespace Volo.Abp.EventBus.Local } public class MyEventHandler : - IEventHandler>, - IEventHandler> + ILocalEventHandler>, + ILocalEventHandler> { public int EntityChangedEventCount { get; set; } public int EntityCreatedEventCount { get; set; } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs index ac725f2ae3..66a48a60bb 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs @@ -3,7 +3,7 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.EventBus.Local { - public class MySimpleEventDataHandler : IEventHandler, ISingletonDependency + public class MySimpleEventDataHandler : ILocalEventHandler, ISingletonDependency { public int TotalData { get; private set; } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs index 6adf34b51b..2c33c8e32c 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; namespace Volo.Abp.EventBus.Local { - public class MySimpleTransientEventHandler : IEventHandler, IDisposable + public class MySimpleTransientEventHandler : ILocalEventHandler, IDisposable { public static int HandleCount { get; set; }