No Content Here (Reserved for Watermark) Decoupling with Domain Events @ardalis Ardalis.com Steve Smith
No Content Here
(Reserved for Watermark)
Decoupling with Domain Events
@ardalisArdalis.com
Steve Smith
No Content Here(Reserved for Watermark)
Pluralsight Courses
http://bit.ly/PS-ssmith
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!
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
No Content Here(Reserved for Watermark)
Application EventsPage Load, Button Click, Window Scroll
System EventsRestart, Backup Completed
Domain EventsAppointment Confirmed, Checkout Completed, Analysis Finished
Kinds of Events
No Content Here(Reserved for Watermark)
Given a customer has created an orderWhen the customer completes their purchaseThen the customer receives an email confirmation
An example scenario
No Content Here(Reserved for Watermark)
OrderService Checkout(Order order) - Save pending order in database - Send confirmation email
An example solution
No Content Here(Reserved for Watermark)
So far, so good
No Content Here(Reserved for Watermark)
Given 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 fulfilledAnd if not a different message is sent to the customer
More requirements
No Content Here(Reserved for Watermark)
OrderServiceCheckout(Order order)oAttempt to process paymento If payment fails, send notification email; exitoSave pending order in databaseoConfirm inventory is availableo If insufficient email, send notification email; exitoSend successful order confirmation email
An example solution
No Content Here(Reserved for Watermark)
Complexity is growing
Must change with each new requirementOpen/Closed Principle
Checkout’s responsibilities are growingSingle Responsibility Principle
Potentially different abstraction levels (emails, order processing rules)
Analysis
No Content Here(Reserved for Watermark)
OrderService Checkout(Order order) - Dispatch new OrderCompletedEvent(order)
Event Handlers:OrderPaymentHandlerOrderInventoryHandlerOrderNotificationHandler
An event-driven solution
No Content Here(Reserved for Watermark)
An EventSomething 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
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
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
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
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
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
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
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)
No Content Here(Reserved for Watermark)
C# eventsStatic DomainEvents helper (Udi Dahan, 2009)Return, don’t publish, domain events (2013)Entity-specific event log (Jimmy Bogard, 2014)
Implementations
No Content Here(Reserved for Watermark)
Demonstration
Implementing Domain Events
No Content Here(Reserved for Watermark)
Pluralsight http://bit.ly/PS-ssmithDomain 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/
ImplementationsUdi 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
No Content Here(Reserved for Watermark)
New Coworking Space in Hudson, OhioPrivate offices / Flex desk spacesFiber internetComplimentary coffee / tea / waterGreat development community
@techhubhudsonWebsite online soon
Tech Hub Hudson
No Content Here(Reserved for Watermark)
Kickstarter SuccessfulAvailable November 2016
Order now:
store.DevIQ.com
2017 Software Craftsmanship Calendar
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