Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active October 3, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/3808311862cc61d2b11d91d7be26b207 to your computer and use it in GitHub Desktop.
Save vkhorikov/3808311862cc61d2b11d91d7be26b207 to your computer and use it in GitHub Desktop.
Domain events
public class OrderSubmitted : IDomainEvent
{
public Order Order { get; }
}
public static class DomainEvents
{
private static Dictionary<Type, List<Delegate>> _handlers;
public static void Register<T>(Action<T> eventHandler)
where T : IDomainEvent
{
_handlers[typeof(T)].Add(eventHandler);
}
public static void Raise<T>(T domainEvent)
where T : IDomainEvent
{
foreach (Delegate handler in _handlers[domainEvent.GetType()])
{
var action = (Action<T>)handler;
action(domainEvent);
}
}
}
public class Order
{
private Customer _customer;
private List<OrderLine> _lines;
public Order(IEnumerable<ProductLine> lines, Customer customer)
{
_customer = customer;
_lines = lines
.Select(x => new OrderLine(x.Product, x.Quantity, x.Price))
.ToList();
DomainEvents.Raise(new OrderSubmitted(this));
}
}
public static class OrderStats
{
static OrderStats()
{
DomainEvents.Register<OrderSubmitted>(ev => ProcessSubmittedOrder(ev));
}
private static void ProcessSubmittedOrder(OrderSubmitted ev)
{
decimal amount = ev.Order.GetAmount();
int numberOfItems = ev.Order.GetNumberOfItems();
/* Persist amount and numberOfItems to DB */
}
}
public static class OrderNotification
{
static OrderNotification()
{
DomainEvents.Register<OrderSubmitted>(ev => ProcessSubmittedOrder(ev));
}
private static void ProcessSubmittedOrder(OrderSubmitted ev)
{
/* Use ev.Order.Lines to compose an email and send it to ev.Order.Customer */
}
}
public class CartService
{
public void CheckOut(IReadOnlyList<ProductLine> productsToCheckOut)
{
var order = new Order(productsToCheckOut, _customer);
_orderStats.Process(order);
_repository.Save(order);
}
}
public class Order
{
private Customer _customer;
private List<OrderLine> _lines;
public Order(IEnumerable<ProductLine> lines, Customer customer)
{
_customer = customer;
_lines = lines
.Select(x => new OrderLine(x.Product, x.Quantity, x.Price))
.ToList();
AddDomainEvent(new OrderSubmitted(this)); // Method in the base Entity or AggregateRoot class
}
}
internal class EventListener :
IPostInsertEventListener,
IPostDeleteEventListener,
IPostUpdateEventListener,
IPostCollectionUpdateEventListener
{
public void OnPostUpdate(PostUpdateEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostDelete(PostDeleteEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostInsert(PostInsertEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostUpdateCollection(PostCollectionUpdateEvent ev)
{
DispatchEvents(ev.AffectedOwnerOrNull as AggregateRoot);
}
private void DispatchEvents(AggregateRoot aggregateRoot)
{
if (aggregateRoot == null)
return;
foreach (IDomainEvent domainEvent in aggregateRoot.DomainEvents)
{
DomainEvents.Dispatch(domainEvent);
}
aggregateRoot.ClearEvents();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment