Skip to content

Commit

Permalink
feature: Subscribing to domain events via delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwittke committed Mar 31, 2018
1 parent e674c6a commit dceffb8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Backend.Fx/Patterns/EventAggregation/EventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public EventAggregator(IEventHandlerProvider eventHandlerProvider)
throw;
}
}

foreach (var subscribedHandler in subscribedEventHandlers.OfType<Action<TDomainEvent>>().ToArray())
{
try
{
subscribedHandler.Invoke(domainEvent);
}
catch (Exception ex)
{
Logger.Error(ex, $"Handling of {typeof(TDomainEvent).Name} by a subscribed handler failed.");
throw;
}
}
}

/// <summary>
Expand Down Expand Up @@ -73,5 +86,15 @@ public EventAggregator(IEventHandlerProvider eventHandlerProvider)
{
subscribedEventHandlers.Add(handler);
}

/// <summary>
/// Register a delegate that should be called synchronously in the same scope/transaction where the domain event is published.
/// </summary>
/// <typeparam name="TDomainEvent"></typeparam>
/// <param name="handler"></param>
public void SubscribeToDomainEvent<TDomainEvent>(Action<TDomainEvent> handler) where TDomainEvent : IDomainEvent
{
subscribedEventHandlers.Add(handler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Backend.Fx.Patterns.EventAggregation
public interface IEventHandlerProvider
{
/// <summary>
/// get all deomain event handlers that want to handle a specific domain event
/// get all domain event handlers that want to handle a specific domain event
/// </summary>
/// <typeparam name="TDomainEvent"></typeparam>
/// <returns></returns>
Expand Down

0 comments on commit dceffb8

Please sign in to comment.