GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: simplified logic in Bus.Invoke method
Lars346915e22e25
diff --git a/LarsWM.Core/Common/Services/Bus.cs b/LarsWM.Core/Common/Services/Bus.csindex f07d9aa0..f02f4c17 100644--- a/LarsWM.Core/Common/Services/Bus.cs+++ b/LarsWM.Core/Common/Services/Bus.cs@@ -7,37 +7,37 @@ using System.Text;namespace LarsWM.Core.Common.Services{/// <summary>- /// The event bus+ /// Bus facilitates communication to command and event handlers./// </summary>public sealed class Bus : IBus{- private static readonly List<Type> RegisteredCommandHandlers = new List<Type>();- private static readonly List<Type> RegisteredEventHandlers = new List<Type>();+ private List<Type> _registeredCommandHandlers = new List<Type>();+ private List<Type> _registeredEventHandlers = new List<Type>();/// <summary>- /// Sends command to appropriate command handler.+ /// Sends command to appropriate command handlers./// </summary>public void Invoke<T>(T command) where T : Command{- var commandType = typeof(T);+ // Create a Type object representing the generic ICommandHandler type.+ var commandHandlerGeneric = typeof(ICommandHandler<>);++ // Create a Type object representing the constructed ICommandHandler generic.+ var handlerTypeToCall = commandHandlerGeneric.MakeGenericType(command.GetType());++ var handlersToCall = _registeredCommandHandlers.Where(handler => handlerTypeToCall.IsAssignableFrom(handler));// TODO: add centralised error handling here?// TODO: create CommandResponse interface with State (failure, success) and Data (any return value from command)?- foreach (var handler in RegisteredCommandHandlers)+ foreach (var handler in handlersToCall){- var handlerInterface = handler.GetInterfaces()[0];- var handlerCommandType = handlerInterface.GetGenericArguments()[0];-- if (handlerCommandType == commandType)- {- ICommandHandler<T> handlerInstance = Program.ServiceProvider.GetService(handler) as ICommandHandler<T>;- handlerInstance.Handle(command);- }+ ICommandHandler<T> handlerInstance = Program.ServiceProvider.GetService(handler) as ICommandHandler<T>;+ handlerInstance.Handle(command);}}/// <summary>- /// Sends event to appropriate event handler.+ /// Sends event to appropriate event handlers./// </summary>public void RaiseEvent<T>(T @event) where T : Event{@@ -46,12 +46,12 @@ namespace LarsWM.Core.Common.Servicespublic void RegisterCommandHandler<T>(){- RegisteredCommandHandlers.Add(typeof(T));+ _registeredCommandHandlers.Add(typeof(T));}public void RegisterEventHandler<T>(){- RegisteredEventHandlers.Add(typeof(T));+ _registeredEventHandlers.Add(typeof(T));}}}