From f3d64923404e7effe4ccc28d5534f077331e052c Mon Sep 17 00:00:00 2001 From: Sumit Ghosh Date: Wed, 13 Oct 2021 12:51:44 +0530 Subject: [PATCH] Moved namespaces to global usings for eventbus --- .../IDynamicIntegrationEventHandler.cs | 9 +- .../EventBus/Abstractions/IEventBus.cs | 29 +-- .../Abstractions/IIntegrationEventHandler.cs | 18 +- .../EventBus/Events/IntegrationEvent.cs | 40 ++- .../Extensions/GenericTypeExtensions.cs | 38 ++- .../EventBus/IEventBusSubscriptionsManager.cs | 51 ++-- .../InMemoryEventBusSubscriptionsManager.cs | 227 +++++++++--------- .../EventBus/EventBus/SubscriptionInfo.cs | 37 ++- 8 files changed, 207 insertions(+), 242 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs index c26ee6a81..8b9fbe497 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IDynamicIntegrationEventHandler.cs @@ -1,9 +1,6 @@ -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); } diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs index 4c9758a40..492a10e42 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs @@ -1,23 +1,20 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions +public interface IEventBus { - public interface IEventBus - { - void Publish(IntegrationEvent @event); + 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 99735b871..030c604b5 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/IIntegrationEventHandler.cs @@ -1,15 +1,11 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System.Threading.Tasks; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions +public interface IIntegrationEventHandler : IIntegrationEventHandler + where TIntegrationEvent : IntegrationEvent { - public interface IIntegrationEventHandler : IIntegrationEventHandler - where TIntegrationEvent : IntegrationEvent - { - Task Handle(TIntegrationEvent @event); - } + Task Handle(TIntegrationEvent @event); +} - public interface IIntegrationEventHandler - { - } +public interface IIntegrationEventHandler +{ } diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs index 1f185a691..31118c4da 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs @@ -1,27 +1,23 @@ -using System; -using System.Text.Json.Serialization; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events -{ - public record IntegrationEvent - { - public IntegrationEvent() - { - Id = Guid.NewGuid(); - CreationDate = DateTime.UtcNow; - } +public record IntegrationEvent +{ + public IntegrationEvent() + { + Id = Guid.NewGuid(); + CreationDate = DateTime.UtcNow; + } - [JsonConstructor] - public IntegrationEvent(Guid id, DateTime createDate) - { - Id = id; - CreationDate = createDate; - } + [JsonConstructor] + public IntegrationEvent(Guid id, DateTime createDate) + { + Id = id; + CreationDate = createDate; + } - [JsonInclude] - public Guid Id { get; private init; } + [JsonInclude] + public Guid Id { get; private init; } - [JsonInclude] - public DateTime CreationDate { get; private init; } - } + [JsonInclude] + public DateTime CreationDate { get; private init; } } diff --git a/src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs b/src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs index 775c29c3e..70e295beb 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs @@ -1,30 +1,26 @@ -using System; -using System.Linq; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions +public static class GenericTypeExtensions { - public static class GenericTypeExtensions + public static string GetGenericTypeName(this Type type) { - public static string GetGenericTypeName(this Type type) - { - var typeName = string.Empty; - - if (type.IsGenericType) - { - var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray()); - typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>"; - } - else - { - typeName = type.Name; - } + var typeName = string.Empty; - return typeName; + if (type.IsGenericType) + { + var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray()); + typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>"; } - - public static string GetGenericTypeName(this object @object) + else { - return @object.GetType().GetGenericTypeName(); + typeName = type.Name; } + + return typeName; + } + + public static string GetGenericTypeName(this object @object) + { + return @object.GetType().GetGenericTypeName(); } } diff --git a/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs index c83c505b1..a140bfc75 100644 --- a/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs @@ -1,34 +1,27 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using static Microsoft.eShopOnContainers.BuildingBlocks.EventBus.InMemoryEventBusSubscriptionsManager; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus +public interface IEventBusSubscriptionsManager { - public interface IEventBusSubscriptionsManager - { - bool IsEmpty { get; } - event EventHandler OnEventRemoved; - void AddDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler; + bool IsEmpty { get; } + event EventHandler OnEventRemoved; + void AddDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler; - void AddSubscription() - where T : IntegrationEvent - where TH : IIntegrationEventHandler; + void AddSubscription() + where T : IntegrationEvent + where TH : IIntegrationEventHandler; - void RemoveSubscription() - where TH : IIntegrationEventHandler - where T : IntegrationEvent; - void RemoveDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler; + 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 + 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(); +} diff --git a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs index a86248f01..aac205f80 100644 --- a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs @@ -1,162 +1,155 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus +public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager { - 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(); - private readonly Dictionary> _handlers; - private readonly List _eventTypes; + public void AddDynamicSubscription(string eventName) + where TH : IDynamicIntegrationEventHandler + { + DoAddSubscription(typeof(TH), eventName, isDynamic: true); + } - public event EventHandler OnEventRemoved; + public void AddSubscription() + where T : IntegrationEvent + where TH : IIntegrationEventHandler + { + var eventName = GetEventKey(); - public InMemoryEventBusSubscriptionsManager() + DoAddSubscription(typeof(TH), eventName, isDynamic: false); + + if (!_eventTypes.Contains(typeof(T))) { - _handlers = new Dictionary>(); - _eventTypes = new List(); + _eventTypes.Add(typeof(T)); } + } - public bool IsEmpty => !_handlers.Keys.Any(); - public void Clear() => _handlers.Clear(); - - public void AddDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler + private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) + { + if (!HasSubscriptionsForEvent(eventName)) { - DoAddSubscription(typeof(TH), eventName, isDynamic: true); + _handlers.Add(eventName, new List()); } - public void AddSubscription() - where T : IntegrationEvent - where TH : IIntegrationEventHandler + if (_handlers[eventName].Any(s => s.HandlerType == handlerType)) { - var eventName = GetEventKey(); - - DoAddSubscription(typeof(TH), eventName, isDynamic: false); - - if (!_eventTypes.Contains(typeof(T))) - { - _eventTypes.Add(typeof(T)); - } + throw new ArgumentException( + $"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType)); } - private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) + if (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)); - } + _handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType)); } - - - public void RemoveDynamicSubscription(string eventName) - where TH : IDynamicIntegrationEventHandler + else { - var handlerToRemove = FindDynamicSubscriptionToRemove(eventName); - DoRemoveHandler(eventName, handlerToRemove); + _handlers[eventName].Add(SubscriptionInfo.Typed(handlerType)); } + } - public void RemoveSubscription() - where TH : IIntegrationEventHandler - where T : IntegrationEvent - { - var handlerToRemove = FindSubscriptionToRemove(); - var eventName = GetEventKey(); - DoRemoveHandler(eventName, handlerToRemove); - } + 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) + private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove) + { + if (subsToRemove != null) { - if (subsToRemove != null) + _handlers[eventName].Remove(subsToRemove); + if (!_handlers[eventName].Any()) { - _handlers[eventName].Remove(subsToRemove); - if (!_handlers[eventName].Any()) + _handlers.Remove(eventName); + var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); + if (eventType != null) { - _handlers.Remove(eventName); - var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); - if (eventType != null) - { - _eventTypes.Remove(eventType); - } - RaiseOnEventRemoved(eventName); + _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; - handler?.Invoke(this, 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; + handler?.Invoke(this, eventName); + } - private SubscriptionInfo FindDynamicSubscriptionToRemove(string eventName) - where TH : IDynamicIntegrationEventHandler - { - return DoFindSubscriptionToRemove(eventName, typeof(TH)); - } + 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) + 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)) { - if (!HasSubscriptionsForEvent(eventName)) - { - return null; - } + return null; + } - return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType); + 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 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 Type GetEventTypeByName(string eventName) => _eventTypes.SingleOrDefault(t => t.Name == eventName); - public string GetEventKey() - { - return typeof(T).Name; - } + public string GetEventKey() + { + return typeof(T).Name; } } diff --git a/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs b/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs index 9e61034a0..7270c6e90 100644 --- a/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs +++ b/src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs @@ -1,28 +1,25 @@ -using System; +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 Type HandlerType { get; } + 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); } } }