-
-
Save vkhorikov/3808311862cc61d2b11d91d7be26b207 to your computer and use it in GitHub Desktop.
Domain events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrderSubmitted : IDomainEvent | |
{ | |
public Order Order { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CartService | |
{ | |
public void CheckOut(IReadOnlyList<ProductLine> productsToCheckOut) | |
{ | |
var order = new Order(productsToCheckOut, _customer); | |
_orderStats.Process(order); | |
_repository.Save(order); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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