Download - Domain events
![Page 1: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/1.jpg)
Domain EventsSteve Smith
@ardalisardalis.com
![Page 3: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/3.jpg)
Usually the last kid picked from the DDD patterns• Repositories ✓• Factories ✓• Services ✓• Entities ✓• Value Objects ✓• Aggregates ✓
• Oh yeah, Domain Events
I’m right here!
![Page 4: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/4.jpg)
Domain-Driven Design• Domain Events not in original DDD
book (2004)
• Covered by Fowler in 2005
• Eric Evans published article on it in 2010
![Page 5: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/5.jpg)
Kinds of Events• Application Events• Page Load, Button Click, Window Scroll
• System Events• Restart, Backup Completed
• Domain Events• Appointment Confirmed, Checkout Completed, Analysis
Finished
![Page 6: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/6.jpg)
An example scenarioGiven a customer has created an orderWhen the customer completes their purchaseThen the customer receives an email confirmation
![Page 7: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/7.jpg)
An example solutionOrderService Checkout(Order order) - Save pending order in database - Send confirmation email
![Page 8: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/8.jpg)
So far, so good
![Page 9: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/9.jpg)
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
![Page 10: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/10.jpg)
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
![Page 11: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/11.jpg)
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)
![Page 12: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/12.jpg)
An event-driven solutionOrderService Checkout(Order order) - Dispatch new OrderCompletedEvent(order)
Event Handlers:• OrderPaymentHandler• OrderInventoryHandler• OrderNotificationHandler
![Page 13: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/13.jpg)
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
![Page 14: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/14.jpg)
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)
![Page 15: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/15.jpg)
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
![Page 16: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/16.jpg)
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
![Page 17: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/17.jpg)
Implementations• C# events• Static DomainEvents helper (Udi Dahan, 2009)• Return, don’t publish, domain events (2013)• Entity-specific event log (Jimmy Bogard, 2014)
![Page 18: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/18.jpg)
DemosImplementing Domain Events
![Page 19: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/19.jpg)
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.
![Page 20: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/20.jpg)
Questions?
![Page 21: Domain events](https://reader035.vdocuments.us/reader035/viewer/2022062822/587ac5861a28abc0478b7c89/html5/thumbnails/21.jpg)
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/