Code re-factorings and formatting in EventBus projects

This commit is contained in:
Rafsanul Hasan 2018-09-09 05:16:41 +06:00
parent 80eb943d78
commit 086302f5f1
No known key found for this signature in database
GPG Key ID: FC57FD2D87BE60DD
7 changed files with 191 additions and 218 deletions

View File

@ -1,8 +1,4 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {

View File

@ -1,8 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
using System;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {
using IntegrationEvent = Events.IntegrationEvent;
public interface IEventBus public interface IEventBus
{ {
void Publish(IntegrationEvent @event); void Publish(IntegrationEvent @event);

View File

@ -1,10 +1,11 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Task = System.Threading.Tasks.Task;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
{ {
using IntegrationEvent = Events.IntegrationEvent;
public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler
where TIntegrationEvent: IntegrationEvent where TIntegrationEvent : IntegrationEvent
{ {
Task Handle(TIntegrationEvent @event); Task Handle(TIntegrationEvent @event);
} }

View File

@ -1,4 +1,5 @@
using System; using DateTime = System.DateTime;
using Guid = System.Guid;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events
{ {

View File

@ -2,10 +2,11 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using static Microsoft.eShopOnContainers.BuildingBlocks.EventBus.InMemoryEventBusSubscriptionsManager;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
{ {
using static InMemoryEventBusSubscriptionsManager;
public interface IEventBusSubscriptionsManager public interface IEventBusSubscriptionsManager
{ {
bool IsEmpty { get; } bool IsEmpty { get; }

View File

@ -3,8 +3,6 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
{ {
@ -28,9 +26,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
public void AddDynamicSubscription<TH>(string eventName) public void AddDynamicSubscription<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler where TH : IDynamicIntegrationEventHandler
{ => DoAddSubscription(typeof(TH), eventName, isDynamic: true);
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
}
public void AddSubscription<T, TH>() public void AddSubscription<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
@ -103,10 +99,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
} }
public IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent public IEnumerable<SubscriptionInfo> GetHandlersForEvent<T>() where T : IntegrationEvent
{ => GetHandlersForEvent(GetEventKey<T>());
var key = GetEventKey<T>();
return GetHandlersForEvent(key);
}
public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName]; public IEnumerable<SubscriptionInfo> GetHandlersForEvent(string eventName) => _handlers[eventName];
private void RaiseOnEventRemoved(string eventName) private void RaiseOnEventRemoved(string eventName)
@ -121,42 +114,24 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName) private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler where TH : IDynamicIntegrationEventHandler
{ => DoFindSubscriptionToRemove(eventName, typeof(TH));
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo FindSubscriptionToRemove<T, TH>() private SubscriptionInfo FindSubscriptionToRemove<T, TH>()
where T : IntegrationEvent where T : IntegrationEvent
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
{ => DoFindSubscriptionToRemove(GetEventKey<T>(), typeof(TH));
var eventName = GetEventKey<T>();
return DoFindSubscriptionToRemove(eventName, typeof(TH));
}
private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType) private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType)
{ => !HasSubscriptionsForEvent(eventName) ? null : _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType);
if (!HasSubscriptionsForEvent(eventName))
{
return null;
}
return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType);
}
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent
{ => HasSubscriptionsForEvent(GetEventKey<T>());
var key = GetEventKey<T>();
return HasSubscriptionsForEvent(key);
}
public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName); 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<T>() public string GetEventKey<T>()
{ => typeof(T).Name;
return typeof(T).Name;
}
} }
} }

View File

@ -7,7 +7,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
public class SubscriptionInfo public class SubscriptionInfo
{ {
public bool IsDynamic { get; } public bool IsDynamic { get; }
public Type HandlerType{ get; } public Type HandlerType { get; }
private SubscriptionInfo(bool isDynamic, Type handlerType) private SubscriptionInfo(bool isDynamic, Type handlerType)
{ {