Browse Source

Implement IocEventHandlerFactory. Changed namespaces for eventbus.

pull/216/head
Halil İbrahim Kalkan 9 years ago
parent
commit
4806c55778
  1. 1
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs
  2. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs
  3. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/AsyncActionEventHandler.cs
  4. 91
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBus.cs
  5. 1
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusOptions.cs
  6. 22
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerWithDisposeAction.cs
  7. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/FactoryUnregistrar.cs
  8. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IAsyncEventHandler.cs
  9. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs
  10. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs
  11. 9
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerDisposeWrapper.cs
  12. 11
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs
  13. 25
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs
  14. 2
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs
  15. 14
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs
  16. 23
      src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs
  17. 1
      test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_MultipleHandle_Test.cs
  18. 1
      test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleAsyncEventDataHandler.cs
  19. 1
      test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleEventDataHandler.cs
  20. 1
      test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientAsyncEventHandler.cs
  21. 1
      test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientEventHandler.cs

1
src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EventBus.Handlers;
using Volo.Abp.Modularity;
namespace Volo.Abp.EventBus

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Handlers/Internals/ActionEventHandler.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/ActionEventHandler.cs

@ -1,7 +1,7 @@
using System;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EventBus.Handlers.Internals
namespace Volo.Abp.EventBus
{
/// <summary>
/// This event handler is an adapter to be able to use an action as <see cref="IEventHandler{TEventData}"/> implementation.

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Handlers/Internals/AsyncActionEventHandler.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/AsyncActionEventHandler.cs

@ -2,7 +2,7 @@ using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EventBus.Handlers.Internals
namespace Volo.Abp.EventBus
{
/// <summary>
/// This event handler is an adapter to be able to use an action as <see cref="IAsyncEventHandler{TEventData}"/> implementation.

91
src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBus.cs

@ -10,10 +10,6 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Factories;
using Volo.Abp.EventBus.Factories.Internals;
using Volo.Abp.EventBus.Handlers;
using Volo.Abp.EventBus.Handlers.Internals;
using Volo.Abp.Threading;
namespace Volo.Abp.EventBus
@ -61,14 +57,14 @@ namespace Volo.Abp.EventBus
var genericArgs = @interface.GetGenericArguments();
if (genericArgs.Length == 1)
{
Register(genericArgs[0], new IocHandlerFactory(serviceProvider, handler));
Register(genericArgs[0], new IocEventHandlerFactory(serviceProvider, handler));
}
}
}
}
/// <inheritdoc/>
public IDisposable Register<TEventData>(Action<TEventData> action)
public IDisposable Register<TEventData>(Action<TEventData> action)
where TEventData : class
{
return Register(typeof(TEventData), new ActionEventHandler<TEventData>(action));
@ -351,68 +347,53 @@ namespace Volo.Abp.EventBus
private void TriggerHandlingException(IEventHandlerFactory handlerFactory, Type eventType, object eventData, List<Exception> exceptions)
{
var eventHandler = handlerFactory.GetHandler();
try
using (var eventHandlerWrapper = handlerFactory.GetHandler())
{
if (eventHandler == null)
try
{
throw new ArgumentNullException($"Registered event handler for event type {eventType.Name} is null!");
}
var handlerType = typeof(IEventHandler<>).MakeGenericType(eventType);
var handlerType = typeof(IEventHandler<>).MakeGenericType(eventType);
var method = handlerType.GetMethod(
"HandleEvent",
new[] { eventType }
);
var method = handlerType.GetMethod(
"HandleEvent",
new[] { eventType }
);
method.Invoke(eventHandler, new[] { eventData });
}
catch (TargetInvocationException ex)
{
exceptions.Add(ex.InnerException);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
finally
{
handlerFactory.ReleaseHandler(eventHandler);
method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData });
}
catch (TargetInvocationException ex)
{
exceptions.Add(ex.InnerException);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
}
private async Task TriggerAsyncHandlingException(IEventHandlerFactory asyncHandlerFactory, Type eventType, object eventData, List<Exception> exceptions)
{
var asyncEventHandler = asyncHandlerFactory.GetHandler();
try
using (var eventHandlerWrapper = asyncHandlerFactory.GetHandler())
{
if (asyncEventHandler == null)
try
{
throw new ArgumentNullException($"Registered async event handler for event type {eventType.Name} is null!");
}
var asyncHandlerType = typeof(IAsyncEventHandler<>).MakeGenericType(eventType);
var asyncHandlerType = typeof(IAsyncEventHandler<>).MakeGenericType(eventType);
var method = asyncHandlerType.GetMethod(
"HandleEventAsync",
new[] { eventType }
);
var method = asyncHandlerType.GetMethod(
"HandleEventAsync",
new[] { eventType }
);
await (Task)method.Invoke(asyncEventHandler, new[] { eventData });
}
catch (TargetInvocationException ex)
{
exceptions.Add(ex.InnerException);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
finally
{
asyncHandlerFactory.ReleaseHandler(asyncEventHandler);
await (Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData });
}
catch (TargetInvocationException ex)
{
exceptions.Add(ex.InnerException);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
}

1
src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusOptions.cs

@ -1,5 +1,4 @@
using Volo.Abp.Collections;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

22
src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerWithDisposeAction.cs

@ -0,0 +1,22 @@
using System;
namespace Volo.Abp.EventBus
{
public class EventHandlerDisposeWrapper : IEventHandlerDisposeWrapper
{
public IEventHandler EventHandler { get; }
private readonly Action _disposeAction;
public EventHandlerDisposeWrapper(IEventHandler eventHandler, Action disposeAction = null)
{
_disposeAction = disposeAction;
EventHandler = eventHandler;
}
public void Dispose()
{
_disposeAction?.Invoke();
}
}
}

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Factories/Internals/FactoryUnregistrar.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/FactoryUnregistrar.cs

@ -1,6 +1,6 @@
using System;
namespace Volo.Abp.EventBus.Factories.Internals
namespace Volo.Abp.EventBus
{
/// <summary>
/// Used to unregister a <see cref="IEventHandlerFactory"/> on <see cref="Dispose"/> method.

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Handlers/IAsyncEventHandler.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/IAsyncEventHandler.cs

@ -1,6 +1,6 @@
using System.Threading.Tasks;
namespace Volo.Abp.EventBus.Handlers
namespace Volo.Abp.EventBus
{
/// <summary>
/// Defines an interface of a class that handles events asynchrounously of type <see cref="IAsyncEventHandler{TEventData}"/>.

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs

@ -1,7 +1,5 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.EventBus.Factories;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Handlers/IEventHandler.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandler.cs

@ -1,4 +1,4 @@
namespace Volo.Abp.EventBus.Handlers
namespace Volo.Abp.EventBus
{
/// <summary>
/// Undirect base interface for all event handlers.

9
src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerDisposeWrapper.cs

@ -0,0 +1,9 @@
using System;
namespace Volo.Abp.EventBus
{
public interface IEventHandlerDisposeWrapper : IDisposable
{
IEventHandler EventHandler { get; }
}
}

11
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Factories/IEventHandlerFactory.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs

@ -1,7 +1,6 @@
using System;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus.Factories
namespace Volo.Abp.EventBus
{
/// <summary>
/// Defines an interface for factories those are responsible to create/get and release of event handlers.
@ -12,18 +11,12 @@ namespace Volo.Abp.EventBus.Factories
/// Gets an event handler.
/// </summary>
/// <returns>The event handler</returns>
IEventHandler GetHandler();
IEventHandlerDisposeWrapper GetHandler();
/// <summary>
/// Gets type of the handler (without creating an instance).
/// </summary>
/// <returns></returns>
Type GetHandlerType();
/// <summary>
/// Releases an event handler.
/// </summary>
/// <param name="handler">Handle to be released</param>
void ReleaseHandler(IEventHandler handler);
}
}

25
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Factories/IocHandlerFactory.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs

@ -1,22 +1,21 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus.Factories
namespace Volo.Abp.EventBus
{
/// <summary>
/// This <see cref="IEventHandlerFactory"/> implementation is used to get/release
/// handlers using Ioc.
/// </summary>
public class IocHandlerFactory : IEventHandlerFactory
public class IocEventHandlerFactory : IEventHandlerFactory
{
public Type HandlerType { get; }
private readonly IServiceProvider _iocResolver;
private readonly IServiceProvider _serviceProvider;
public IocHandlerFactory(IServiceProvider iocResolver, Type handlerType)
public IocEventHandlerFactory(IServiceProvider serviceProvider, Type handlerType)
{
_iocResolver = iocResolver;
_serviceProvider = serviceProvider;
HandlerType = handlerType;
}
@ -24,20 +23,18 @@ namespace Volo.Abp.EventBus.Factories
/// Resolves handler object from Ioc container.
/// </summary>
/// <returns>Resolved handler object</returns>
public IEventHandler GetHandler()
public IEventHandlerDisposeWrapper GetHandler()
{
return (IEventHandler)_iocResolver.GetRequiredService(HandlerType);
var scope = _serviceProvider.CreateScope();
return new EventHandlerDisposeWrapper(
(IEventHandler) scope.ServiceProvider.GetRequiredService(HandlerType),
() => scope.Dispose()
);
}
public Type GetHandlerType()
{
return HandlerType;
}
public void ReleaseHandler(IEventHandler handler)
{
//TODO: Scope!!!
//_iocResolver.Release(handler);
}
}
}

2
src/Volo.Abp.EventBus/Volo/Abp/EventBus/NullEventBus.cs

@ -1,7 +1,5 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.EventBus.Factories;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

14
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Factories/Internals/SingleInstanceHandlerFactory.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs

@ -1,8 +1,7 @@
using System;
using Volo.Abp.DynamicProxy;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus.Factories.Internals
namespace Volo.Abp.EventBus
{
/// <summary>
/// This <see cref="IEventHandlerFactory"/> implementation is used to handle events
@ -16,7 +15,7 @@ namespace Volo.Abp.EventBus.Factories.Internals
/// <summary>
/// The event handler instance.
/// </summary>
public IEventHandler HandlerInstance { get; private set; }
public IEventHandler HandlerInstance { get; }
/// <summary>
///
@ -27,19 +26,14 @@ namespace Volo.Abp.EventBus.Factories.Internals
HandlerInstance = handler;
}
public IEventHandler GetHandler()
public IEventHandlerDisposeWrapper GetHandler()
{
return HandlerInstance;
return new EventHandlerDisposeWrapper(HandlerInstance);
}
public Type GetHandlerType()
{
return ProxyHelper.UnProxy(HandlerInstance).GetType();
}
public void ReleaseHandler(IEventHandler handler)
{
}
}
}

23
src/Volo.Abp.EventBus/Volo/Abp/EventBus/Factories/Internals/TransientEventHandlerFactory.cs → src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs

@ -1,7 +1,6 @@
using System;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus.Factories.Internals
namespace Volo.Abp.EventBus
{
/// <summary>
/// This <see cref="IEventHandlerFactory"/> implementation is used to handle events
@ -17,26 +16,18 @@ namespace Volo.Abp.EventBus.Factories.Internals
/// Creates a new instance of the handler object.
/// </summary>
/// <returns>The handler object</returns>
public IEventHandler GetHandler()
public IEventHandlerDisposeWrapper GetHandler()
{
return new THandler();
var handler = new THandler();
return new EventHandlerDisposeWrapper(
handler,
() => (handler as IDisposable)?.Dispose()
);
}
public Type GetHandlerType()
{
return typeof(THandler);
}
/// <summary>
/// Disposes the handler object if it's <see cref="IDisposable"/>. Does nothing if it's not.
/// </summary>
/// <param name="handler">Handler to be released</param>
public void ReleaseHandler(IEventHandler handler)
{
if (handler is IDisposable)
{
(handler as IDisposable).Dispose();
}
}
}
}

1
test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/EventBus_MultipleHandle_Test.cs

@ -2,7 +2,6 @@
using Shouldly;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus.Handlers;
using Xunit;
namespace Volo.Abp.EventBus

1
test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleAsyncEventDataHandler.cs

@ -1,6 +1,5 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

1
test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleEventDataHandler.cs

@ -1,5 +1,4 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

1
test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientAsyncEventHandler.cs

@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

1
test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/MySimpleTransientEventHandler.cs

@ -1,5 +1,4 @@
using System;
using Volo.Abp.EventBus.Handlers;
namespace Volo.Abp.EventBus
{

Loading…
Cancel
Save