/// <summary> /// Invokes each subscriber method registered to handle the event. /// </summary> /// <param name="event"></param> public void Publish(IEvent @event) { var eventName = @event.GetType().GetClassName(); var key = new EventOverrideKey { EventName = eventName, IdentityTenant = @event.IdentityTenant }; if (_overriders.Keys.Any(k => k.EventName == key.EventName && k.IdentityTenant == @event.IdentityTenant)) { var customization = _overriders .FirstOrDefault(kv => kv.Key.EventName == key.EventName && kv.Key.IdentityTenant == @event.IdentityTenant) .Value; customization?.Invoke(@event); } else if (_subscribers.ContainsKey(eventName)) { var actions = _subscribers[eventName]; foreach (var action in actions) { action.Invoke(@event); } } else { throw new UnhandledEventException(eventName); } }
/// <summary> /// Register a custom tenant-specific handler for the event. /// </summary> public void Override <T>(Action <T> action, Guid tenant) where T : IEvent { var key = new EventOverrideKey { EventName = typeof(T).FullName, IdentityTenant = tenant }; if (_overriders.Any(x => x.Key.EventName == key.EventName && x.Key.IdentityTenant == key.IdentityTenant)) { throw new AmbiguousCommandHandlerException(key.EventName); } _overriders.Add(key, (command) => action((T)command)); }