Given at DogFoodCon 2016 in Columbus, Ohio
Domain Events, a Domain-Driven Design (DDD) pattern, provide a way to decouple your code. Rather than hard-coding a series of steps that must be completed in response to a particular action a user takes, that action can raise an event. Handlers for an event can be added without changing the code responsible for raising the event, resulting in a more extensible design. Coupled with SignalR or push notifications in the UI layer, domain events can trigger live updates, even to web-based applications and mobile apps.
3. No Content Here
(Reserved for Watermark)
Repositories ✓
Factories ✓
Services ✓
Entities ✓
Value Objects ✓
Aggregates ✓
Oh yeah, Domain Events
Usually the last kid picked from
the DDD patterns I’m right
here!
4. No Content Here
(Reserved for Watermark)
Domain Events not covered in original DDD book (2004)
Covered by Martin Fowler in 2005
Evans published article on them in 2010
Domain-Driven Design
5. No Content Here
(Reserved for Watermark)
Application Events
Page Load, Button Click, Window Scroll
System Events
Restart, Backup Completed
Domain Events
Appointment Confirmed, Checkout Completed, Analysis Finished
Kinds of Events
6. No Content Here
(Reserved for Watermark)
Given a customer has created an order
When the customer completes their purchase
Then the customer receives an email confirmation
An example scenario
7. No Content Here
(Reserved for Watermark)
OrderService
Checkout(Order order)
- Save pending order in database
- Send confirmation email
An example solution
9. No Content Here
(Reserved for Watermark)
Given a customer has created an order
When the customer completes their purchase
Then 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
More requirements
10. No Content Here
(Reserved for Watermark)
OrderService
Checkout(Order order)
oAttempt to process payment
oIf payment fails, send notification email; exit
oSave pending order in database
oConfirm inventory is available
oIf insufficient email, send notification email; exit
oSend successful order confirmation email
An example solution
11. No Content Here
(Reserved for Watermark)
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)
Analysis
12. No Content Here
(Reserved for Watermark)
OrderService
Checkout(Order order)
- Dispatch new OrderCompletedEvent(order)
Event Handlers:
OrderPaymentHandler
OrderInventoryHandler
OrderNotificationHandler
An event-driven solution
13. No Content Here
(Reserved for Watermark)
An 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
Event-Driven Programming
14. No Content Here
(Reserved for Watermark)
Model 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)
Domain Events
15. No Content Here
(Reserved for Watermark)
Related concept – not required to take advantage of domain events
Store sequences of events rather than directly mutating object state
Determine current state by replaying events
Learn more: http://martinfowler.com/eaaDev/EventSourcing.html
Event Sourcing
16. No Content Here
(Reserved for Watermark)
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
Event Processing
17. No Content Here
(Reserved for Watermark)
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.
Common Scenario
18. No Content Here
(Reserved for Watermark)
How do you add logic to entities that affects multiple entities?
Don’t solve this by using Dependency Injection on your entities. You should be able to
easily instantiate entities without dependencies.
Avoid: Injecting Dependencies into Entities
19. No Content Here
(Reserved for Watermark)
How do you add logic to entities that affects multiple entities?
Don’t move logic that belongs within an entity into a service just because other
entities are interested in what’s happening. This leads to the anemic domain model
antipattern.
Avoid: Shifting Entity logic to Services
20. No Content Here
(Reserved for Watermark)
How do you add logic to entities that affects multiple entities?
Raise a domain event to represent the action that took place.
Handle the domain event in a handler that collaborates with other entities.
Alternately, have the Aggregate Root register for events raised by members of its
Aggregate.
Use Domain Events (and perhaps Aggregates)
21. No Content Here
(Reserved for Watermark)
C# events
Static DomainEvents helper (Udi Dahan, 2009)
Return, don’t publish, domain events (2013)
Entity-specific event log (Jimmy Bogard, 2014)
Implementations
23. No Content Here
(Reserved for Watermark)
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/
References
24. No Content Here
(Reserved for Watermark)
New Coworking Space in Hudson, Ohio
Private offices / Flex desk spaces
Fiber internet
Complimentary coffee / tea / water
Great development community
@techhubhudson
Website online soon
Tech Hub Hudson
25. No Content Here
(Reserved for Watermark)
Kickstarter Successful
Available November 2016
Order now:
store.DevIQ.com
2017 Software Craftsmanship Calendar
26. No Content Here
(Reserved for Watermark)
No Content Here
(Reserved for Watermark)
No Content Here
(Reserved for Course Previews)
Thanks!
Follow us:
/DevIQPage
DevIQ.com/updates
@deviq
• full-length courses
• samples
• member-only events
Enroll in DevIQ for access to:
DevIQ.com/enroll
Ardalis.com
@ardalis
Steve Smith
Hinweis der Redaktion
Domain Events d
Model object – typically an entity.
Implementing this kind of logic directly within the Customer or Order entity is cumbersome. Moving it into a service may result in an anemic domain model.
Implementing this kind of logic directly within the Customer or Order entity is cumbersome. Moving it into a service may result in an anemic domain model.
Implementing this kind of logic directly within the Customer or Order entity is cumbersome. Moving it into a service may result in an anemic domain model.
Show Domain Events Demo
Tests – LanguageEvents
Udi Domain Events
DbContextSaveChangesShould
Show Domain Events Console
Walk through and run
Show Vet Clinic App