From 086302f5f14d1e9c4d253f498f527ed5ae3aa91d Mon Sep 17 00:00:00 2001 From: Rafsanul Hasan Date: Sun, 9 Sep 2018 05:16:41 +0600 Subject: [PATCH] Code re-factorings and formatting in EventBus projects --- .../IDynamicIntegrationEventHandler.cs | 14 +- .../EventBus/Abstractions/IEventBus.cs | 35 ++- .../Abstractions/IIntegrationEventHandler.cs | 21 +- .../EventBus/Events/IntegrationEvent.cs | 23 +- .../EventBus/IEventBusSubscriptionsManager.cs | 47 +-- .../InMemoryEventBusSubscriptionsManager.cs | 281 ++++++++---------- .../EventBus/EventBus/SubscriptionInfo.cs | 42 +-- 7 files changed, 218 insertions(+), 245 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs index 55d62ade2..851be201d 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs @@ -1,13 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions { - public interface IDynamicIntegrationEventHandler - { - Task Handle(dynamic eventData); - } + public interface IDynamicIntegrationEventHandler + { + Task Handle(dynamic eventData); + } } diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs index dde05e1e3..8152328ca 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs @@ -1,24 +1,23 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; - -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions { - public interface IEventBus - { - void Publish(IntegrationEvent @event); + using IntegrationEvent = Events.IntegrationEvent; + + public interface IEventBus + { + void Publish(IntegrationEvent @event); - void Subscribe() - where T : IntegrationEvent - where TH : IIntegrationEventHandler; + void Subscribe() + where T : IntegrationEvent + where TH : IIntegrationEventHandler; - void SubscribeDynamic(string eventName) - where TH : IDynamicIntegrationEventHandler; + void SubscribeDynamic(string eventName) + where TH : IDynamicIntegrationEventHandler; - void UnsubscribeDynamic(string eventName) - where TH : IDynamicIntegrationEventHandler; + void UnsubscribeDynamic(string eventName) + where TH : IDynamicIntegrationEventHandler; - void Unsubscribe() - where TH : IIntegrationEventHandler - where T : IntegrationEvent; - } + void Unsubscribe() + where TH : IIntegrationEventHandler + where T : IntegrationEvent; + } } diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs index 828aed26a..00d5563a8 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs @@ -1,15 +1,16 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System.Threading.Tasks; +using Task = System.Threading.Tasks.Task; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions { - public interface IIntegrationEventHandler : IIntegrationEventHandler - where TIntegrationEvent: IntegrationEvent - { - Task Handle(TIntegrationEvent @event); - } + using IntegrationEvent = Events.IntegrationEvent; - public interface IIntegrationEventHandler - { - } + public interface IIntegrationEventHandler : IIntegrationEventHandler + where TIntegrationEvent : IntegrationEvent + { + Task Handle(TIntegrationEvent @event); + } + + public interface IIntegrationEventHandler + { + } } diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs index e01a7aaa8..1ae8a54b5 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs @@ -1,16 +1,17 @@ -using System; +using DateTime = System.DateTime; +using Guid = System.Guid; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events { - public class IntegrationEvent - { - public IntegrationEvent() - { - Id = Guid.NewGuid(); - CreationDate = DateTime.UtcNow; - } + public class IntegrationEvent + { + public IntegrationEvent() + { + Id = Guid.NewGuid(); + CreationDate = DateTime.UtcNow; + } - public Guid Id { get; } - public DateTime CreationDate { get; } - } + public Guid Id { get; } + public DateTime CreationDate { get; } + } } diff --git a/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs index c83c505b1..895df9122 100644 --- a/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs @@ -2,33 +2,34 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using System; using System.Collections.Generic; -using static Microsoft.eShopOnContainers.BuildingBlocks.EventBus.InMemoryEventBusSubscriptionsManager; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus { - public interface IEventBusSubscriptionsManager - { - bool IsEmpty { get; } - event EventHandler OnEventRemoved; - void AddDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler; + using static InMemoryEventBusSubscriptionsManager; - void AddSubscription() - where T : IntegrationEvent - where TH : IIntegrationEventHandler; + public interface IEventBusSubscriptionsManager + { + bool IsEmpty { get; } + event EventHandler OnEventRemoved; + void AddDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler; - void RemoveSubscription() - where TH : IIntegrationEventHandler - where T : IntegrationEvent; - void RemoveDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler; + void AddSubscription() + where T : IntegrationEvent + where TH : IIntegrationEventHandler; - bool HasSubscriptionsForEvent() where T : IntegrationEvent; - bool HasSubscriptionsForEvent(string eventName); - Type GetEventTypeByName(string eventName); - void Clear(); - IEnumerable GetHandlersForEvent() where T : IntegrationEvent; - IEnumerable GetHandlersForEvent(string eventName); - string GetEventKey(); - } + void RemoveSubscription() + where TH : IIntegrationEventHandler + where T : IntegrationEvent; + void RemoveDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler; + + bool HasSubscriptionsForEvent() where T : IntegrationEvent; + bool HasSubscriptionsForEvent(string eventName); + Type GetEventTypeByName(string eventName); + void Clear(); + IEnumerable GetHandlersForEvent() where T : IntegrationEvent; + IEnumerable GetHandlersForEvent(string eventName); + string GetEventKey(); + } } \ No newline at end of file diff --git a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs index 88be8cf96..a7efdf905 100644 --- a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs @@ -3,160 +3,135 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Text; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus { - public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager - { - - - private readonly Dictionary> _handlers; - private readonly List _eventTypes; - - public event EventHandler OnEventRemoved; - - public InMemoryEventBusSubscriptionsManager() - { - _handlers = new Dictionary>(); - _eventTypes = new List(); - } - - public bool IsEmpty => !_handlers.Keys.Any(); - public void Clear() => _handlers.Clear(); - - public void AddDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler - { - DoAddSubscription(typeof(TH), eventName, isDynamic: true); - } - - public void AddSubscription() - where T : IntegrationEvent - where TH : IIntegrationEventHandler - { - var eventName = GetEventKey(); - DoAddSubscription(typeof(TH), eventName, isDynamic: false); - _eventTypes.Add(typeof(T)); - } - - private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) - { - if (!HasSubscriptionsForEvent(eventName)) - { - _handlers.Add(eventName, new List()); - } - - if (_handlers[eventName].Any(s => s.HandlerType == handlerType)) - { - throw new ArgumentException( - $"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType)); - } - - if (isDynamic) - { - _handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType)); - } - else - { - _handlers[eventName].Add(SubscriptionInfo.Typed(handlerType)); - } - } - - - public void RemoveDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler - { - var handlerToRemove = FindDynamicSubscriptionToRemove(eventName); - DoRemoveHandler(eventName, handlerToRemove); - } - - - public void RemoveSubscription() - where TH : IIntegrationEventHandler - where T : IntegrationEvent - { - var handlerToRemove = FindSubscriptionToRemove(); - var eventName = GetEventKey(); - DoRemoveHandler(eventName, handlerToRemove); - } - - - private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove) - { - if (subsToRemove != null) - { - _handlers[eventName].Remove(subsToRemove); - if (!_handlers[eventName].Any()) - { - _handlers.Remove(eventName); - var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); - if (eventType != null) - { - _eventTypes.Remove(eventType); - } - RaiseOnEventRemoved(eventName); - } - - } - } - - public IEnumerable GetHandlersForEvent() where T : IntegrationEvent - { - var key = GetEventKey(); - return GetHandlersForEvent(key); - } - public IEnumerable GetHandlersForEvent(string eventName) => _handlers[eventName]; - - private void RaiseOnEventRemoved(string eventName) - { - var handler = OnEventRemoved; - if (handler != null) - { - OnEventRemoved(this, eventName); - } - } - - - private SubscriptionInfo FindDynamicSubscriptionToRemove(string eventName) - where TH : IDynamicIntegrationEventHandler - { - return DoFindSubscriptionToRemove(eventName, typeof(TH)); - } - - - private SubscriptionInfo FindSubscriptionToRemove() - where T : IntegrationEvent - where TH : IIntegrationEventHandler - { - var eventName = GetEventKey(); - return DoFindSubscriptionToRemove(eventName, typeof(TH)); - } - - private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType) - { - if (!HasSubscriptionsForEvent(eventName)) - { - return null; - } - - return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType); - - } - - public bool HasSubscriptionsForEvent() where T : IntegrationEvent - { - var key = GetEventKey(); - 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() - { - return typeof(T).Name; - } - } + public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager + { + + + private readonly Dictionary> _handlers; + private readonly List _eventTypes; + + public event EventHandler OnEventRemoved; + + public InMemoryEventBusSubscriptionsManager() + { + _handlers = new Dictionary>(); + _eventTypes = new List(); + } + + public bool IsEmpty => !_handlers.Keys.Any(); + public void Clear() => _handlers.Clear(); + + public void AddDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler + => DoAddSubscription(typeof(TH), eventName, isDynamic: true); + + public void AddSubscription() + where T : IntegrationEvent + where TH : IIntegrationEventHandler + { + var eventName = GetEventKey(); + DoAddSubscription(typeof(TH), eventName, isDynamic: false); + _eventTypes.Add(typeof(T)); + } + + private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) + { + if (!HasSubscriptionsForEvent(eventName)) + { + _handlers.Add(eventName, new List()); + } + + if (_handlers[eventName].Any(s => s.HandlerType == handlerType)) + { + throw new ArgumentException( + $"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType)); + } + + if (isDynamic) + { + _handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType)); + } + else + { + _handlers[eventName].Add(SubscriptionInfo.Typed(handlerType)); + } + } + + + public void RemoveDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler + { + var handlerToRemove = FindDynamicSubscriptionToRemove(eventName); + DoRemoveHandler(eventName, handlerToRemove); + } + + + public void RemoveSubscription() + where TH : IIntegrationEventHandler + where T : IntegrationEvent + { + var handlerToRemove = FindSubscriptionToRemove(); + var eventName = GetEventKey(); + DoRemoveHandler(eventName, handlerToRemove); + } + + + private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove) + { + if (subsToRemove != null) + { + _handlers[eventName].Remove(subsToRemove); + if (!_handlers[eventName].Any()) + { + _handlers.Remove(eventName); + var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); + if (eventType != null) + { + _eventTypes.Remove(eventType); + } + RaiseOnEventRemoved(eventName); + } + + } + } + + public IEnumerable GetHandlersForEvent() where T : IntegrationEvent + => GetHandlersForEvent(GetEventKey()); + public IEnumerable GetHandlersForEvent(string eventName) => _handlers[eventName]; + + private void RaiseOnEventRemoved(string eventName) + { + var handler = OnEventRemoved; + if (handler != null) + { + OnEventRemoved(this, eventName); + } + } + + + private SubscriptionInfo FindDynamicSubscriptionToRemove(string eventName) + where TH : IDynamicIntegrationEventHandler + => DoFindSubscriptionToRemove(eventName, typeof(TH)); + + + private SubscriptionInfo FindSubscriptionToRemove() + where T : IntegrationEvent + where TH : IIntegrationEventHandler + => DoFindSubscriptionToRemove(GetEventKey(), typeof(TH)); + + private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType) + => !HasSubscriptionsForEvent(eventName) ? null : _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType); + + public bool HasSubscriptionsForEvent() where T : IntegrationEvent + => HasSubscriptionsForEvent(GetEventKey()); + public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName); + + public Type GetEventTypeByName(string eventName) => _eventTypes.SingleOrDefault(t => t.Name == eventName); + + public string GetEventKey() + => typeof(T).Name; + } } diff --git a/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs b/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs index a20b3031c..1d81c4205 100644 --- a/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs +++ b/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs @@ -2,27 +2,27 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus { - public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager - { - public class SubscriptionInfo - { - public bool IsDynamic { get; } - public Type HandlerType{ get; } + public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager + { + public class SubscriptionInfo + { + public bool IsDynamic { get; } + public Type HandlerType { get; } - private SubscriptionInfo(bool isDynamic, Type handlerType) - { - IsDynamic = isDynamic; - HandlerType = handlerType; - } + private SubscriptionInfo(bool isDynamic, Type handlerType) + { + IsDynamic = isDynamic; + HandlerType = handlerType; + } - public static SubscriptionInfo Dynamic(Type handlerType) - { - return new SubscriptionInfo(true, handlerType); - } - public static SubscriptionInfo Typed(Type handlerType) - { - return new SubscriptionInfo(false, handlerType); - } - } - } + public static SubscriptionInfo Dynamic(Type handlerType) + { + return new SubscriptionInfo(true, handlerType); + } + public static SubscriptionInfo Typed(Type handlerType) + { + return new SubscriptionInfo(false, handlerType); + } + } + } }