Moved namespaces to global usings for eventbus

This commit is contained in:
Sumit Ghosh 2021-10-13 12:51:44 +05:30
parent f847ec020f
commit f3d6492340
8 changed files with 245 additions and 280 deletions

View File

@ -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);
}
} }

View File

@ -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<T, TH>() void Subscribe<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>; where TH : IIntegrationEventHandler<T>;
void SubscribeDynamic<TH>(string eventName) void SubscribeDynamic<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler; where TH : IDynamicIntegrationEventHandler;
void UnsubscribeDynamic<TH>(string eventName) void UnsubscribeDynamic<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler; where TH : IDynamicIntegrationEventHandler;
void Unsubscribe<T, TH>() void Unsubscribe<T, TH>()
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent; where T : IntegrationEvent;
}
} }

View File

@ -1,15 +1,11 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler
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,27 +1,23 @@
using System; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System.Text.Json.Serialization;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events public record IntegrationEvent
{ {
public record IntegrationEvent public IntegrationEvent()
{ {
public IntegrationEvent() Id = Guid.NewGuid();
{ CreationDate = DateTime.UtcNow;
Id = Guid.NewGuid();
CreationDate = DateTime.UtcNow;
}
[JsonConstructor]
public IntegrationEvent(Guid id, DateTime createDate)
{
Id = id;
CreationDate = createDate;
}
[JsonInclude]
public Guid Id { get; private init; }
[JsonInclude]
public DateTime CreationDate { get; private init; }
} }
[JsonConstructor]
public IntegrationEvent(Guid id, DateTime createDate)
{
Id = id;
CreationDate = createDate;
}
[JsonInclude]
public Guid Id { get; private init; }
[JsonInclude]
public DateTime CreationDate { get; private init; }
} }

View File

@ -1,30 +1,26 @@
using System; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
using System.Linq;
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 typeName = string.Empty; var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray());
typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>";
if (type.IsGenericType) }
{ else
var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray()); {
typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>"; typeName = type.Name;
}
else
{
typeName = type.Name;
}
return typeName;
} }
public static string GetGenericTypeName(this object @object) return typeName;
{ }
return @object.GetType().GetGenericTypeName();
} public static string GetGenericTypeName(this object @object)
{
return @object.GetType().GetGenericTypeName();
} }
} }

View File

@ -1,34 +1,27 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
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
{ {
public interface IEventBusSubscriptionsManager bool IsEmpty { get; }
{ event EventHandler<string> OnEventRemoved;
bool IsEmpty { get; } void AddDynamicSubscription<TH>(string eventName)
event EventHandler<string> OnEventRemoved; where TH : IDynamicIntegrationEventHandler;
void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler;
void AddSubscription<T, TH>() void AddSubscription<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>; where TH : IIntegrationEventHandler<T>;
void RemoveSubscription<T, TH>() void RemoveSubscription<T, TH>()
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent; where T : IntegrationEvent;
void RemoveDynamicSubscription<TH>(string eventName) void RemoveDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler; where TH : IDynamicIntegrationEventHandler;
bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent; bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent;
bool HasSubscriptionsForEvent(string eventName); bool HasSubscriptionsForEvent(string eventName);
Type GetEventTypeByName(string eventName); Type GetEventTypeByName(string eventName);
void Clear(); void Clear();
IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent; IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent;
IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName); IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName);
string GetEventKey<T>(); string GetEventKey<T>();
} }
}

View File

@ -1,162 +1,155 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
{ {
public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
private readonly Dictionary<string, List<SubscriptionInfo>> _handlers;
private readonly List<Type> _eventTypes;
public event EventHandler<string> OnEventRemoved;
public InMemoryEventBusSubscriptionsManager()
{ {
_handlers = new Dictionary<string, List<SubscriptionInfo>>();
_eventTypes = new List<Type>();
}
public bool IsEmpty => !_handlers.Keys.Any();
public void Clear() => _handlers.Clear();
private readonly Dictionary<string, List<SubscriptionInfo>> _handlers; public void AddDynamicSubscription<TH>(string eventName)
private readonly List<Type> _eventTypes; where TH : IDynamicIntegrationEventHandler
{
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
}
public event EventHandler<string> OnEventRemoved; public void AddSubscription<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = GetEventKey<T>();
public InMemoryEventBusSubscriptionsManager() DoAddSubscription(typeof(TH), eventName, isDynamic: false);
if (!_eventTypes.Contains(typeof(T)))
{ {
_handlers = new Dictionary<string, List<SubscriptionInfo>>(); _eventTypes.Add(typeof(T));
_eventTypes = new List<Type>();
}
public bool IsEmpty => !_handlers.Keys.Any();
public void Clear() => _handlers.Clear();
public void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
}
public void AddSubscription<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = GetEventKey<T>();
DoAddSubscription(typeof(TH), eventName, isDynamic: false);
if (!_eventTypes.Contains(typeof(T)))
{
_eventTypes.Add(typeof(T));
}
}
private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic)
{
if (!HasSubscriptionsForEvent(eventName))
{
_handlers.Add(eventName, new List<SubscriptionInfo>());
}
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<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
var handlerToRemove = FindDynamicSubscriptionToRemove<TH>(eventName);
DoRemoveHandler(eventName, handlerToRemove);
}
public void RemoveSubscription<T, TH>()
where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent
{
var handlerToRemove = FindSubscriptionToRemove<T, TH>();
var eventName = GetEventKey<T>();
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<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent
{
var key = GetEventKey<T>();
return GetHandlersForEvent(key);
}
public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName];
private void RaiseOnEventRemoved(string eventName)
{
var handler = OnEventRemoved;
handler?.Invoke(this, eventName);
}
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo FindSubscriptionToRemove<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = GetEventKey<T>();
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<T>() where T : IntegrationEvent
{
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;
} }
} }
private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic)
{
if (!HasSubscriptionsForEvent(eventName))
{
_handlers.Add(eventName, new List<SubscriptionInfo>());
}
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<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
var handlerToRemove = FindDynamicSubscriptionToRemove<TH>(eventName);
DoRemoveHandler(eventName, handlerToRemove);
}
public void RemoveSubscription<T, TH>()
where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent
{
var handlerToRemove = FindSubscriptionToRemove<T, TH>();
var eventName = GetEventKey<T>();
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<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent
{
var key = GetEventKey<T>();
return GetHandlersForEvent(key);
}
public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName];
private void RaiseOnEventRemoved(string eventName)
{
var handler = OnEventRemoved;
handler?.Invoke(this, eventName);
}
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo FindSubscriptionToRemove<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = GetEventKey<T>();
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<T>() where T : IntegrationEvent
{
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

@ -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; }
private SubscriptionInfo(bool isDynamic, Type handlerType)
{ {
public bool IsDynamic { get; } IsDynamic = isDynamic;
public Type HandlerType { get; } HandlerType = handlerType;
}
private SubscriptionInfo(bool isDynamic, Type handlerType) public static SubscriptionInfo Dynamic(Type handlerType)
{ {
IsDynamic = isDynamic; return new SubscriptionInfo(true, handlerType);
HandlerType = handlerType; }
} public static SubscriptionInfo Typed(Type handlerType)
{
public static SubscriptionInfo Dynamic(Type handlerType) return new SubscriptionInfo(false, handlerType);
{
return new SubscriptionInfo(true, handlerType);
}
public static SubscriptionInfo Typed(Type handlerType)
{
return new SubscriptionInfo(false, handlerType);
}
} }
} }
} }