using Client.Domain.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace Client.Infrastructure.Events { public class InMemoryEventBus : EventBusInterface { public void Publish(T @event) where T : EventInterface { var type = @event.GetType(); if (subscribers.ContainsKey(type)) { foreach (var subscriber in subscribers[type]) { (subscriber as EventHandlerInterface)?.Handle(@event); } } } public void Subscrbe(EventHandlerInterface handler) where T : EventInterface { var type = typeof(T); if (!subscribers.ContainsKey(type)) { subscribers[type] = new List(); } subscribers[type].Add(handler); } public void Unsubscrbe(EventHandlerInterface handler) where T : EventInterface { var type = typeof(T); if (subscribers.ContainsKey(type)) { subscribers[type].Remove(handler); } } private Dictionary> subscribers = new Dictionary>(); } }