From 9086bad6f1097aaa561104fd055477317e15a76f Mon Sep 17 00:00:00 2001 From: Musa Demir Date: Tue, 1 Sep 2020 06:18:34 +0300 Subject: [PATCH] store event type with event name Since event name might be different from class name, store it with event name --- .../InMemoryEventBusSubscriptionsManager.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs index ce982b8e9..3d13e4b61 100644 --- a/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs +++ b/src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs @@ -11,14 +11,14 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus private readonly Dictionary> _handlers; - private readonly List _eventTypes; + private readonly Dictionary _eventTypes; public event EventHandler OnEventRemoved; public InMemoryEventBusSubscriptionsManager() { _handlers = new Dictionary>(); - _eventTypes = new List(); + _eventTypes = new Dictionary(); } public bool IsEmpty => !_handlers.Keys.Any(); @@ -38,9 +38,9 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus DoAddSubscription(typeof(TH), eventName, isDynamic: false); - if (!_eventTypes.Contains(typeof(T))) + if (!_eventTypes.ContainsKey(eventName)) { - _eventTypes.Add(typeof(T)); + _eventTypes.Add(eventName, typeof(T)); } } @@ -94,10 +94,9 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus if (!_handlers[eventName].Any()) { _handlers.Remove(eventName); - var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); - if (eventType != null) + if (_eventTypes.ContainsKey(eventName)) { - _eventTypes.Remove(eventType); + _eventTypes.Remove(eventName); } RaiseOnEventRemoved(eventName); } @@ -152,7 +151,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus } 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.ContainsKey(eventName) ? _eventTypes[eventName] : default; public string GetEventKey() {