Code re-factorings and formatting in EventBus projects

This commit is contained in:
Rafsanul Hasan 2018-09-09 05:16:41 +06:00
parent 80eb943d78
commit 086302f5f1
No known key found for this signature in database
GPG Key ID: FC57FD2D87BE60DD
7 changed files with 191 additions and 218 deletions

View File

@ -1,13 +1,9 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {
public interface IDynamicIntegrationEventHandler public interface IDynamicIntegrationEventHandler
{ {
Task Handle(dynamic eventData); Task Handle(dynamic eventData);
} }
} }

View File

@ -1,24 +1,23 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
using System;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {
public interface IEventBus using IntegrationEvent = Events.IntegrationEvent;
{
void Publish(IntegrationEvent @event);
void Subscribe<T, TH>() public interface IEventBus
where T : IntegrationEvent {
where TH : IIntegrationEventHandler<T>; void Publish(IntegrationEvent @event);
void SubscribeDynamic<TH>(string eventName) void Subscribe<T, TH>()
where TH : IDynamicIntegrationEventHandler; where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>;
void UnsubscribeDynamic<TH>(string eventName) void SubscribeDynamic<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler; where TH : IDynamicIntegrationEventHandler;
void Unsubscribe<T, TH>() void UnsubscribeDynamic<TH>(string eventName)
where TH : IIntegrationEventHandler<T> where TH : IDynamicIntegrationEventHandler;
where T : IntegrationEvent;
} void Unsubscribe<T, TH>()
where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent;
}
} }

View File

@ -1,15 +1,16 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Task = System.Threading.Tasks.Task;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {
public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler using IntegrationEvent = Events.IntegrationEvent;
where TIntegrationEvent: IntegrationEvent
{
Task Handle(TIntegrationEvent @event);
}
public interface IIntegrationEventHandler public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler
{ where TIntegrationEvent : IntegrationEvent
} {
Task Handle(TIntegrationEvent @event);
}
public interface IIntegrationEventHandler
{
}
} }

View File

@ -1,16 +1,17 @@
using System; using DateTime = System.DateTime;
using Guid = System.Guid;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events
{ {
public class IntegrationEvent public class IntegrationEvent
{ {
public IntegrationEvent() public IntegrationEvent()
{ {
Id = Guid.NewGuid(); Id = Guid.NewGuid();
CreationDate = DateTime.UtcNow; CreationDate = DateTime.UtcNow;
} }
public Guid Id { get; } public Guid Id { get; }
public DateTime CreationDate { get; } public DateTime CreationDate { get; }
} }
} }

View File

@ -2,33 +2,34 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using static Microsoft.eShopOnContainers.BuildingBlocks.EventBus.InMemoryEventBusSubscriptionsManager;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
{ {
public interface IEventBusSubscriptionsManager using static InMemoryEventBusSubscriptionsManager;
{
bool IsEmpty { get; }
event EventHandler<string> OnEventRemoved;
void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler;
void AddSubscription<T, TH>() public interface IEventBusSubscriptionsManager
where T : IntegrationEvent {
where TH : IIntegrationEventHandler<T>; bool IsEmpty { get; }
event EventHandler<string> OnEventRemoved;
void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler;
void RemoveSubscription<T, TH>() void AddSubscription<T, TH>()
where TH : IIntegrationEventHandler<T> where T : IntegrationEvent
where T : IntegrationEvent; where TH : IIntegrationEventHandler<T>;
void RemoveDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler;
bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent; void RemoveSubscription<T, TH>()
bool HasSubscriptionsForEvent(string eventName); where TH : IIntegrationEventHandler<T>
Type GetEventTypeByName(string eventName); where T : IntegrationEvent;
void Clear(); void RemoveDynamicSubscription<TH>(string eventName)
IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent; where TH : IDynamicIntegrationEventHandler;
IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName);
string GetEventKey<T>(); bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent;
} bool HasSubscriptionsForEvent(string eventName);
Type GetEventTypeByName(string eventName);
void Clear();
IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent;
IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName);
string GetEventKey<T>();
}
} }

View File

@ -3,160 +3,135 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
{ {
public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
{ {
private readonly Dictionary<string, List<SubscriptionInfo>> _handlers; private readonly Dictionary<string, List<SubscriptionInfo>> _handlers;
private readonly List<Type> _eventTypes; private readonly List<Type> _eventTypes;
public event EventHandler<string> OnEventRemoved; public event EventHandler<string> OnEventRemoved;
public InMemoryEventBusSubscriptionsManager() public InMemoryEventBusSubscriptionsManager()
{ {
_handlers = new Dictionary<string, List<SubscriptionInfo>>(); _handlers = new Dictionary<string, List<SubscriptionInfo>>();
_eventTypes = new List<Type>(); _eventTypes = new List<Type>();
} }
public bool IsEmpty => !_handlers.Keys.Any(); public bool IsEmpty => !_handlers.Keys.Any();
public void Clear() => _handlers.Clear(); public void Clear() => _handlers.Clear();
public void AddDynamicSubscription<TH>(string eventName) public void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler where TH : IDynamicIntegrationEventHandler
{ => DoAddSubscription(typeof(TH), eventName, isDynamic: true);
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
}
public void AddSubscription<T, TH>() public void AddSubscription<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
{ {
var eventName = GetEventKey<T>(); var eventName = GetEventKey<T>();
DoAddSubscription(typeof(TH), eventName, isDynamic: false); DoAddSubscription(typeof(TH), eventName, isDynamic: false);
_eventTypes.Add(typeof(T)); _eventTypes.Add(typeof(T));
} }
private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic)
{ {
if (!HasSubscriptionsForEvent(eventName)) if (!HasSubscriptionsForEvent(eventName))
{ {
_handlers.Add(eventName, new List<SubscriptionInfo>()); _handlers.Add(eventName, new List<SubscriptionInfo>());
} }
if (_handlers[eventName].Any(s => s.HandlerType == handlerType)) if (_handlers[eventName].Any(s => s.HandlerType == handlerType))
{ {
throw new ArgumentException( throw new ArgumentException(
$"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType)); $"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType));
} }
if (isDynamic) if (isDynamic)
{ {
_handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType)); _handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType));
} }
else else
{ {
_handlers[eventName].Add(SubscriptionInfo.Typed(handlerType)); _handlers[eventName].Add(SubscriptionInfo.Typed(handlerType));
} }
} }
public void RemoveDynamicSubscription<TH>(string eventName) public void RemoveDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler where TH : IDynamicIntegrationEventHandler
{ {
var handlerToRemove = FindDynamicSubscriptionToRemove<TH>(eventName); var handlerToRemove = FindDynamicSubscriptionToRemove<TH>(eventName);
DoRemoveHandler(eventName, handlerToRemove); DoRemoveHandler(eventName, handlerToRemove);
} }
public void RemoveSubscription<T, TH>() public void RemoveSubscription<T, TH>()
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent where T : IntegrationEvent
{ {
var handlerToRemove = FindSubscriptionToRemove<T, TH>(); var handlerToRemove = FindSubscriptionToRemove<T, TH>();
var eventName = GetEventKey<T>(); var eventName = GetEventKey<T>();
DoRemoveHandler(eventName, handlerToRemove); DoRemoveHandler(eventName, handlerToRemove);
} }
private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove) private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove)
{ {
if (subsToRemove != null) if (subsToRemove != null)
{ {
_handlers[eventName].Remove(subsToRemove); _handlers[eventName].Remove(subsToRemove);
if (!_handlers[eventName].Any()) if (!_handlers[eventName].Any())
{ {
_handlers.Remove(eventName); _handlers.Remove(eventName);
var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName);
if (eventType != null) if (eventType != null)
{ {
_eventTypes.Remove(eventType); _eventTypes.Remove(eventType);
} }
RaiseOnEventRemoved(eventName); RaiseOnEventRemoved(eventName);
} }
} }
} }
public IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent public IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent
{ => GetHandlersForEvent(GetEventKey<T>());
var key = GetEventKey<T>(); public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName];
return GetHandlersForEvent(key);
}
public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName];
private void RaiseOnEventRemoved(string eventName) private void RaiseOnEventRemoved(string eventName)
{ {
var handler = OnEventRemoved; var handler = OnEventRemoved;
if (handler != null) if (handler != null)
{ {
OnEventRemoved(this, eventName); OnEventRemoved(this, eventName);
} }
} }
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName) private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler where TH : IDynamicIntegrationEventHandler
{ => DoFindSubscriptionToRemove(eventName, typeof(TH));
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo FindSubscriptionToRemove<T, TH>() private SubscriptionInfo FindSubscriptionToRemove<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
{ => DoFindSubscriptionToRemove(GetEventKey<T>(), typeof(TH));
var eventName = GetEventKey<T>();
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType) private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType)
{ => !HasSubscriptionsForEvent(eventName) ? null : _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType);
if (!HasSubscriptionsForEvent(eventName))
{
return null;
}
return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType); public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent
=> HasSubscriptionsForEvent(GetEventKey<T>());
public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName);
} public Type GetEventTypeByName(string eventName) => _eventTypes.SingleOrDefault(t => t.Name == eventName);
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent public string GetEventKey<T>()
{ => typeof(T).Name;
var key = GetEventKey<T>(); }
return HasSubscriptionsForEvent(key);
}
public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName);
public Type GetEventTypeByName(string eventName) => _eventTypes.SingleOrDefault(t => t.Name == eventName);
public string GetEventKey<T>()
{
return typeof(T).Name;
}
}
} }

View File

@ -2,27 +2,27 @@
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
{ {
public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
{ {
public class SubscriptionInfo public class SubscriptionInfo
{ {
public bool IsDynamic { get; } public bool IsDynamic { get; }
public Type HandlerType{ get; } public Type HandlerType { get; }
private SubscriptionInfo(bool isDynamic, Type handlerType) private SubscriptionInfo(bool isDynamic, Type handlerType)
{ {
IsDynamic = isDynamic; IsDynamic = isDynamic;
HandlerType = handlerType; HandlerType = handlerType;
} }
public static SubscriptionInfo Dynamic(Type handlerType) public static SubscriptionInfo Dynamic(Type handlerType)
{ {
return new SubscriptionInfo(true, handlerType); return new SubscriptionInfo(true, handlerType);
} }
public static SubscriptionInfo Typed(Type handlerType) public static SubscriptionInfo Typed(Type handlerType)
{ {
return new SubscriptionInfo(false, handlerType); return new SubscriptionInfo(false, handlerType);
} }
} }
} }
} }