domain events

21
Domain Events Steve Smith @ardalis ardalis.com

Upload: steven-smith

Post on 15-Jan-2017

451 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Domain events

Domain EventsSteve Smith

@ardalisardalis.com

Page 2: Domain events

Pluralsight http://bit.ly/PS-ssmith

Page 3: Domain events

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

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

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

An example scenarioGiven a customer has created an orderWhen the customer completes their purchaseThen the customer receives an email confirmation

Page 7: Domain events

An example solutionOrderService Checkout(Order order) - Save pending order in database - Send confirmation email

Page 8: Domain events

So far, so good

Page 9: Domain events

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

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

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

An event-driven solutionOrderService Checkout(Order order) - Dispatch new OrderCompletedEvent(order)

Event Handlers:• OrderPaymentHandler• OrderInventoryHandler• OrderNotificationHandler

Page 13: Domain events

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

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

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

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

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

DemosImplementing Domain Events

Page 19: 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.

Page 20: Domain events

Questions?

Page 21: Domain events

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/