Browse Source

Refactored eventbus

pull/625/head
Halil ibrahim Kalkan 8 years ago
parent
commit
d678b5f29d
  1. 2
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/EntityChangeEventHelper.cs
  2. 12
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs
  3. 4
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs
  4. 0
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/DistributedEventBusOptions.cs
  5. 4
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs
  6. 4
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullDistributedEventBus.cs
  7. 14
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
  8. 123
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs
  9. 17
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs
  10. 25
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs
  11. 111
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs
  12. 13
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/ILocalEventHandler.cs
  13. 4
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs
  14. 4
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_MultipleHandle_Test.cs
  15. 2
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleEventDataHandler.cs
  16. 2
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/MySimpleTransientEventHandler.cs

2
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 entityType = ProxyHelper.UnProxy(entity).GetType();
var eventType = genericEventType.MakeGenericType(entityType); var eventType = genericEventType.MakeGenericType(entityType);

12
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs

@ -1,7 +1,6 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus.Local; using Volo.Abp.EventBus.Local;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -23,15 +22,14 @@ namespace Volo.Abp.EventBus
services.OnRegistred(context => services.OnRegistred(context =>
{ {
if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IEventHandler<>))) if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(ILocalEventHandler<>)))
{ {
localHandlers.Add(context.ImplementationType); 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<>)))
//else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) {
//{ distributedHandlers.Add(context.ImplementationType);
// distributedHandlers.Add(context.ImplementationType); }
//}
}); });
services.Configure<LocalEventBusOptions>(options => services.Configure<LocalEventBusOptions>(options =>

4
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs

@ -5,11 +5,11 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EventBus namespace Volo.Abp.EventBus
{ {
/// <summary> /// <summary>
/// This event handler is an adapter to be able to use an action as <see cref="IEventHandler{TEvent}"/> implementation. /// This event handler is an adapter to be able to use an action as <see cref="ILocalEventHandler{TEvent}"/> implementation.
/// </summary> /// </summary>
/// <typeparam name="TEvent">Event type</typeparam> /// <typeparam name="TEvent">Event type</typeparam>
public class ActionEventHandler<TEvent> : public class ActionEventHandler<TEvent> :
IEventHandler<TEvent>, ILocalEventHandler<TEvent>,
ITransientDependency ITransientDependency
{ {
/// <summary> /// <summary>

0
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs → framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/DistributedEventBusOptions.cs

4
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs

@ -53,7 +53,7 @@ namespace Volo.Abp.EventBus.Distributed
return _localEventBus.Subscribe(action); return _localEventBus.Subscribe(action);
} }
public IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public IDisposable Subscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
return _localEventBus.Subscribe(handler); return _localEventBus.Subscribe(handler);
} }
@ -83,7 +83,7 @@ namespace Volo.Abp.EventBus.Distributed
_localEventBus.Unsubscribe(action); _localEventBus.Unsubscribe(action);
} }
public void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public void Unsubscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
_localEventBus.Unsubscribe(handler); _localEventBus.Unsubscribe(handler);
} }

4
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullLocalEventBus.cs → framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/NullDistributedEventBus.cs

@ -17,7 +17,7 @@ namespace Volo.Abp.EventBus.Distributed
return NullDisposable.Instance; return NullDisposable.Instance;
} }
public IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public IDisposable Subscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
return NullDisposable.Instance; return NullDisposable.Instance;
} }
@ -47,7 +47,7 @@ namespace Volo.Abp.EventBus.Distributed
} }
public void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public void Unsubscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
} }

14
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.EventBus.Distributed;
using Volo.Abp.Reflection; using Volo.Abp.Reflection;
namespace Volo.Abp.EventBus.Local namespace Volo.Abp.EventBus
{ {
public abstract class EventBusBase : IEventBus public abstract class EventBusBase : IEventBus
{ {
@ -19,7 +19,7 @@ namespace Volo.Abp.EventBus.Local
} }
/// <inheritdoc/> /// <inheritdoc/>
public virtual IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public virtual IDisposable Subscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
return Subscribe(typeof(TEvent), handler); return Subscribe(typeof(TEvent), handler);
} }
@ -49,7 +49,7 @@ namespace Volo.Abp.EventBus.Local
public abstract void Unsubscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class; public abstract void Unsubscribe<TEvent>(Func<TEvent, Task> action) where TEvent : class;
/// <inheritdoc/> /// <inheritdoc/>
public virtual void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public virtual void Unsubscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
Unsubscribe(typeof(TEvent), handler); Unsubscribe(typeof(TEvent), handler);
} }
@ -138,12 +138,12 @@ namespace Volo.Abp.EventBus.Local
{ {
var handlerType = eventHandlerWrapper.EventHandler.GetType(); 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) .MakeGenericType(eventType)
.GetMethod( .GetMethod(
nameof(IEventHandler<object>.HandleEventAsync), nameof(ILocalEventHandler<object>.HandleEventAsync),
new[] { eventType } new[] { eventType }
); );
@ -151,7 +151,7 @@ namespace Volo.Abp.EventBus.Local
} }
else if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler<>))) else if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler<>)))
{ {
var method = typeof(IDistributedEventHandler<>) //TODO: to a static field var method = typeof(IDistributedEventHandler<>)
.MakeGenericType(eventType) .MakeGenericType(eventType)
.GetMethod( .GetMethod(
nameof(IDistributedEventHandler<object>.HandleEventAsync), nameof(IDistributedEventHandler<object>.HandleEventAsync),

123
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs

@ -1,7 +1,128 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.EventBus namespace Volo.Abp.EventBus
{ {
public interface IEventBus : IEventSubscriber, IEventPublisher public interface IEventBus
{ {
/// <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);
/// <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>(ILocalEventHandler<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>(ILocalEventHandler<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);
} }
} }

17
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 namespace Volo.Abp.EventBus
{ {
/// <summary> /// <summary>
/// Undirect base interface for all event handlers. /// Undirect base interface for all event handlers.
/// Implement <see cref="IEventHandler{TEvent}"/> instead of this one. /// Implement <see cref="ILocalEventHandler{TEvent}"/> or <see cref="IDistributedEventHandler{TEvent}"/> instead of this one.
/// </summary> /// </summary>
public interface IEventHandler public interface IEventHandler
{ {
} }
/// <summary>
/// Defines an interface of a class that handles events asynchrounously of type <see cref="IEventHandler{TEvent}"/>.
/// </summary>
/// <typeparam name="TEvent">Event type to handle</typeparam>
public interface IEventHandler<in TEvent> : IEventHandler
{
/// <summary>
/// Handler handles the event by implementing this method.
/// </summary>
/// <param name="eventData">Event data</param>
Task HandleEventAsync(TEvent eventData);
}
} }

25
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventPublisher.cs

@ -1,25 +0,0 @@
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);
}
}

111
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventSubscriber.cs

@ -1,111 +0,0 @@
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);
}
}

13
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<in TEvent> : IEventHandler
{
/// <summary>
/// Handler handles the event by implementing this method.
/// </summary>
/// <param name="eventData">Event data</param>
Task HandleEventAsync(TEvent eventData);
}
}

4
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/NullLocalEventBus.cs

@ -17,7 +17,7 @@ namespace Volo.Abp.EventBus.Local
return NullDisposable.Instance; return NullDisposable.Instance;
} }
public IDisposable Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public IDisposable Subscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
return NullDisposable.Instance; return NullDisposable.Instance;
} }
@ -47,7 +47,7 @@ namespace Volo.Abp.EventBus.Local
} }
public void Unsubscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class public void Unsubscribe<TEvent>(ILocalEventHandler<TEvent> handler) where TEvent : class
{ {
} }

4
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 : public class MyEventHandler :
IEventHandler<EntityChangedEventData<MyEntity>>, ILocalEventHandler<EntityChangedEventData<MyEntity>>,
IEventHandler<EntityCreatedEventData<MyEntity>> ILocalEventHandler<EntityCreatedEventData<MyEntity>>
{ {
public int EntityChangedEventCount { get; set; } public int EntityChangedEventCount { get; set; }
public int EntityCreatedEventCount { get; set; } public int EntityCreatedEventCount { get; set; }

2
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 namespace Volo.Abp.EventBus.Local
{ {
public class MySimpleEventDataHandler : IEventHandler<MySimpleEventData>, ISingletonDependency public class MySimpleEventDataHandler : ILocalEventHandler<MySimpleEventData>, ISingletonDependency
{ {
public int TotalData { get; private set; } public int TotalData { get; private set; }

2
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 namespace Volo.Abp.EventBus.Local
{ {
public class MySimpleTransientEventHandler : IEventHandler<MySimpleEventData>, IDisposable public class MySimpleTransientEventHandler : ILocalEventHandler<MySimpleEventData>, IDisposable
{ {
public static int HandleCount { get; set; } public static int HandleCount { get; set; }

Loading…
Cancel
Save