domain events
TRANSCRIPT
Domain EventsSteve Smith
@ardalisardalis.com
Pluralsight http://bit.ly/PS-ssmith
Usually the last kid picked from the DDD patterns• Repositories ✓• Factories ✓• Services ✓• Entities ✓• Value Objects ✓• Aggregates ✓
• Oh yeah, Domain Events
I’m right here!
Domain-Driven Design• Domain Events not in original DDD
book (2004)
• Covered by Fowler in 2005
• Eric Evans published article on it in 2010
Kinds of Events• Application Events• Page Load, Button Click, Window Scroll
• System Events• Restart, Backup Completed
• Domain Events• Appointment Confirmed, Checkout Completed, Analysis
Finished
An example scenarioGiven a customer has created an orderWhen the customer completes their purchaseThen the customer receives an email confirmation
An example solutionOrderService Checkout(Order order) - Save pending order in database - Send confirmation email
So far, so good
More requirementsGiven a customer has created an orderWhen the customer completes their purchaseThen the customer’s card is charged
And if it fails, send a different message to the customer
Then inventory is checked to ensure the order can be fulfilled
And if not a different message is sent to the customer
An example solutionOrderService
Checkout(Order order)oAttempt to process paymentoIf payment fails, send notification email; exitoSave pending order in databaseoConfirm inventory is availableoIf insufficient email, send notification email; exitoSend successful order confirmation email
Analysis• Complexity is growing
• Must change with each new requirement• Open/Closed Principle
• Checkout’s responsibilities are growing• Single Responsibility Principle
• Potentially different abstraction levels (emails, order processing rules)
An event-driven solutionOrderService Checkout(Order order) - Dispatch new OrderCompletedEvent(order)
Event Handlers:• OrderPaymentHandler• OrderInventoryHandler• OrderNotificationHandler
Event-Driven ProgrammingAn Event• Something that happened• …that other parts of the application may need to know
about
• It’s a message• …which should be immutable, since it represents the past
• Usually asynchronous• …especially across process boundaries
Domain EventsModel something that happens which is of interest to a domain expert
May lead to (or result from) a state change in a domain object
Are part of the domain model
Are usually handled synchronously within the application (but may themselves raise events outside of the application)
Event Sourcing• Related concept – not required to take advantage of
domain events
• Store sequences of events rather than directly mutation object state
• Determine current state by replaying events
• Learn more: http://martinfowler.com/eaaDev/EventSourcing.html
Event Processing• An event is raised
• Handlers within the current process handle the event
• If external applications need to be notified, specific handlers can adapt and send events to these applications as well
• If unknown or future external applications will need to respond to events, a service bus can be implemented
Implementations• C# events• Static DomainEvents helper (Udi Dahan, 2009)• Return, don’t publish, domain events (2013)• Entity-specific event log (Jimmy Bogard, 2014)
DemosImplementing Domain Events
Common Scenario• How do you add logic to entities that affects multiple
entities?
Example:When a customer’s total amount purchased exceeds $1000, notify a salesperson to contact them.
Questions?
References• Pluralsight http://bit.ly/PS-ssmith• Domain Events – Fowler http://martinfowler.com/eaaDev/DomainEvent.html • Domain Events – Evans https://domainlanguage.com/newsletter/2010-03/ • Domain Events – Bogard https://
lostechies.com/jimmybogard/2010/04/08/strengthening-your-domain-domain-events/ Implementations• Udi Dahan – http://www.udidahan.com/2009/06/14/domain-events-salvation/ • Jimmy Bogard –
https://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/
• Jay Kronquist - http://www.jayway.com/2013/06/20/dont-publish-domain-events-return-them/