Upload
martraire-cyrille
View
1.968
Download
1
Embed Size (px)
Citation preview
@cyriuxCyrille Martraire
DDDPatternsBeyond the Blue Book
Event Storming Event Sourcing
CQRSDDD
Whatwasbefore?
Isituseful?
CanIgetbetteratDDDbylookingatthingsoutsideofDDD?
Thistalkistheanswer.
Passionate developer Deliberate Designer
PARIS
Since 1999
@cyriuxCyrille Martraire
Paris Software Craftsmanship Community since 2011
http://www.meetup.com/paris-software-craftsmanship/
Software Development
Its history
Software Engineering
Tools, notations, MDA
19
Vendor-sponsored conferences
Fortunately
Eric Evans - Domain-Driven Design
ShoulditbetheonlybooktoreadtolearnDomain-DrivenDesign?
THE ONE & ONLY BOOK?
Anyheadlinethatendsinaquestionmarkcanbeansweredbyno.
EricEvansadvocatesinhisbooktolookoutsideformorepatterns&
inspirations.
Idid.
ForthistalkIhaveselected43patternsandI'mgoingtoexplainthem
onebyonenow.
<Facepalm>
OK.
"The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer.”
— Cunningham’s Law
Ward Cunningham
WIKI XP
CRC Cards
Ward Cunningham
Patterns in software!
http://c2.com/ppr/checks.html
DDD Inspiration
CHECKS Patterns
1. Whole Value 2. Exceptional cases (absence, error…)3. Meaningless Behavior 4. Echo Back 5. Visible Implication 6. Deferred Validation 7. Instant Projection 8. Hypothetical Publication 9. Forecast Confirmation 10. Diagnostic Query Analysis Patterns
25m+10m=35m
(Custom Arithmetics)
Whole Object with unit.
Whole Object
Whole Object
#NoPrimitive
”Becausebits,stringsandnumberscanbeusedtorepresentalmostanything,anyone
inisolationmeansalmostnothing.”
Exceptional Value
NaNUnde:ined
NotApplicable
25m+NaN=NaN
Don’tclutteryourcodewithspecialcasehanding
goingfurther…
Cashflow Sequence +
Cashflow Sequence =
Cashflow Sequence
Monoid
Could we say that?
Hotel Booking Jan 26 to 27 Hotel Booking
Jan 27 to 29
Hotel Booking Jan 26 to 29
+=
Monoid
Monoids Vector Spaces Cyclic Groups
Monoidsadvertisementbroughttoyouby
@cyriux!
CHECKS Patterns Traceability
Normal display: 652 MM USD EQV
Diagnostic display: 622,456,325.07 USD + 3,624,878,450 JPY + 23,549.54 FRF
CHECKS Patterns
1. Whole Value 2. Exceptional cases (absence, error…)3. Meaningless Behavior 4. Echo Back 5. Visible Implication 6. Deferred Validation 7. Instant Projection 8. Hypothetical Publication 9. Forecast Confirmation 10. Diagnostic Query Analysis Patterns
THE WORLD’s
FIRST WIKI
PLoP98
Mind the biblio
In most domain models, most design
patterns are technical noise.
observerSingleton
FactoryCommand
VISITOR
MEDIATOR
Manager
observer
PROXY
FACADE
MEMENTO
But some design patterns also describe business domain concepts and relations.
Composite Interpreter Flyweight Strategy
observerSingleton
FactoryCommand
VISITOR
MEDIATOR
Manager
observer
PROXY
FACADE
MEMENTO
Good-Old hint: ”Use pattern names in type
names.”
In a domain model, avoid pattern names
in types name s.
DateAdjustmentStrategy
Strategy
DateAdjustment DayShiftMethod EndOfMonthRule RoundingConvention CalculationMode
Strategy
DateAdjustment DayShiftMethod EndOfMonthRule RoundingConvention CalculationMode
Strategy
Belongs to the Ubiquitous Language
ReimbursementPolicy EligibilityCriterion AcceptanceSpecification
Strategy
Belongs to the Ubiquitous Language
AccountingValuation NetPresentValueValuation LinearValuation BenchmarkBasedValuation AssetBasedValuation
Strategy
AccountingValuations NetPresentValueValuation LinearValuation BenchmarkBasedValuation AssetBasedValuation
Strategy
Belongs to the Ubiquitous Language
AccountingValuations NetPresentValueValuation LinearValuation BenchmarkBasedValuation AssetBasedValuation
Strategy
No Noise
ValuationImpl
Strategy
Strategy PatternVAT
VAT on net price
Not Applicable
Calculation
+ Null Object
Null Object.
No, you should not replace every
Null Object with Optional<T>
When to apply a pattern? (or not)
IF Statements vs. Strategy pattern?
Naming-Driven
Whichever reveals the business domain best
AccountingValuation NetPresentValueValuation LinearValuation BenchmarkBasedValuation AssetBasedValuation
Strategy
DateAdjustmentStrategy
Domain Patterns
#NotInMyName
Annotate classes with custom annotationsStereotypes Annotations
Stereotypes AnnotationsAnnotate classes with custom annotations
Domain type names are precious. They are for
domain naming.
Use annotations to document the
stereotypes instead.
Stereotypes AnnotationsAnnotate classes with custom annotations
Embedded Learning
Stereotypes AnnotationsAnnotate with custom annotations
Stereotypes AnnotationsAnnotate with custom annotations
Stereotypes AnnotationsAnnotate domain classes with custom annotations
Stereotypes AnnotationsAnnotate domain classes with custom annotations
And learn on the job!
https://leanpub.com/livingdocumentation
BUY MY BOOK!
LIVING DOCUMENTATION
AndnowformoreObject-OrientedPatterns
But Cyrille, Nobody does
Object-Oriented anymore!
NOW WE DO FP!
#NoPAttern
EvergreenSKILLS
Evergreen skills are worth learning
Sonow…IntroducingtheFlyweight
pattern!
Flyweight
Intent:Usesharingtosupportlargenumbersof[ine-grainedobjectsef[iciently
”Hahaha. Nobody uses the
Flyweight”
A matter of Compression
= 3x
503010 …
Flyweight
BUY SELL
503010 …
Flyweight
We always want to make multiplicity manageable
503010 …
-503010 …
Flyweight
-503010 …
{10, 30, -50 …}
Flyweight
{10, 30, -50 …}
∑ = -10
Flyweight
∑ = -10
manageable = aggregable, comparable…
Flyweight
Equity Trading:
Standard Stock
Multiple Trades of the same
$10M-
$30M- -
$50Mx
$10MIBM
$30MIBM AXA
$50M
Derivative Trading:
Multiple Contracts with a common structure
$10MIBM
$30MIBM AXA
$50M
Derivative Trading:
x
$10MIBM
$30MIBM AXA
$50M
The essence of Flyweight
x
Many small, calculable elements One BlackboxIntrinsicExtrinsic
#WIN
Flyweight
#WIN
This never works.
I see what you mean…
BUT no way, IT DOES NOT WORK from a legal
perspective!
FATAL
OBJECTION
FATAL
OBJECTION as hint of bounded
Context!
Tip: Focus on one context
=
≠≠
Works in this context only
=
≠≠
Legal
Money Management
It works in my context!
=≠
Transport Regulation
Cargo Shipping
In what PERSPECTIVE could
we say that?
Bounded Context
Different Bounded Contexts,
Different Patterns.
Different Patterns, Different Bounded
Contexts.
#WorksInMyContext
HEY CYRILLE
WHY U SOFASCINATEDwith patterns?
Would you build Photoshop only
with IF statements and FOR loops?
for (...) if (...)
if (..) {
for... }
else else
while
for (...) if (...)
if (..) {
for... }
else else
while
You should have a look at
design patterns!
Strategy+
State=
EASY AGAIN!
Patterns
ABUSE (Like every beginner)
ThenIdiscoveredMartinFowler
Event Sourcing-ish
Retail Banking
Medical Care
Traceability
Patterns illustrate deeper principles.
Traceability always points to the Past.
(aka. Consequences depend on the Cause)
ORDERBASKET
PAYMENTRETURN
timerefers torefers torefers to
causality
e-commerce
Study the patterns. Digest the principles.
Apply to new domains.
CLAIMINSURANCE
POLICYLOSS
$ PAYMENT
timerefers torefers torefers to
causality
Insurance
EvergreenSKILLS
Different domains, same principle
SuppleDomainModels
BespokeCode
Bespoke, Custom. OK.
Reinventing the Wheel. NOT OK.
https://martinfowler.com/eaaDev/timeNarrative.html
Temporal Property
https://martinfowler.com/eaaDev/timeNarrative.html
Snapshot
https://martinfowler.com/eaaDev/timeNarrative.html
Temporal Object
Effectivity Range
Agreement v1 - effectivity: [01/2016, 01/2017[ Agreement v2 - effectivity: [01/2017, 01/2018[
Agreement v3 - effectivity: [01/2018, - [
Invariant:rangesarecontinuous&non-overlapping
Effectivity Range
Agreement v1 - effectivity: [01/2016, 01/2017[ Agreement v2 - effectivity: [01/2017, 01/2018[
Agreement v3 - effectivity: [01/2018, - [
06/2017
Invariant:rangesarecontinuous&non-overlappingWhat version was
effective on this date?
https://martinfowler.com/eaaDev/timeNarrative.html
Bi-temporal Time
Time has two dimensions in software!
[…] you may have only additive changes. An additive change
always goes onto the end of the record.
https://martinfowler.com/eaaDev/timeNarrative.html
Append-onlyCompensation
[…] you may have only additive changes. An additive change
always goes onto the end of the record.
https://martinfowler.com/eaaDev/timeNarrative.html
Event-Sourcing-ish
Excellent Design Teaching Material
Learn the history, not just the end state.
Learning Steps
(à la Event Sourcing)
Know the Holy Family
:)
Illustrious Ancestors
Rebecca J. Wirfs-Brock
First RDD paper in 1989!
http://tonyprusac.blogspot.fr/2015/11/deco2300-week-6.html
CRC Cards
Event Storming Ancestor
@mathiasverraes@ziobrando
MOAR ANALYSISPATTERNS
TO DESIGNDOMAINMODELS
Not just CodeAlso Ideas!
Reuse all the things!
And past insights
164
A Business Transaction
A quantity of something (e.g. cash)
A quantity of something
else
165
A Business Transaction
A quantity of something (e.g. cash)
A quantity of something
else
Law: one side has to be cash
166
A Business Transaction
A quantity of something (e.g. cash)
A quantity of something
else
Received Date
Paid Date
Trade Date
Congratulations! You can now reuse
this domain insight!
WHY NOT WRITE YOUR OWN PATTERNS?
SO MANY ANALYSISPATTERNS
TO DESIGNDOMAINMODELS
Most developers have never seen a
domain model. They've only seen
data models.
WHAT’S A DOMAIN MODEL?
Let’s take an example
Loan Approval
Loan Application
System
Credit Bureau
Signatures
Requests Approval
100k$
Approved/ Rejected
Loan Approval
Loan Application
System
Credit Bureau
Signatures
Requests Approval
100k$change
to90k$
Accept with in-place mutation :(
Do you approve
this 100k$ loan?
I approved this 90k$
loan!
Do you approve
this 100k$ loan?
… What do I do
with that now?!!
Answering a different question is
rude!
(plus sync back the mutation is not so easy)
What if we just reject with a
suggestion to change the amount before
re-submit?
In other words:
”Single Path to Approval”
Jez Humble and David Farley, “Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation”, Addison Wesley Professional, 2010
In other words:
”Single Path to Approval”Production
Jez Humble and David Farley, “Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation”, Addison Wesley Professional, 2010
Continuous Delivery
Loan Approval
Loan Application
System
Credit Bureau
Signatures
Reject + suggest reduced amount
Requests Approval
100k$OK for
90k$x
Approved/ Rejected
No patch here!Fix the source then commit as usual
No way! It would delay the process!
Plus we don’t want to ask approvers to sign
again!
Business
OK.
Still, perhaps we could do it anyway…
Dev Team
OK for 100k$
➜ a fortiori OK for 90k$
➜
”A fortiori principle”No need to sign again!
Now we need to codify accurately what a fortiori means.
That’s modeling the domain.
Not that hard: Lower risk -> a fortiori OK.
Domain Modeling means forming an opinion on how it
behaves.
Programming as Theory-Building
— Peter Naur (1985)
Anything can be an inspiration.
Even Continuous Delivery patterns!
Let’s take another example
Only rebuild what needs to be rebuilt after a change
Given a set of source code files
http://maemo.org/maemo_release_documentation/maemo4.1.x/node5.html
http://maemo.org/maemo_release_documentation/maemo4.1.x/node5.html
http://maemo.org/maemo_release_documentation/maemo4.1.x/node5.html
Imagine doing that with a workflow
engine?
GNU Make has an opinion on how to
consider the problem.
Declare the dependencies. Let the tool find out the
workflow.
http://maemo.org/maemo_release_documentation/maemo4.1.x/node5.html
http://maemo.org/maemo_release_documentation/maemo4.1.x/node5.html
ReviewRisk checkLegal
check
Management Review
Management Review
Product Launch
R&D Done Sales
ReadyMarketing Campaign
Product SpecsProduct Design
A breakthrough in domain modeling
Why insist on using workflow engines
everywhere?
Do you have an opinion on how to
consider the business domain?
(Hint: ”We need Event Sourcing” is probably not the
best answer)
Which pattern?
”Propose several models, choose one.”
Design Spike.Try them.
Which pattern?
from simple to less simple.
Teaching Patterns to your team mates
Mob-Programming
@woodyzuill
https://www.industriallogic.com/blog/design-patterns-playing-cards/
Design Patterns Poker cards
Design Patterns Poker cards
Show the benefits.Don’t preach.
Own Internal Branding
You don’t have to talkFlyweight or Monoid
The best code sample for training is your own code base.
Embedded Learning
YOU DIDN’T SEE ALL THESE PATTERNS
BEFORE?
You probably procrastinate on
tech stuff.
No, you probably don’t need this Event Sourcing
here.
You probably should spend more time
Domain Model
Technical Infrastructure
HERE
Design is hard? Do it more often!
From Scratch? Lack of domain maturity makes everything look
CRUD.
Mature Legacy Systems FTW!
NEW HORIZONS
DCI by Jim Coplien #DDDEU
https://blog.acolyer.org/ http://paperswelove.org/
In closing
Many patterns authors. Many patterns.
They converge, mostly. No need to learn them all.
Many patterns authors. Many patterns.
Still, learn more than one!
So many patterns outside the Blue Book
But do you really know all the patterns in the Blue
Book?
Merci !
h$p://cathy313.centerblog.net/539-bisounours
Follow me @cyriux
Slides: slideshare.net/cyriux Blog: cyrille.martraire.com
We offer training, coaching & consulting on DDD, BDD, TDD