Browse Source

store event type with event name

Since event name might be different from class name, store it with event name
pull/1417/head
Musa Demir 4 years ago
parent
commit
9086bad6f1
1 changed files with 7 additions and 8 deletions
  1. +7
    -8
      src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs

+ 7
- 8
src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs View File

@ -11,14 +11,14 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
private readonly Dictionary<string, List<SubscriptionInfo>> _handlers;
private readonly List<Type> _eventTypes;
private readonly Dictionary<string, Type> _eventTypes;
public event EventHandler<string> OnEventRemoved;
public InMemoryEventBusSubscriptionsManager()
{
_handlers = new Dictionary<string, List<SubscriptionInfo>>();
_eventTypes = new List<Type>();
_eventTypes = new Dictionary<string, Type>();
}
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<T>()
{


Loading…
Cancel
Save