Decoupling with Domain Events
-
Upload
steven-smith -
Category
Software
-
view
718 -
download
2
Transcript of Decoupling with Domain Events
![Page 1: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/1.jpg)
No Content Here
(Reserved for Watermark)
Decoupling with Domain Events
@ardalisArdalis.com
Steve Smith
![Page 2: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/2.jpg)
No Content Here(Reserved for Watermark)
Pluralsight Courses
http://bit.ly/PS-ssmith
![Page 3: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/3.jpg)
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!
![Page 4: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/4.jpg)
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
![Page 5: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/5.jpg)
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
![Page 6: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/6.jpg)
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
![Page 7: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/7.jpg)
No Content Here(Reserved for Watermark)
OrderService Checkout(Order order) - Save pending order in database - Send confirmation email
An example solution
![Page 8: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/8.jpg)
No Content Here(Reserved for Watermark)
So far, so good
![Page 9: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/9.jpg)
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
![Page 10: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/10.jpg)
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
![Page 11: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/11.jpg)
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
![Page 12: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/12.jpg)
No Content Here(Reserved for Watermark)
OrderService Checkout(Order order) - Dispatch new OrderCompletedEvent(order)
Event Handlers:OrderPaymentHandlerOrderInventoryHandlerOrderNotificationHandler
An event-driven solution
![Page 13: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/13.jpg)
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
![Page 14: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/14.jpg)
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
![Page 15: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/15.jpg)
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
![Page 16: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/16.jpg)
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
![Page 17: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/17.jpg)
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
![Page 18: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/18.jpg)
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
![Page 19: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/19.jpg)
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
![Page 20: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/20.jpg)
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)
![Page 21: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/21.jpg)
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
![Page 22: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/22.jpg)
No Content Here(Reserved for Watermark)
Demonstration
Implementing Domain Events
![Page 23: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/23.jpg)
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
![Page 24: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/24.jpg)
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
![Page 25: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/25.jpg)
No Content Here(Reserved for Watermark)
Kickstarter SuccessfulAvailable November 2016
Order now:
store.DevIQ.com
2017 Software Craftsmanship Calendar
![Page 26: Decoupling with Domain Events](https://reader036.fdocuments.net/reader036/viewer/2022062503/587ac5861a28abc0478b7c85/html5/thumbnails/26.jpg)
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