$30 off During Our Annual Pro Sale. View Details »

Domain-Driven Design (Tutorial)

Domain-Driven Design (Tutorial)

Tutorial »Domain-Driven Design«

Henning Schwentner

September 26, 2023
Tweet

More Decks by Henning Schwentner

Other Decks in Programming

Transcript

  1. Starring With
    Directed by
    In the role of
    Also Starring

    View Slide

  2. View Slide

  3. View Slide

  4. @hschwentner
    Domain-Driven Design

    View Slide

  5. @hschwentner
    #dddesign

    View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. »Business-Software
    die Spaß macht«

    View Slide

  12. View Slide

  13. View Slide

  14. @hschwentner
    Timetable

    View Slide

  15. Workshop
    Workshop
    Workshop
    Workshop
    Morning Afternoon
    Not remote

    View Slide

  16. Morning Afternoon
    Wor
    ksho
    p
    Wor
    ksho
    p
    Wor
    ksho
    p
    Wor
    ksho
    p
    Wor
    ksho
    p
    Wor
    ksho
    p
    Wor
    ksho
    p

    View Slide

  17. @hschwentner
    Contents

    View Slide

  18. View Slide

  19. @hschwentner
    #unabsteigbar

    View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. Workshop
    Domain-Driven Design concrete
    wps.de/ddd

    View Slide

  25. Ubiquitous Language

    View Slide

  26. Ubiquitous Language
    Building Blocks Domain Event
    Aggregate
    Entity
    Value Object
    Bounded Context
    Strategic Design
    Context Mapping
    Collaborative Modeling
    Domain Expert
    Event Storming
    Modeling in Code
    Domain Storytelling
    Core Domain
    Domain Model

    View Slide

  27. Microservices
    CQRS
    Hexagonal Architecture
    Agile
    Event Sourcing
    Extreme Programming
    Scrum
    Cloud
    Self-Contained Systems
    Verticals
    Clean Architecture
    DevOps AWS
    Azure
    Onion Architecture

    View Slide

  28. @hschwentner
    Structure

    View Slide

  29. View Slide

  30. 26.09.23 //// Seite 71
    WPS – Workplace Solutions
    DOMÄNE
    DOMÄNE

    View Slide

  31. @hschwentner
    You

    View Slide

  32. Programming Languages
    My Questions
    My life as a developer
    My experiences using DDD
    My wishes regarding the training
    family
    My first computer

    View Slide

  33. Story Time

    View Slide

  34. View Slide

  35. View Slide

  36. Grafik: Robert Gloth/Wikipedia

    View Slide

  37. View Slide

  38. Foto: Serge Melki/Wikipedia

    View Slide

  39. Foto: Public Domain/Wikipedia

    View Slide

  40. View Slide

  41. @hschwentner
    How is that supposed to work?

    View Slide

  42. View Slide

  43. View Slide

  44. Foto: Henning Schwentner

    View Slide

  45. View Slide

  46. Detail: Depth

    View Slide

  47. Detail: Tiefenzahl

    View Slide

  48. Foto: Afrank99/Wikipedia

    View Slide

  49. Foto: Henning Schwentner

    View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. PUTS
    NAVIGATING
    OFFICER
    CAPTAIN
    ASKS FOR
    7
    SHIP
    SILHOUETTE
    ON
    CARTO-
    GRAPHER
    SOUNDING SHIP
    DEPTH
    MEASURES
    1
    SENDS
    2
    TO
    CALCULATES
    3
    DRAW
    S
    4
    SENDS
    5
    DEPTH MAP
    TO
    MOVES & TURNS
    8
    SHIP
    SILHOUETTE
    TO FIND
    9
    DISCUSSES WITH
    6
    ROUTE
    ROUTE
    DEPTH MAP
    DEPTH MAP
    DEPTH (RAW)
    ROUTE
    CONTOUR
    LINES

    View Slide

  56. @hschwentner
    Is There an App for It?

    View Slide

  57. View Slide

  58. View Slide

  59. CYBER

    View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. View Slide

  66. @hschwentner
    Software != End in itself

    View Slide

  67. View Slide

  68. ?

    View Slide

  69. View Slide

  70. IMPORTANT!

    View Slide

  71. View Slide

  72. Software
    Domain

    View Slide

  73. Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

    View Slide

  74. View Slide

  75. Domain
    Knowledg
    e
    Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

    View Slide

  76. View Slide

  77. Groß
    Klein

    View Slide

  78. Collaborative
    Modeling

    View Slide

  79. Domain
    Expert
    Developer

    View Slide

  80. Knowledge crunching

    View Slide

  81. View Slide

  82. @hschwentner
    Methods

    View Slide

  83. View Slide

  84. https://domainstorytelling.org

    View Slide

  85. LANGUAGE

    View Slide

  86. Fach-
    sprache
    Domain
    Expert
    Developer

    View Slide

  87. =>
    direct
    commu-
    nication
    telephone
    game
    to
    user
    dev
    user dev

    View Slide

  88. Ludwig Wi)genstein
    “Whereof one cannot
    speak, thereof one
    must be silent.”

    View Slide

  89. Ludwig Wittgenstein
    »Wovon man nicht
    sprechen kann, darüber
    muss man schweigen.«

    View Slide

  90. Depth
    Scale
    Silhouette
    Marking
    Depth Map
    to move
    to measure
    run aground
    neon yellow

    View Slide

  91. Ubiquitous Language

    View Slide

  92. Jubikjutuss Lengwitsch

    View Slide

  93. PUTS
    NAVIGATING
    OFFICER
    CAPTAIN
    ASKS FOR
    7
    SHIP
    SILHOUETTE
    ON
    CARTO-
    GRAPHER
    SOUNDING SHIP
    DEPTH
    MEASURES
    1
    SENDS
    2
    TO
    CALCULATES
    3
    DRAW
    S
    4
    SENDS
    5
    DEPTH MAP
    TO
    MOVES & TURNS
    8
    SHIP
    SILHOUETTE
    TO FIND
    9
    DISCUSSES WITH
    6
    ROUTE
    ROUTE
    DEPTH MAP
    DEPTH MAP
    DEPTH (RAW)
    ROUTE
    CONTOUR
    LINES

    View Slide

  94. SMALL

    View Slide

  95. View Slide

  96. class Silhouette {
    //...
    }

    View Slide

  97. View Slide

  98. class Silhouette {
    public void moveBy(Length length)
    //...
    }

    View Slide

  99. View Slide

  100. class Silhouette {
    public void moveBy(Length length)
    //...
    public void turnBy(Angle angle)
    //...
    }

    View Slide

  101. Erich Gamma
    “Program to an interface,
    not to an implementation”

    View Slide

  102. class Silhouette {
    public void setPosition(Position p)
    //...
    }

    View Slide


  103. View Slide

  104. WARNING
    SETTERS
    CAUSE
    MODEL
    ANEMIA

    View Slide

  105. MOVES & TURNS
    8
    SHIP
    SILHOUETTE
    TO FIND
    ROUTE
    NAUTICAL
    OFFICER
    «Entity»
    Silhouette
    move()
    turn()

    View Slide

  106. Tactical Design

    View Slide

  107. View Slide

  108. Entity

    View Slide

  109. View Slide

  110. import org.jmolecules.ddd.annotation.*;
    @Entity
    public class DepthMap {
    //...
    }

    View Slide

  111. Value Object

    View Slide

  112. Example: Depth

    View Slide

  113. import org.jmolecules.ddd.annotation.*;
    @ValueObject
    public record Depth(/* ... */)

    View Slide

  114. Domain Event

    View Slide

  115. MANEUVER
    PLANNING
    DEPTHS SOUNDING
    DEPTH MAP
    COMPLETED

    View Slide

  116. BIG

    View Slide

  117. Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

    View Slide

  118. Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

    View Slide

  119. View Slide

  120. View Slide

  121. View Slide

  122. #

    View Slide

  123. View Slide

  124. View Slide

  125. Bounded Context

    View Slide

  126. #

    View Slide

  127. PUTS
    NAVIGATING
    OFFICER
    CAPTAIN
    ASKS FOR
    7
    SHIP
    SILHOUETTE
    ON
    CARTO-
    GRAPHER
    SOUNDING SHIP
    DEPTH
    MEASURES
    1
    SENDS
    2
    TO
    CALCULATES
    3
    DRAW
    S
    4
    SENDS
    5
    DEPTH MAP
    TO
    MOVES & TURNS
    8
    SHIP
    SILHOUETTE
    TO FIND
    9
    DISCUSSES WITH
    6
    ROUTE
    ROUTE
    DEPTH MAP
    DEPTH MAP
    DEPTH (RAW)
    ROUTE
    CONTOUR
    LINES
    DEPTH
    MEASURE-
    MENT
    MANEUVER
    PLANNING

    View Slide

  128. TIDAL
    FORE-
    CAST
    MANEUVER
    PLANNING
    DEPTH
    MEASUREMENT

    View Slide

  129. Ubiquitous Language

    View Slide

  130. View Slide

  131. WPS – Workplace Solutions #dddkonkret www.wps.de
    COLLABORATIVE MODELING
    Domain-Driven Design concrete

    View Slide

  132. 26.09.23 //// Seite 235
    WPS – Workplace Solutions
    INHALT
    DOMÄNE
    DOMÄNE

    View Slide

  133. View Slide

  134. Collaborative Modeling

    View Slide

  135. Collaborative Modeling

    View Slide

  136. https://www.domainstorytelling.org

    View Slide

  137. LEGT
    NAUTIKER KAPITÄN
    FRAGT NACH
    7
    SCHIFFS-
    SILHOUETTE
    AUF
    PEILDIENST
    PEILSCHIFF
    TIEFE
    PEILT
    1
    SENDET
    2
    AN
    BERECHNET
    3
    ERZ
    EUGT
    4
    SENDET
    5
    PEILPLAN
    AN
    VERSCHIEBT &
    DREHT
    8
    SCHIFFS-
    SILHOUETTE
    UND
    FINDET
    9
    MELDET AN
    6
    ROUTE
    ROUTE
    PEILPLAN
    PEILPLAN
    TIEFEN-
    ZAHLEN
    ROUTE
    TIEFENLINIEN

    View Slide

  138. View Slide

  139. View Slide

  140. @hschwentner
    Explained

    View Slide

  141. @hschwentner
    Domain Storytelling =
    Pictographic Language +
    Workshop Format

    View Slide

  142. @hschwentner
    The
    Workshop
    Format

    View Slide

  143. @hschwentner
    The right people

    View Slide

  144. View Slide

  145. Storyteller
    Listeners
    !
    !

    View Slide

  146. View Slide

  147. View Slide

  148. @hschwentner
    DEPTH MAP
    HANDS OVER
    NAUTICAL
    OFFICER
    CARTOGRAPHER
    TO
    “The cartographer sends
    the
    depth map to the nautical
    officer”
    SENDS

    View Slide

  149. Active Listening

    View Slide

  150. CONCRETE STORIES VS.
    ABSTRACT PROCESSES

    View Slide

  151. @hschwentner
    The
    Pictographic
    Language

    View Slide

  152. @hschwentner
    ßDraw here
    Leave some empty space there à
    Annota3ons and
    varia3ons
    Preconditions and
    assumptions
    Name of the Domain Story

    View Slide

  153. @hschwentner
    actor work object activity
    5
    sequence
    number

    View Slide

  154. @hschwentner
    SHIP
    SILHOUETTE
    CAPTAIN MOVES

    View Slide

  155. @hschwentner
    DRAWS
    MEASURES
    DEPTH
    DEPTH MAP

    View Slide

  156. @hschwentner
    SENDS
    DRAWS
    DEPTH MAP
    DEPTH MAP

    View Slide

  157. @hschwentner
    person group IT system

    View Slide

  158. ACTORS ONCE/
    WORK OBJECTS SEVERAL TIMES
    PUTS
    NAUTICAL
    OFFICER
    7
    SHIP
    SILHOUETTE
    MOVES & TURNS
    8
    SHIP
    SILHOUETTE

    View Slide

  159. PICTOGRAPHIC LANGUAGE –
    NO IF/SWITCH/OR
    actor work object ac3vity
    5
    sequence
    number

    View Slide

  160. Scenario-based
    modeling

    View Slide

  161. Scenarios
    Maneuver Planning – The Happy Path
    Maneuver Planning – Storm Flooding Expected
    Maneuver Planning – All Berths Full

    View Slide

  162. @hschwentner
    Scope

    View Slide

  163. Scope Factors

    View Slide

  164. @hschwentner
    Granularity
    Point in time
    Domain Purity

    View Slide

  165. Granularity
    fine-
    grained
    coarse-
    grained
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x

    View Slide

  166. @hschwentner
    A Day at the Beach
    " Sea Level
    # Kite Level
    ☁ Cloud Level
    % Fish Level
    & Clam Level
    Foto: Dennis Hamilton/flickr/CC BY 2.0
    Alistair
    Cockburn

    View Slide

  167. Point in Time
    as-is to-be
    t

    View Slide

  168. Point in Time
    now optimized
    t
    with new
    system

    View Slide

  169. Domain Purity
    pure digitalized

    View Slide

  170. @hschwentner
    Tools

    View Slide

  171. View Slide

  172. PowerPoint

    View Slide

  173. @hschwentner

    View Slide

  174. https://www.wps.de/modeler/
    • https://egon.io

    View Slide

  175. @hschwentner
    Purposes

    View Slide

  176. @hschwentner
    draw boundaries
    learn language
    work on
    requirements
    implement domain
    model
    find shadow IT …

    View Slide

  177. @hschwentner
    More on
    Domain Storytelling

    View Slide

  178. View Slide

  179. More info:
    https://hschwentner.io

    View Slide

  180. 26.09.23 //// Seite 327
    WPS – Workplace Solutions
    EXERCISE:
    Domain
    Storytelling in
    Groups

    View Slide

  181. WPS – Workplace Solutions #dddkonkret www.wps.de
    STRATEGIC DESIGN
    Domain-Driven Design concrete

    View Slide

  182. 26.09.23 //// Seite 330
    WPS – Workplace Solutions
    ÜBERSICHT
    DOMÄNE
    DOMÄNE

    View Slide

  183. ?

    View Slide

  184. ?

    View Slide

  185. View Slide

  186. View Slide

  187. View Slide

  188. View Slide

  189. View Slide

  190. ?

    View Slide

  191. ?

    View Slide

  192. View Slide

  193. @hschwentner
    “Whoever uses a canonical model
    has lost control over his life.”
    – Karl Lagerfeld

    View Slide

  194. @hschwentner
    »Wer ein unternehmensweites
    Domänenmodell einsetzt, hat die
    Kontrolle über sein Leben verloren«
    – Karl Lagerfeld

    View Slide

  195. View Slide

  196. !

    View Slide

  197. View Slide

  198. #

    View Slide

  199. View Slide

  200. View Slide

  201. View Slide

  202. View Slide

  203. Bounded Context

    View Slide

  204. #

    View Slide

  205. Bewegung
    Kosmetik
    #
    Gefühle

    View Slide

  206. PUTS
    NAVIGATING
    OFFICER
    CAPTAIN
    ASKS FOR
    7
    SHIP
    SILHOUETTE
    ON
    CARTO-GRAPHER
    SOUNDING SHIP
    DEPTH
    MEASURES
    1
    SENDS
    2
    TO
    CALCULATES
    3
    DRAW
    S
    4
    SENDS
    5
    DEPTH MAP
    TO
    MOVES & TURNS
    8
    SHIP SLHOETTE
    TO FIND
    9
    DISCUSSES WITH
    6
    ROUTE
    ROUTE
    DEPTH MAP
    DEPTH MAP
    DEPTH (RAW)
    ROUTE
    CONTOUR
    LINES
    DEPTH
    MEASURE-
    MENT
    MANEUVER
    PLANNING

    View Slide

  207. @hschwentner
    Another Example

    View Slide

  208. View Slide

  209. View Slide

  210. @hschwentner
    Do I get a car for this?

    View Slide

  211. View Slide

  212. 26.09.23 //// Seite 360
    WPS – Workplace Solutions

    View Slide

  213. 26.09.23 //// Seite 361
    WPS – Workplace Solutions

    View Slide

  214. CUSTOMER
    TELLS WISH
    FOR
    1
    SALES-
    PERSON
    SIGNS
    TO
    GIVES
    FOR
    CONTRACT
    3
    RISK MANAGER
    CONTRACT
    PASSES ON
    TO
    4
    CONTRACT
    VO
    TES
    CHECKS
    CALCU
    LATES
    5
    6
    7
    CALCU-
    LATES
    TO
    8
    2
    CAR
    CREDIT
    RATING
    INSTALLMENT
    CAR
    RESALE
    VALUE
    CONTRACT

    View Slide

  215. CUSTOMER
    SIGNS
    CONTRACT
    3
    RISK MANAGER
    CONTRACT
    VOTES
    7
    «Entity»
    Contract
    sign()
    vote()

    View Slide

  216. «Entity»
    Contract
    sign()
    vote()

    View Slide

  217. «Entity»
    Contract
    sign()
    vote()
    extend()
    terminate()
    sell_to_refinance()

    View Slide

  218. BBOM
    Foto: Benutzer:Summi/Wikipedia/CC-BY-SA-3.0-migrated

    View Slide

  219. View Slide

  220. View Slide

  221. @hschwentner
    Why is that?

    View Slide

  222. View Slide

  223. View Slide

  224. @hschwentner
    Too big
    to be understood
    as a whole

    View Slide

  225. View Slide

  226. View Slide

  227. 26.09.23 //// Seite 375
    WPS – Workplace Solutions
    CUSTOMER
    TELLS WISH FOR
    1
    SALES-
    PERSON
    SIGNS
    TO
    GIVES
    FOR
    CONTRACT
    3
    RISK MANAGER
    CONTRACT
    PASSES ON
    TO
    4
    CONTRACT
    VOTES
    CHECKS
    CALCULATES
    5
    6
    7
    CALCU-
    LATES
    TO
    8
    2
    CAR
    CREDIT
    RATING
    INSTALLMENT
    CAR
    RESALE
    VALUE
    CONTRACT
    SALES
    RISK ASSESSMENT

    View Slide

  228. «En$ty»
    Contract
    sign()
    vote()

    View Slide

  229. «En$ty»
    Contract
    sign()
    «En$ty»
    Contract
    vote()

    View Slide

  230. «En$ty»
    Contract
    sign()
    vote()

    View Slide

  231. «En$ty»
    Contract
    sign()
    «Entity»
    Contract
    vote()

    View Slide

  232. «En$ty»
    Contract
    sign()
    «En$ty»
    Contract
    vote()
    SALES
    RISK
    ASSESSMENT

    View Slide

  233. 26.09.23 //// Seite 381
    WPS – Workplace Solutions
    Common heuristics:
    § Points of no Return (persistent work results)
    § Boundaries within the process
    § State changes, that affect the “nature” of a
    work object (e.g. “contract legally binding,”
    “shopping cart ordered”)
    § Departments of the organisation
    § Contextual language
    § Different usage of work objects
    § Triggers at different points in time
    § NOT: by work objects!
    HOW TO CUT THE DOMAIN?
    Foto: Wikipedia/PD-ScottForesman

    View Slide

  234. 26.09.23 //// Seite 382
    WPS – Workplace Solutions

    View Slide

  235. 26.09.23 //// Seite 383
    WPS – Workplace Solutions
    Team Head
    Team Tail
    Team Body
    Team Legs

    View Slide

  236. 26.09.23 //// Seite 384
    WPS – Workplace Solutions
    Team Emma
    Team Berta
    Team Erna
    Team Lisl

    View Slide

  237. @hschwentner
    Code
    Database
    Team

    View Slide

  238. @hschwentner
    Drawing Boundaries
    with Domain
    Storytelling

    View Slide

  239. @hschwentner
    Scope:
    coarse-grained
    to-be (optimized)
    pure

    View Slide

  240. Which activities
    belong together
    (from an actor’s
    perspective)?

    View Slide

  241. CUSTOMER
    TELLS WISH
    FOR
    1
    SALESPERSON
    SIGNS
    TO
    GIVES
    FOR
    CONTRACT
    3
    RISK MANAGER
    CONTRACT
    PASSES ON
    TO
    4
    CONTRACT
    VO
    TES
    CHECKS
    CALCU
    LATES
    5
    6
    7
    CALCU-
    LATES
    TO
    8
    2
    CAR
    CREDIT
    RATING
    INSTALLMENT
    CAR
    RESALE
    VALUE
    CONTRACT
    SALES
    RISK ASSESSMENT

    View Slide

  242. CUSTOMER
    TELLS WISH
    FOR
    1
    SALESPERSON
    SIGNS
    TO
    GIVES
    FOR
    CONTRACT
    3
    RISK MANAGER
    CONTRACT
    PASSES ON
    TO
    4
    CONTRACT
    VO
    TES
    CHECKS
    CALCU
    LATES
    5
    6
    7
    CALCU-
    LATES
    TO
    8
    2
    CAR
    CREDIT
    RATING
    INSTALLMENT
    CAR
    RESALE
    VALUE
    CONTRACT
    SALES
    Group the sentences
    • Boundary around activites
    and work objects
    • Keep actors outside
    boundaries
    Give a name to the group
    RISK ASSESSMENT

    View Slide

  243. @hschwentner
    Naming Subdomains
    Express what is done
    Verbs turned into nouns
    Often: -ing-form
    Anti-pattern: name of work object
    as name for subdomain

    View Slide

  244. @hschwentner
    Naming Subdomains—
    Examples
    Price
    calculation
    Risk
    assess-
    ment
    Maneuver
    planning
    Customer
    Car
    Price
    Payment
    Car
    inspection

    View Slide

  245. Indicators:
    1) Actor produces result on their own
    2) One-way informa$on flow
    3) Different triggers
    ($me vs. on demand)
    4) Ac$vi$es suppor$ng something that is not
    in the picture
    5) Difference in language
    6) Different use of the same thing
    Ask your
    domain experts!

    View Slide

  246. 26.09.23 //// Seite 406
    WPS – Workplace Solutions
    EXERCISE:
    Find Bounded
    Contexts

    View Slide

  247. 26.09.23 //// Seite 407
    WPS – Workplace Solutions
    BOUNDED CONTEXTS FÜR DAS KINO
    ABLAUF-
    PLANUNG
    KARTEN-
    VERKAUF
    BUCH-
    HALTUNG

    View Slide

  248. 26.09.23 //// Seite 429
    WPS – Workplace Solutions
    AND NOW?
    § Core vs Supporting
    § Event Storming
    § Taktisches Design
    § Code/LeasingNinja
    § Hexagonal/Onion Architecture
    § Context Mapping
    § How to split the monolith

    View Slide

  249. @hschwentner
    How to split the
    monolith

    View Slide

  250. 1) How should it be?
    2) How is it?
    3) How to move the “is” to the “ideal”?
    RISK
    MANAGE-
    MENT
    SALES

    View Slide

  251. 1) How should it be?
    1) Domain Re-Discovery
    2) “ideal” context map
    2) How is it?
    1) Architecture Analysis
    2) As-is context map
    3) How to move the “is” to the “ideal”?
    1) Compare
    2) Create List of Refactorings
    4) Do the move
    1) Extract a suppor$ng domain to learn
    2) Then extract core(s)

    View Slide

  252. old old old old
    new
    new new new
    “just flip
    the lever”
    1 2 3 4 5
    a.k.a.
    big bang
    replacement

    View Slide

  253. old old old
    new
    new new new
    2 3 4 5
    old
    1

    View Slide

  254. Strangler
    Fig
    Application

    View Slide

  255. Strangler Fig Application

    View Slide

  256. Lesson:
    is better than
    Strangler Fig
    Application
    Big Bang
    Replacement

    View Slide

  257. <>
    Contract
    sign()
    SALES
    RISK
    ASSESSMENT
    <>
    Contract
    sign()
    vote()

    BIG BALL
    OF MUD
    <>
    Contract
    vote()

    View Slide

  258. Read on:
    h"ps://hschwentner.io/domain-driven-refactorings

    View Slide

  259. @hschwentner
    Always Microservices?

    View Slide

  260. Simon Brown

    View Slide

  261. Distributed
    Big Ball Of Mud
    Modulith Microservices
    Monolithic
    Big Ball Of Mud

    View Slide

  262. View Slide

  263. @hschwentner
    Consulting

    View Slide

  264. 26.09.23 //// Seite 509
    WPS – Workplace Solutions
    § Subdomänen
    § Kern (Core Domain)
    § Unterstützende (Supporting Domain)
    § Allgemeine (Generic Domain)
    § Context Mapping
    § Shared Kernel
    § Customer/Supplier
    § Open-Host-Service
    § Published Language
    § Separate Ways
    § Anticorruption Layer
    § Conformist
    STRATEGISCHES DESIGN – WEITERE BEGRIFFE

    View Slide

  265. WPS – Workplace Solutions #dddkonkret www.wps.de
    UBIQUITOUS LANGUAGE
    Domain-Driven Design konkret

    View Slide

  266. 26.09.23 //// Seite 511
    WPS – Workplace Solutions
    ÜBERSICHT
    DOMÄNE
    DOMÄNE

    View Slide

  267. “There are only two hard things in Computer Science:
    cache invalidation and naming things.”
    Phil Karlton

    View Slide

  268. “There are only two hard things in Computer Science:
    cache invalidation, naming things,
    and off-by-one errors.”
    Phil Karlton

    View Slide

  269. 26.09.23 //// Seite 514
    WPS – Workplace Solutions
    TECHNISCHE SPRACHE
    Auf den
    Feature
    Branch
    pushen?
    Den
    Container
    hochfahren
    Ich baue
    dafür ein
    Interface

    View Slide

  270. 26.09.23 //// Seite 515
    WPS – Workplace Solutions
    TECHNISCHE SPRACHE
    X
    Class
    The
    picture
    can’t be
    displayed.
    Database
    Server
    Client
    O/R-Mapping
    Inheritance
    A
    B
    Method
    Interface
    Linux
    Windows
    Eclipse
    Visual Studio

    View Slide

  271. 26.09.23 //// Seite 516
    WPS – Workplace Solutions
    DOMÄNEN-SPRACHE – BEISPIEL SCHIFFAHRT
    Tiefenzahl
    Maßstab
    Silhouette
    Peilplan
    Markierung
    Länge ü.a.
    Breite
    Baggertoleranz

    View Slide

  272. 26.09.23 //// Seite 517
    WPS – Workplace Solutions
    DOMÄNEN-SPRACHE – BEISPIEL SCHIFFAHRT
    Container
    Containernummer
    4300
    Kran
    Twistlock
    Frachtbrief

    View Slide

  273. 26.09.23 //// Seite 518
    WPS – Workplace Solutions
    DOMÄNEN-SPRACHE – BEISPIEL SCHACH
    Brett
    König
    Figuren Spieler
    Schachuhr

    View Slide

  274. 26.09.23 //// Seite 519
    WPS – Workplace Solutions
    DOMÄNEN-SPRACHE – BEISPIEL SCHACH II
    Remis
    Zugzwang
    Schachmatt
    Partie
    Zug
    Rochade

    View Slide

  275. 26.09.23 //// Seite 520
    WPS – Workplace Solutions
    Fach-
    sprache
    Techno
    Babble
    ?
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  276. 26.09.23 //// Seite 521
    WPS – Workplace Solutions
    Fach-
    sprache
    Techno
    Babble
    Fach-
    sprache
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  277. 26.09.23 //// Seite 522
    WPS – Workplace Solutions
    Fach-
    sprache
    Techno
    Babble
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  278. 26.09.23 //// Seite 523
    WPS – Workplace Solutions
    Techno
    Babble
    Fach-
    sprache
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  279. 26.09.23 //// Seite 524
    WPS – Workplace Solutions
    Fach-
    sprache
    {
    Techno
    Babble
    }
    ?
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  280. 26.09.23 //// Seite 525
    WPS – Workplace Solutions
    Fach-
    sprache
    {
    Fach-
    sprache
    }
    MOTIVATION ALLGEGENWÄRTIGE SPRACHE

    View Slide

  281. 26.09.23 //// Seite 526
    WPS – Workplace Solutions
    Fach-
    sprache
    {
    Fach-
    sprache
    }
    UBIQUITOUS LANGUAGE
    public class SchiffsSilhouette
    {
    public void verschiebe { … }
    public void drehe { … }

    }

    View Slide

  282. 26.09.23 //// Seite 527
    WPS – Workplace Solutions
    GEMEINSAME SPRACHE
    § Fachexperten verstehen keine Begriffe zu technischen Umsetzungen
    § Fachexperten sprechen den Jargon ihrer Domäne, der für Außenstehende wiederum schwer
    verständlich sein kann
    è Eine gemeinsame Sprache ist notwendig!
    § Welche soll es sein?
    § Die der Entwickler?
    § Die der Fachexperten?
    § Etwas dazwischen?
    èPrinzip von DDD:
    „Verwende eine Sprache die auf
    dem Domänenmodell basiert“

    View Slide

  283. 26.09.23 //// Seite 528
    WPS – Workplace Solutions
    ALLGEGENWÄRTIGE SPRACHE
    § Verwende die gemeinsame Sprache in..
    § der Kommunikation
    § mündlich
    § schriftlich
    § grafisch
    § im Code
    § Im Grunde genommen überall
    è Deswegen nennt sich die Sprache allgegenwärtig.

    View Slide

  284. 26.09.23 //// Seite 529
    WPS – Workplace Solutions
    SPRACHE TAUCHT NICHT EINFACH AUF
    § Es braucht Wochen bis Monate...
    § harter Arbeit
    § und scharfem Fokus
    § … um die Schlüsselkonzepte offenzulegen.
    § Die ersten Wörter einer allgegenwärtigen Sprache kommen üblicherweise direkt aus der Domäne
    § Im Laufe der Entwicklung werden neue Begriffe definiert und hinzugefügt

    View Slide

  285. 26.09.23 //// Seite 530
    WPS – Workplace Solutions
    DOMÄNEN-SPRACHE
    Brett
    König
    Figuren
    Spieler
    Schachuhr

    View Slide

  286. 26.09.23 //// Seite 531
    WPS – Workplace Solutions
    UBIQIUTOUS LANGUAGE
    Brett
    Figuren
    Spieler

    View Slide

  287. 26.09.23 //// Seite 532
    WPS – Workplace Solutions
    UBIQUITOUS LANGUAGE – ITERATION 2
    Figuren
    Spieler
    Nichtmenschlicher
    Spieler
    Brett

    View Slide

  288. 26.09.23 //// Seite 533
    WPS – Workplace Solutions
    SPRACHEN SIND LEBENDIG
    § Experimentiere mit alternativen Ausdrucksformen
    § Das Modell und die Sprache entwickeln sich weiter
    § Überarbeite dann den Code
    § Benenne Klassen, Methoden, Module
    § Entspreche dem neuen Modell
    § Eine Sprache will gesprochen werden:
    § Beseitige Unklarheiten durch Konversation

    View Slide

  289. View Slide

  290. View Slide

  291. View Slide

  292. http://www.domainstorytelling.or

    View Slide

  293. View Slide

  294. 26.09.23 //// Seite 539
    WPS – Workplace Solutions
    GLOSSAR
    § Fachsprache der Benutzer/Ubiquitous Language
    •bereits existierende Begriffe
    •rekonstruierte Begriffe
    •neue Begriffsbildungen
    • Wer tut was damit wozu?
    § Kernkonzepte
    § Wichtiger am Anfang des Projektes
    § Oft Wegwerfprodukt

    View Slide

  295. 26.09.23 //// Seite 540
    WPS – Workplace Solutions
    GLOSSAR – BEISPIELDOMÄNE SCHACH
    SPIELER
    EINE VON ZWEI PERSONEN,
    DIE FIGUREN AUF DEM BRETT
    BEWEGT.
    KÖNIG
    EINE EINMALIGE FIGUR,
    DIE NUR EIN FELD PRO
    ZUG BEWEGT WERDEN
    KANN. WENN DER KÖNIG
    SCHACHMATT GESETZT
    WURDE, IST DAS SPIEL VORBEI.
    Beschreibung
    Begriff
    Oberbegriff
    Unterscheidungs-
    merkmale

    View Slide

  296. 26.09.23 //// Seite 541
    WPS – Workplace Solutions
    § Ein Team spricht eine eigene Sprache
    § è Familiensprache, Dialekt
    § Gleiche Begriffe können (leicht) unterschiedliche
    Bedeutung haben
    § Sprache und Modell sind eng gekoppelt
    JEDER BOUNDED CONTEXT HAT SEINE EIGENE UBIQUITOUS
    LANGUAGE
    TIEFEN-
    MESSUNG
    PEIL-
    SCHIFF
    TIEFEN-
    ZAHL
    MARKIER-
    UNG
    PEIL-
    PLAN
    TIEFEN-
    ZAHL
    MANÖVERPLANUNG

    View Slide

  297. 26.09.23 //// Seite 542
    WPS – Workplace Solutions
    JEDER BOUNDED CONTEXT HAT SEIN EIGENES GLOSSAR
    TIEFENZAHL
    TIEFE AN EINEM
    BESTIMMTEN ORT
    UNTER BERÜCKSICHTIGUNG
    VON EBBE UND FLUT
    TIEFENZAHL
    PER ECHOLOT GEMESSENE
    TIEFE BEI NORMALNULL
    MANÖVER-
    PLANUNG
    TIEFENMESSUNG

    View Slide

  298. 26.09.23 //// Seite 543
    WPS – Workplace Solutions
    EXERCISE:
    FACHLICHE
    KERNBEGRIFFE IM
    IST IDENTIFIZIEREN

    View Slide

  299. 26.09.23 //// Seite 544
    WPS – Workplace Solutions
    KERNBEGRIFFE FÜR DAS KINO
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Gesamtablauf-
    plan
    Tagesablaufplan
    Film
    Platz/Sitzplatz
    Saal/Kinosaal
    Werbung
    Veranstaltung
    Eisverkauf
    Vorführung
    ?
    ?

    View Slide

  300. 26.09.23 //// Seite 545
    WPS – Workplace Solutions
    BOUNDED CONTEXTS FÜR DAS KINO
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Gesamtablauf-
    plan
    Tagesablaufplan
    Film
    Platz/Sitzplatz
    Saal/Kinosaal
    Werbung
    Veranstaltung
    Eisverkauf
    Vorführung
    Kartenverkauf Ablaufplanung

    View Slide

  301. 26.09.23 //// Seite 546
    WPS – Workplace Solutions
    REDUKTION AUF BOUNDED CONTEXT »KARTENVERKAUF«
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Gesamtablauf-
    plan
    Tagesablaufplan
    Film
    Platz/Sitzplatz
    Saal/Kinosaal
    Werbung
    Veranstaltung
    Eisverkauf
    Vorführung
    Kartenverkauf Ablaufplanung

    View Slide

  302. 26.09.23 //// Seite 547
    WPS – Workplace Solutions
    REDUKTION AUF BOUNDED CONTEXT »KARTENVERKAUF«
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Film
    Platz/Sitzplatz
    Saal/Kinosaal

    View Slide

  303. 26.09.23 //// Seite 548
    WPS – Workplace Solutions
    REDUKTION AUF BOUNDED CONTEXT »KARTENVERKAUF«
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Film
    Platz/Sitzplatz
    Saal/Kinosaal

    View Slide

  304. 26.09.23 //// Seite 549
    WPS – Workplace Solutions
    REDUKTION AUF BOUNDED CONTEXT »KARTENVERKAUF«
    Saalplan
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    Saalplanstapel
    Liste der
    Reservierungs-
    nummern
    Film
    Platz/Sitzplatz
    Saal/Kinosaal

    View Slide

  305. WPS – Workplace Solutions #dddkonkret www.wps.de
    FACHLICHE HANDLUNGEN
    Domain-Driven Design hands-on

    View Slide

  306. 26.09.23 //// Seite 551
    WPS – Workplace Solutions
    ÜBERSICHT
    DOMÄNE
    DOMÄNE

    View Slide

  307. 26.09.23 //// Seite 552
    WPS – Workplace Solutions
    § Durch die Fachexperten (domain experts)
    § Sie wissen
    § Worum es bei ihrer Arbeit geht
    § Wo die Software sie unterstützen kann
    èWer die Fachlichkeit nicht
    versteht, kann ihr nicht helfen
    èSoftwareentwickler und
    Fachexperten bilden zusammen
    das Team
    WIE LERNEN WIR DIE FACHLICHKEIT?
    Foto: Brandon Raile/Wikipedia

    View Slide

  308. 26.09.23 //// Seite 553
    WPS – Workplace Solutions
    WIE KOMMEN WIR AN DAS WISSEN DER FACHEXPERTEN?
    Techniken:
    § Interviews
    § Szenarios
    § Event Storming
    § Domain Storytelling
    Das wollen wir
    extrahieren!
    Foto: Brandon Raile/Wikipedia
    Fach-
    wissen

    View Slide

  309. 26.09.23 //// Seite 554
    WPS – Workplace Solutions
    WORKSHOPS MIT ANWENDERN UND PRODUCT OWNERS
    “Georgia?” by The Library of Congress, flickr.com
    • Teilnehmer aus verschiedenen Bereichen (Business, IT, Management, …)
    • ein Moderator für den Workshop
    à direktes Feedback von allen Beteiligten

    View Slide

  310. 26.09.23 //// Seite 555
    WPS – Workplace Solutions
    EVENT STORMING
    § Eine Methode um Geschäftsprozesse zu modellieren
    § Modelliert werden:
    § Domain Events
    § Commands
    § Akteure
    § Aggregates

    View Slide

  311. 26.09.23 //// Seite 556
    WPS – Workplace Solutions
    Akteur Arbeitsgegenstand
    /Arbeitsergebnis
    Handlung
    DOMAIN STORYTELLING

    View Slide

  312. CUSTOMER
    TELLS WISH FOR
    1
    SALES-PERSON
    SIGNS
    TO
    GIVES
    FOR
    CONTRACT
    3
    RISIK MANAGER
    CONTRACT
    PASSES ON
    TO
    4
    CONTRACT
    VOTES
    CHECKS
    CALCULATES
    5
    6
    7
    CALCU-
    LATES
    TO
    8
    2
    CAR
    CREDIT
    RATING
    INSTALLMENT
    CAR
    RESALE
    VALUE
    CONTRACT

    View Slide

  313. CUSTOMER
    SIGNS
    CONTRACT
    3
    RISIK MANAGER
    CONTRACT
    VOTES
    7
    «Entity»
    LeasingContract
    sign()
    vote()

    View Slide

  314. EXERCISE:
    FACHLICHE
    HANDLUNGEN IM IST
    IDENTIFIZIEREN

    View Slide

  315. 26.09.23 //// Seite 560
    WPS – Workplace Solutions
    FACHLICHE HANDLUNGEN IM KARTENVERKAUF
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Vorstellung
    Reservierungs-
    nummer
    Kinokarte
    • Mit Platz beschriften
    Saalplanstapel
    • Saalplan zu Vorst. Holen
    • Saalplan zurücklegen
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen
    Film
    Platz/Sitzplatz
    Saal/Kinosaal

    View Slide

  316. 26.09.23 //// Seite 561
    WPS – Workplace Solutions
    EINIGE DATEN ERGÄNZEN
    Vorstellung
    • Datum
    • Zeitraum
    • Film
    Kinokarte
    • Mit Platz beschriften
    • Vorstellung
    Film
    • Titel
    • Regisseur, Schauspieler
    • Freigabe
    • Spieldauer
    Platz/Sitzplatz
    • Reihe
    • Nummer
    Saal/Kinosaal
    • Kapazität
    • Anzahl Reihen
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Reservierungs-
    nummer
    Saalplanstapel
    • Saalplan zu Vorst. Holen
    • Saalplan zurücklegen
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen

    View Slide

  317. WPS – Workplace Solutions #dddkonkret www.wps.de
    TACTICAL DESIGN
    Domain-Driven Design concrete

    View Slide

  318. 26.09.23 //// Seite 563
    WPS – Workplace Solutions
    OVERVIEW
    DOMÄNE
    DOMÄNE

    View Slide

  319. 26.09.23 //// Seite 564
    WPS – Workplace Solutions
    ARCHITECTURE WITH BOUNDED CONTEXTS
    TIDAL
    FORECAST
    MANEUVER
    PLANNING
    DEPTHS
    MEASUREMENT

    View Slide

  320. 26.09.23 //// Seite 565
    WPS – Workplace Solutions
    ARCHITECTURE WITHIN A BOUNDED CONTEXT
    MANEUVER
    PLANNING
    User Interface
    Application
    Domain
    Infrastructure

    View Slide

  321. 26.09.23 //// Seite 566
    WPS – Workplace Solutions
    DOMAIN-DRIVEN DESIGN—LAYER ARCHITECTURE
    § User interface layer
    § Receives input and user commands and presents information.
    § Application layer (Fowler: Service Layer)
    § Describes and coordinates application processes.
    § Domain layer
    § Represents the business domain logic.
    § Infrastructure layer
    § Provides technical services such as persistence or communication with other systems.
    User Interface
    Application
    Domain
    Infrastructure

    View Slide

  322. 26.09.23 //// Seite 567
    WPS – Workplace Solutions
    LAYER ARCHITECTURE – CRITIQUE
    MANEUVER
    PLANNING
    § Domain layer is linked to the
    database layer
    § Technology “bubbles up” into
    domain layer
    § No clean separation of
    domain & technology
    Alistair
    Cockburn
    Foto: Fotograf Dennis Hamilton/Alistair Cockburn/flickr/CC BY 2.0
    User Interface
    Application
    Domain
    Infrastructure

    View Slide

  323. 26.09.23 //// Seite 568
    WPS – Workplace Solutions
    below
    above
    FROM ABOVE/BELOW TO INSIDE/OUTSIDE
    out-
    side
    inside

    View Slide

  324. 26.09.23 //// Seite 569
    WPS – Workplace Solutions
    port
    port
    HEXAGONAL ARCHITECTURE
    Foto: Dennis Hamilton/flickr/CC BY 2.0
    http://alistair.cockburn.us/Hexagonal%2Barchitecture
    adapter
    adapter
    Alistair
    Cockburn

    View Slide

  325. 26.09.23 //// Seite 570
    WPS – Workplace Solutions
    KINDS OF PORTS
    - For UI etc.
    - Methods to be called
    - “from above”
    - For DB and infrastructure
    - Interfaces to be
    implemented
    - “from below”

    View Slide

  326. 26.09.23 //// Seite 571
    WPS – Workplace Solutions
    ONION ARCHITECTURE
    U
    I
    “application core”
    domain
    services
    domain
    model
    infra
    app
    ture
    struc
    serv
    lication
    ices
    Jeffrey
    Palermo
    !

    View Slide

  327. 26.09.23 //// Seite 572
    WPS – Workplace Solutions
    CLEAN ARCHITECTURE Robert C.
    Martin
    ”Uncle Bob“

    View Slide

  328. 26.09.23 //// Seite 573
    WPS – Workplace Solutions
    DIE OBJEKTORIENTIERTE GRUNDIDEE:
    FACHLICHE GEGENSTÄNDE ALS AUSGANGSPUNKT

    View Slide

  329. 26.09.23 //// Seite 574
    WPS – Workplace Solutions
    FROM DOMAIN STORY TO DOMAIN MODEL
    Silhouette
    MOVES
    SILHOUETTE
    NAUTICAL
    OFFICER
    LENGTH
    Length
    moveBy(:Length)
    BY
    8

    View Slide

  330. EXERCISE:
    BUILD DOMAIN
    MODEL FROM DOMAIN
    STUDIES IN CASE
    STUDY

    View Slide

  331. 26.09.23 //// Seite 576
    WPS – Workplace Solutions
    En##es
    Value Objects
    Aggregates
    Services
    Factories
    Repositories

    View Slide

  332. 26.09.23 //// Seite 577
    WPS – Workplace Solutions
    DOMAIN TERMS – WHAT IS STRIKING?
    insurance policy
    vacation request
    building activity
    purchase contract
    order
    zip code
    GPS coordinate
    IBAN
    container number
    IATA code

    View Slide

  333. 26.09.23 //// Seite 579
    WPS – Workplace Solutions
    ENTITIES
    § Objects of a domain that the user works with.
    § The results of the work are reflected by their state!
    § Can be composed of other entities.
    § Have an (immutable) identity.
    § Maintain their own consistency and integrity!
    § Have a clearly defined life cycle.
    § Have a (mostly mutable) state.
    § Describe their state by value objects.
    § Synonyms: Business Object / Domain Object / Work
    Material
    § NOT TO BE confused with the term “ENTITY” from
    Entity-Relationship-Model!
    Foto: Bundesrepublik Deutschland/Wikipedia/PD Germany
    Foto: Kaz/pixabay/CC0
    Foto: Thomas G./Wikipedia/CC BY-SA 3.0
    Foto: OpenClipart-Vectors/pixabay/CC0

    View Slide

  334. View Slide

  335. 26.09.23 //// Seite 581
    WPS – Workplace Solutions
    ENTITIES
    «Entity»
    DepthMap
    depthMapId
    mark(Marking)

    View Slide

  336. 26.09.23 //// Seite 583
    WPS – Workplace Solutions
    VALUE OBJECTS
    § Value objects are symbols for values of a
    certain type in the domain.
    § Symbolize the same value if they are equal.
    § They are not edited by the user and cannot
    be modified.
    § Can be calculated (from other value objects),
    if necessary.
    § Can consist of other value objects, but never
    of entities!
    ValueObject
    2.5
    ValueObject
    ValueObject
    ValueObject
    ValueObject
    two and
    a half

    View Slide

  337. Depth
    Diameter
    Position

    View Slide

  338. 26.09.23 //// Seite 586
    WPS – Workplace Solutions
    VALUE OBJECTS
    «Value Object»
    Depth
    centimeters : int
    equals()

    View Slide

  339. 26.09.23 //// Seite 589
    WPS – Workplace Solutions
    EXERCISE:
    ENTITES,
    VALUE OBJECTS

    View Slide

  340. 26.09.23 //// Seite 590
    WPS – Workplace Solutions
    ENTITIES UND VALUE OBJECTS IM KARTENVERKAUF
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Vorstellung
    • Datum
    • Zeitraum
    • Film
    Reservierungs
    -nummer
    Kinokarte
    • Mit Platz beschriften
    • Vorstellung
    Saalplanstapel
    • Saalplan zu Vorst. Holen
    • Saalplan zurücklegen
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen
    Film
    • Titel
    • Regisseur, Schauspieler
    • Freigabe
    • Spieldauer
    Platz/Sitzplatz
    • Reihe
    • Nummer
    Saal/Kinosaal
    • Kapazität
    • Anzahl Reihen
    VO
    Entity
    Entity
    Entity
    Entity Entity
    Entity

    View Slide

  341. 26.09.23 //// Seite 591
    WPS – Workplace Solutions
    ENTITIES UND VALUE OBJECTS IM KARTENVERKAUF
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Vorstellung
    • Datum
    • Zeitraum
    • Film
    Reservierungs
    -nummer
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen
    Film
    • Titel
    • Regisseur, Schauspieler
    • Freigabe
    • Spieldauer
    Platz/Sitzplatz
    • Reihe
    • Nummer
    Saal/Kinosaal
    • Kapazität
    • Anzahl Reihen
    VO
    Entity
    Entity
    Entity
    Entity Entity
    Entity

    View Slide

  342. WPS – Workplace Solutions #dddkonkret www.wps.de
    REPOSITORIES AND AGGREGATES

    View Slide

  343. 26.09.23 //// Seite 593
    WPS – Workplace Solutions
    REPOSITORIES
    § Are the access points to the aggregates!
    § Encapsulate the technical details of the technical
    infrastructure layer ...
    § ... and map external data to entities and value
    objects.
    § Store aggregates (e. g. in databases).
    Repository
    Aggregate
    Aggregate
    Aggregate
    Aggregate
    Aggregate

    View Slide

  344. 26.09.23 //// Seite 594
    WPS – Workplace Solutions
    REPOSITORY @ HAMBURG PORT: THE DEPTH MAP CABINET

    View Slide

  345. 26.09.23 //// Seite 595
    WPS – Workplace Solutions
    REPOSITORY: TWO PART IMPLEMENTATION
    «Repository Interface»
    DepthMapCabinet
    getDepthMap(Area, Scale) : DepthMap
    putBack(DepthMap)
    «Repository Implementation»
    MySQLDepthMapCabinet

    View Slide

  346. 26.09.23 //// Seite 596
    WPS – Workplace Solutions
    AGGREGATES
    § Aggregates are entities that are relevant in
    themselves (and not just as part of another
    entity).
    § Protect the consistency and integrity of their
    inner entities.
    § They don't necessarily have to hide them to do
    this!
    § Always have a designated entity as an entry
    point (root).
    § Usually stored.
    § As a whole!
    § Not necessarily in a relational way!
    Root Entity
    VO
    VO
    VO
    Aggregate

    View Slide

  347. 26.09.23 //// Seite 597
    WPS – Workplace Solutions
    AGGREGATES
    § Actually, the value objects are outside as they
    are immutable and may be reused anywhere.
    Root Entity
    VO
    VO
    VO
    Some other
    object
    Aggregate

    View Slide

  348. 26.09.23 //// Seite 598
    WPS – Workplace Solutions
    AGGREGATES
    § An aggregate can consist of many entities.
    Root Entity
    Entity
    Entity
    Entity
    Entity
    VO
    VO
    VO
    VO
    VO
    VO
    VO
    VO
    VO
    VO
    VO
    Aggregate

    View Slide

  349. View Slide

  350. 26.09.23 //// Seite 600
    WPS – Workplace Solutions
    Aggregate
    «Aggregate Root»
    DepthMap
    «Entity»
    Marking
    «Value Object»
    Depth

    View Slide

  351. 26.09.23 //// Seite 601
    WPS – Workplace Solutions
    ENTITIES UND VALUE OBJECTS IM KARTENVERKAUF
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Vorstellung
    • Datum
    • Zeitraum
    • Film
    Reservierungs
    -nummer
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen
    Film
    • Titel
    • Regisseur, Schauspieler
    • Freigabe
    • Spieldauer
    Platz/Sitzplatz
    • Reihe
    • Nummer
    Saal/Kinosaal
    • Kapazität
    • Anzahl Reihen
    VO
    Entity
    Entity
    Entity
    Entity Entity
    Entity

    View Slide

  352. EXERCISE:
    DRAW AGGREGATE
    BOUNDARIES.
    WHICH REPOSITORIES DO
    WE NEED?

    View Slide

  353. 26.09.23 //// Seite 603
    WPS – Workplace Solutions
    ENTITIES UND VALUE OBJECTS IM KARTENVERKAUF
    Saalplan
    • Anzahl Plätze suchen
    • Verkaufte Plätze mark.
    • Reserv. Plätze mit RN
    markieren
    Vorstellung
    • Datum
    • Zeitraum
    • Film
    Reservierungs
    -nummer
    Liste der
    Reservierungsnummern
    • Reservierungsnummer abholen
    • Name+Vorst. Vermerken
    • Vorst. Mit RN heraussuchen
    Film
    • Titel
    • Regisseur, Schauspieler
    • Freigabe
    • Spieldauer
    Platz/Sitzplatz
    • Reihe
    • Nummer
    Saal/Kinosaal
    • Kapazität
    • Anzahl Reihen
    VO
    Entity
    Entity
    Wurzel-Entity
    Entity Entity
    Entity
    Aggregate
    Aggregate
    Aggregate Aggregate
    Aggregate

    View Slide

  354. WPS – Workplace Solutions #dddkonkret www.wps.de
    IMPLEMENTING THE BUILDING BLOCKS IN JAVA
    Domain-Driven Design concrete

    View Slide

  355. 26.09.23 //// Seite 622
    WPS – Workplace Solutions

    View Slide

  356. 26.09.23 //// Seite 623
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    public class Account {
    }

    View Slide

  357. 26.09.23 //// Seite 624
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    import org.jmolecules.ddd.annotation.Entity;
    @Entity
    public class Account {
    }

    View Slide

  358. 26.09.23 //// Seite 625
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    import org.jmolecules.ddd.annotation.Entity;
    @Entity
    public class Account {
    }

    View Slide

  359. 26.09.23 //// Seite 626
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    @Entity
    public class Account {
    private int _balance;
    public int getBalance() {
    return _balance;
    }
    public void setBalance(int balance) {
    _balance = balance;
    }
    }

    Bad: The
    account balance
    can be set to
    any value

    View Slide

  360. 26.09.23 //// Seite 627
    WPS – Workplace Solutions
    A BANK ACCOUNT—SECOND VERSION
    @Entity
    public class Account {
    private int _balance;
    public int balance() {
    return _balance;
    }
    public void deposit(int amount) {
    _balance += amount;
    }
    public void withdraw(int amount) {
    _balance -= amount;
    }
    }
    Better:
    Operations with
    domain-specific
    behavior and
    names

    View Slide

  361. 26.09.23 //// Seite 628
    WPS – Workplace Solutions
    A BANK ACCOUNT—THIRD VERSION
    @Entity
    public class Account {
    private int _balance;
    public int balance() {
    return _balance;
    }
    public void deposit(int amount) {
    _balance += amount;
    }
    public void withdraw(int amount) {
    if (amount > balance()) {
    throw new IllegalArgumentException("Amount too big");
    }
    _balance -= amount;
    }
    Even better:
    Preconditions
    can be checked

    View Slide

  362. 26.09.23 //// Seite 630
    WPS – Workplace Solutions
    DESIGN BY CONTRACT
    Foto: Urheber Fuchsias/Bertrand Meyer/Wikipedia/CC BY-SA 4.0

    View Slide

  363. 26.09.23 //// Seite 631
    WPS – Workplace Solutions
    A BANK ACCOUNT—DESIGN BY CONTRACT USING “ASSERT”
    @Entity
    public class Account {
    // ...
    public void withdraw(int amount) {
    assert amount <= balance();
    _balance -= amount;
    }
    }
    Assertion using
    keyword
    assert

    View Slide

  364. 26.09.23 //// Seite 632
    WPS – Workplace Solutions
    A BANK ACCOUNT—DESIGN BY CONTRACT USING “VALID4J”
    import static org.valid4j.Assertive.*;
    @Entity
    public class Account {
    // ...
    public void withdraw(int amount) {
    require(amount <= balance());
    _balance -= amount;
    }
    } Hamcrest
    matchers can be
    used

    View Slide

  365. 26.09.23 //// Seite 636
    WPS – Workplace Solutions
    GREAT, BUT…
    @Entity
    public class Account {
    // ...
    public void withdraw(int amount) {
    assert amount <= balance();
    _balance -= amount;
    }
    }
    Can I withdraw a
    negative
    amount?
    In EUR or
    GBP or…?

    View Slide

  366. 26.09.23 //// Seite 638
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    public class Amount {
    }

    View Slide

  367. 26.09.23 //// Seite 639
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    import org.jmolecules.ddd.annotation.ValueObject;
    @ValueObject
    public class Amount {
    }

    View Slide

  368. 26.09.23 //// Seite 640
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private int _amount;
    private Currency _currency;
    }

    View Slide

  369. 26.09.23 //// Seite 641
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    }

    View Slide

  370. 26.09.23 //// Seite 642
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    public Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    }

    View Slide

  371. 26.09.23 //// Seite 643
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    private Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    public static Amount of(int amount, Currency currency) {
    return new Amount(amount, currency);
    }
    }

    View Slide

  372. 26.09.23 //// Seite 644
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    public Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    @Override
    public boolean equals(Object other) {
    return _amount == ((Amount) other)._amount
    && _currency.equals(((Amount) other)._currency);
    }
    }

    View Slide

  373. 26.09.23 //// Seite 645
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    public Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    @Override
    public boolean equals(Object other) {
    return _amount == ((Amount) other)._amount
    && _currency.equals(((Amount) other)._currency);
    }
    // hashCode()
    }

    View Slide

  374. 26.09.23 //// Seite 646
    WPS – Workplace Solutions
    AN AMOUNT TYPE—JAVA 14 AND HIGHER
    @ValueObject
    public record Amount(int amount, Currency currency);

    View Slide

  375. 26.09.23 //// Seite 647
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    public Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    public Amount add(Amount otherAmount) {
    return Amount.of(_amount + otherAmount._amount, _currency);
    }
    }
    The new value
    object type
    behaves
    correctly

    View Slide

  376. 26.09.23 //// Seite 648
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    @ValueObject
    public class Amount {
    private final int _amount;
    private final Currency _currency;
    public Amount(int amount, Currency currency) {
    _amount = amount;
    _currency = currency;
    }
    public Amount add(Amount otherAmount) {
    assert hasSameCurrency(otherAmount);
    return Amount.of(_amount + otherAmount._amount, _currency);
    }
    public boolean hasSameCurrency(Amount otherAmount) {
    return otherAmount._currency == _currency;
    ... and contracts
    ensure the
    correct currency

    View Slide

  377. 26.09.23 //// Seite 652
    WPS – Workplace Solutions
    A BANK ACCOUNT—FOURTH VERSION
    @Entity
    public class Account {
    private Amount _balance;
    public Amount balance() {
    return _balance;
    }
    public void deposit(Amount amount) {
    _balance = _balance.add(amount);
    }
    public void withdraw(Amount amount) {
    assert amount.isLessOrEqual(balance());
    _balance = _balance.subtract(amount);
    }
    }
    Now we can use
    the amount type

    View Slide

  378. 26.09.23 //// Seite 653
    WPS – Workplace Solutions
    A BANK ACCOUNT—FOURTH VERSION
    @Entity
    public class Account {
    private Amount _balance;
    public Amount balance() {
    return _balance;
    }
    public void deposit(Amount amount) {
    _balance = _balance.add(amount);
    }
    public void withdraw(Amount amount) {
    assert amount.hasSameCurrency(balance());
    assert amount.isLessOrEqual(balance());
    _balance = _balance.subtract(amount);
    }
    }
    ... and define
    new contracts

    View Slide

  379. 26.09.23 //// Seite 654
    WPS – Workplace Solutions
    A BANK ACCOUNT—EVENT SOURCED
    @Entity
    public class Account {
    private List _transactions;
    public Amount balance() {
    // sum up _transactions
    }
    public void deposit(Amount amount) {
    _transactions.append(new Deposition(amount));
    }
    public void withdraw(Amount amount) {
    assert amount.hasSameCurrency(balance());
    assert amount.isLessOrEqual(balance());
    _transactions.append(new Withdrawal(amount));
    }
    }
    History
    instead of
    current
    status

    View Slide

  380. 26.09.23 //// Seite 657
    WPS – Workplace Solutions
    IDENTITY OF ENTITIES
    § In Java, every object has an identity
    § Namely its reference.
    § It's a technical identity.
    § An entity has a identity from a domain perspective
    § Different technical objects can represent the same entity
    § In different processes: e.g. on the client / on the server
    § When they are stored and later read from the database.
    § The identity can be implemented in two ways:
    § explicitly (e.g. IBAN) by using a unique key the user is aware of
    § implicitly (e.g. UUID / GUID) by using a technical key hidden
    from the user
    ?

    View Slide

  381. 26.09.23 //// Seite 658
    WPS – Workplace Solutions
    A BANK ACCOUNT—WITH EXPLICIT IDENTITY
    @Entity
    public class Account {
    @Identity
    private final IBAN _iban;
    public Account(IBAN iban) {
    _iban = iban;
    }
    @Override
    public boolean equals(Object other) {
    return _iban.equals(((Account)other)._iban);
    }
    // ...
    }
    The identity is
    immutable
    The key is of
    course a value
    object
    The implementation
    of equals() is based
    on the identity

    View Slide

  382. 26.09.23 //// Seite 659
    WPS – Workplace Solutions
    HOW CAN A NEW IDENTITY BE CREATED?
    § Database
    § Pro: Central
    § Can sometimes be created “lazily”
    § A system-wide value type factory
    § Domain-specific keys known to the user
    § Often implemented by the repository of a corresponding aggregate type the key is used for
    § A UUID / GUID generator
    § Pro: can be generated independently on any server or client
    § Con: String in a special format

    View Slide

  383. 26.09.23 //// Seite 660
    WPS – Workplace Solutions
    BANK TRANSFER—BY DOMAIN SERVICE
    @Service
    public class CreditTransferService {
    public void transfer(Account source, Account destination, Amount amount) {
    source.withdraw(amount);
    destination.deposit(amount);
    }
    }
    No fields à
    stateless

    View Slide

  384. EXERCISE:
    MOBPROGRAMMING
    @ THE CINEMA

    View Slide

  385. 26.09.23 //// Seite 663
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN GLOSSARY—REVISITED
    DEPTH
    DEPTH AT A SPECIFIC
    GEO POSITION CONSIDERING
    THE CURRENT TIDAL LEVEL
    DEPTH
    DEPTH BELOW SEA LEVEL
    MEASURED BY ECHO
    SOUNDER
    MANEUVER
    PLANNING
    DEPTH
    MEASUREMENT

    View Slide

  386. 26.09.23 //// Seite 664
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN IMPLEMENTATION
    public struct Depth
    {
    public Depth(
    int depthBelowSeaLevel,
    int tideLevel)
    {
    // ...
    }
    }
    MANEUVER
    PLANNING
    DEPTH
    MEASUREMENT
    public class Depth {
    public Depth(
    int centimeter,
    LocalDate soundingDate) {
    // ...
    }
    }

    View Slide

  387. WPS – Workplace Solutions #dddkonkret www.wps.de
    IMPLEMENTING THE BUILDING BLOCKS IN C#
    Domain-Driven Design concrete

    View Slide

  388. 26.09.23 //// Seite 666
    WPS – Workplace Solutions

    View Slide

  389. 26.09.23 //// Seite 667
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    public class Account
    {
    }

    View Slide

  390. 26.09.23 //// Seite 668
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    using NMolecules.DDD;
    [Entity]
    public class Account
    {
    }

    View Slide

  391. 26.09.23 //// Seite 669
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    [Entity]
    public class Account
    {
    public int Balance { get; set; }
    }

    Bad: The
    account balance
    can be set to
    any value

    View Slide

  392. 26.09.23 //// Seite 670
    WPS – Workplace Solutions
    A BANK ACCOUNT—SECOND VERSION
    [Entity]
    public class Account
    {
    public int Balance { get; private set; }
    public void Deposit(int amount)
    {
    Balance += amount;
    }
    public void Withdraw(int amount)
    {
    Balance -= amount;
    }
    }
    Better:
    Operations with
    domain-specific
    behavior and
    names

    View Slide

  393. 26.09.23 //// Seite 671
    WPS – Workplace Solutions
    A BANK ACCOUNT—THIRD VERSION
    [Entity]
    public class Account
    {
    public int Balance { get; private set; }
    public void Deposit(int amount)
    {
    Balance += amount;
    }
    public void Withdraw(int amount)
    {
    if (amount > balance())
    {
    throw new ArgumentException("Amount too big");
    }
    Balance -= amount;
    }
    Even better:
    Preconditions
    can be checked

    View Slide

  394. 26.09.23 //// Seite 673
    WPS – Workplace Solutions
    DESIGN BY CONTRACT
    Foto: Urheber Fuchsias/Bertrand Meyer/Wikipedia/CC BY-SA 4.0

    View Slide

  395. 26.09.23 //// Seite 674
    WPS – Workplace Solutions
    A BANK ACCOUNT—DESIGN BY CONTRACT USING “ASSERT”
    using static System.Diagnostics.Debug;
    [Entity]
    public class Account
    {
    // ...
    public void Withdraw(int amount)
    {
    Assert(amount <= balance());
    Balance -= amount;
    }
    }
    Assertion using
    Debug.Assert

    View Slide

  396. 26.09.23 //// Seite 677
    WPS – Workplace Solutions
    GREAT, BUT…
    using static System.Diagnostics.Debug;
    [Entity]
    public class Account
    {
    // ...
    public void Withdraw(int amount)
    {
    Assert(amount <= balance());
    Balance -= amount;
    }
    }
    Can I withdraw a
    negative
    amount?
    In EUR or
    GBP or…?

    View Slide

  397. 26.09.23 //// Seite 679
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    public class Amount
    {
    }

    View Slide

  398. 26.09.23 //// Seite 680
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    using NMolecules.DDD;
    [ValueObject]
    public class Amount
    {
    }

    View Slide

  399. 26.09.23 //// Seite 681
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private int _amount;
    private Currency _currency;
    }

    View Slide

  400. 26.09.23 //// Seite 682
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    }

    View Slide

  401. 26.09.23 //// Seite 683
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    public Amount(int amount, Currency currency)
    {
    _amount = amount;
    _currency = currency;
    }
    }

    View Slide

  402. 26.09.23 //// Seite 684
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    private Amount(int amount, Currency currency)
    {
    _amount = amount;
    _currency = currency;
    }
    public static Amount Of(int amount, Currency currency)
    {
    return new Amount(amount, currency);
    }
    }

    View Slide

  403. 26.09.23 //// Seite 685
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    // ...
    public override bool Equals(object other)
    {
    return other is Amount
    && _amount == ((Amount) other)._amount
    && _currency.Equals(((Amount) other)._currency);
    }
    }

    View Slide

  404. 26.09.23 //// Seite 686
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    // ...
    public override bool Equals(object other)
    => other is Amount
    && _amount == ((Amount) other)._amount
    && _currency.Equals(((Amount) other)._currency);
    }

    View Slide

  405. 26.09.23 //// Seite 687
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    // ...
    public override bool Equals(object other)
    => other is Amount
    && _amount == ((Amount) other)._amount
    && _currency.Equals(((Amount) other)._currency);
    // GetHashCode()
    }

    View Slide

  406. 26.09.23 //// Seite 688
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    // ...
    public static bool operator ==(Amount a, Amount b)
    => a.Equals(b);
    }

    View Slide

  407. 26.09.23 //// Seite 689
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public class Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    // ...
    public static bool operator ==(Amount a, SignDate b)
    => a.Equals(b);
    public static bool operator !=(Amount a, SignDate b)
    => !a.Equals(b);
    }

    View Slide

  408. View Slide

  409. 26.09.23 //// Seite 691
    WPS – Workplace Solutions
    3
    CLASSES DEFINE REFERENCE TYPES

    View Slide

  410. 26.09.23 //// Seite 692
    WPS – Workplace Solutions
    3
    STRUCTS DEFINE VALUE TYPES

    View Slide

  411. 26.09.23 //// Seite 693
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public struct Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    public Amount(int amount, Currency currency)
    {
    _amount = amount;
    _currency = currency;
    }
    }

    View Slide

  412. 26.09.23 //// Seite 694
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public struct Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    public Amount(int amount, Currency currency)
    {
    _amount = amount;
    _currency = currency;
    }
    // Equals() does not have to be overridden
    }

    View Slide

  413. 26.09.23 //// Seite 695
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public struct Amount
    {
    private readonly int _amount;
    private readonly Currency _currency;
    public Amount(int amount, Currency currency)
    {
    _amount = amount;
    _currency = currency;
    }
    // but the operators have to be overridden
    }

    View Slide

  414. @hschwentner
    equals() !
    == "

    View Slide

  415. 26.09.23 //// Seite 697
    WPS – Workplace Solutions
    AN AMOUNT TYPE—C# 9 AND HIGHER
    [ValueObject]
    public record Amount(int amount, Currency currency);

    View Slide

  416. 26.09.23 //// Seite 698
    WPS – Workplace Solutions
    AN AMOUNT TYPE—C# 9 AND HIGHER
    [ValueObject]
    public record Amount(int amount, Currency currency);
    // automatically readonly
    // Equals(), GetHashCode(), ToString(), operators
    // do not have to be overloaded and work as expected

    View Slide

  417. 26.09.23 //// Seite 699
    WPS – Workplace Solutions
    AN AMOUNT TYPE—C# 9 AND HIGHER
    [ValueObject]
    public record struct Amount(int amount, Currency currency);

    View Slide

  418. 26.09.23 //// Seite 700
    WPS – Workplace Solutions
    AN AMOUNT TYPE—C# 10 AND HIGHER
    [ValueObject]
    public readonly record struct Amount(int amount, Currency currency);

    View Slide

  419. 26.09.23 //// Seite 701
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public readonly record struct Amount(int _amount, Currency _currency)
    {
    public Amount Add(Amount otherAmount)
    {
    return Amount.of(_amount + otherAmount._amount, _currency);
    }
    }
    The new value
    object type
    behaves
    correctly

    View Slide

  420. 26.09.23 //// Seite 702
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public readonly record struct Amount(int amount, Currency currency)
    {
    public Amount Add(Amount otherAmount)
    {
    Assert(HasSameCurrency(otherAmount));
    return Amount.of(_amount + otherAmount.amount, currency);
    }
    public boolean HasSameCurrency(Amount otherAmount)
    => otherAmount.currency == currency;
    }
    ... and contracts
    ensure the
    correct currency

    View Slide

  421. 26.09.23 //// Seite 703
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    [ValueObject]
    public readonly record struct Amount(int amount, Currency currency)
    {
    public Amount operator +(Amount otherAmount)
    {
    Assert(HasSameCurrency(otherAmount));
    return Amount.Of(_amount + otherAmount._amount, _currency);
    }
    public boolean HasSameCurrency(Amount otherAmount) {
    return otherAmount._currency == _currency;
    }
    }

    View Slide

  422. 26.09.23 //// Seite 704
    WPS – Workplace Solutions
    A BANK ACCOUNT—FOURTH VERSION
    [Entity]
    public class Account
    {
    public Amount Balance { get; private set; }
    public void Deposit(Amount amount)
    {
    Balance += amount;
    }
    public void Withdraw(Amount amount)
    {
    Assert(amount <= Balance);
    Balance -= amount;
    }
    }
    Now we can use
    the amount type

    View Slide

  423. 26.09.23 //// Seite 705
    WPS – Workplace Solutions
    A BANK ACCOUNT—FOURTH VERSION
    [Entity]
    public class Account
    {
    public Amount Balance { get; private set; }
    public void Deposit(Amount amount)
    {
    Balance += amount;
    }
    public void Withdraw(Amount amount)
    {
    Assert(amount.HasSameCurrencyAs(Balance);
    Assert(amount <= Balance);
    Balance -= amount;
    }
    }
    ... and define
    new contracts

    View Slide

  424. 26.09.23 //// Seite 706
    WPS – Workplace Solutions
    A BANK ACCOUNT—EVENT SOURCED
    @Entity
    public class Account {
    private List _transactions;
    public Amount balance() {
    // sum up _transactions
    }
    public void deposit(Amount amount) {
    _transactions.append(new Deposition(amount));
    }
    public void withdraw(Amount amount) {
    assert amount.hasSameCurrency(balance());
    assert amount.isLessOrEqual(balance());
    _transactions.append(new Withdrawal(amount));
    }
    }
    History
    instead of
    current
    status

    View Slide

  425. 26.09.23 //// Seite 709
    WPS – Workplace Solutions
    IDENTITY OF ENTITIES
    § In Java, every object has an identity
    § Namely its reference.
    § It's a technical identity.
    § An entity has a identity from a domain perspective
    § Different technical objects can represent the same entity
    § In different processes: e.g. on the client / on the server
    § When they are stored and later read from the database.
    § The identity can be implemented in two ways:
    § explicitly (e.g. IBAN) by using a unique key the user is aware of
    § implicitly (e.g. UUID / GUID) by using a technical key hidden
    from the user
    ?

    View Slide

  426. 26.09.23 //// Seite 710
    WPS – Workplace Solutions
    A BANK ACCOUNT—WITH EXPLICIT IDENTITY
    @Entity
    public class Account {
    @Identity
    private final IBAN _iban;
    public Account(IBAN iban) {
    _iban = iban;
    }
    @Override
    public boolean equals(Object other) {
    return _iban.equals(((Account)other)._iban);
    }
    // ...
    }
    The identity is
    immutable
    The key is of
    course a value
    object
    The implementation
    of equals() is based
    on the identity

    View Slide

  427. 26.09.23 //// Seite 711
    WPS – Workplace Solutions
    HOW CAN A NEW IDENTITY BE CREATED?
    § Database
    § Pro: Central
    § Can sometimes be created “lazily”
    § A system-wide value type factory
    § Domain-specific keys known to the user
    § Often implemented by the repository of a corresponding aggregate type the key is used for
    § A UUID / GUID generator
    § Pro: can be generated independently on any server or client
    § Con: String in a special format

    View Slide

  428. 26.09.23 //// Seite 712
    WPS – Workplace Solutions
    BANK TRANSFER—BY DOMAIN SERVICE
    @Service
    public class CreditTransferService {
    public void transfer(Account source, Account destination, Amount amount) {
    source.withdraw(amount);
    destination.deposit(amount);
    }
    }
    No fields à
    stateless

    View Slide

  429. EXERCISE:
    MOBPROGRAMMING
    @ THE CINEMA

    View Slide

  430. 26.09.23 //// Seite 715
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN GLOSSARY—REVISITED
    DEPTH
    DEPTH AT A SPECIFIC
    GEO POSITION CONSIDERING
    THE CURRENT TIDAL LEVEL
    DEPTH
    DEPTH BELOW SEA LEVEL
    MEASURED BY ECHO
    SOUNDER
    MANEUVER
    PLANNING
    DEPTH
    MEASUREMENT

    View Slide

  431. 26.09.23 //// Seite 716
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN IMPLEMENTATION
    public struct Depth
    {
    public Depth(
    int depthBelowSeaLevel,
    int tideLevel)
    {
    // ...
    }
    }
    MANEUVER
    PLANNING
    DEPTH
    MEASUREMENT
    public class Depth {
    public Depth(
    int centimeter,
    LocalDate soundingDate) {
    // ...
    }
    }

    View Slide

  432. WPS – Workplace Solutions #dddkonkret www.wps.de
    IMPLEMENTING THE BUILDING BLOCKS IN PHP
    Domain-Driven Design concrete

    View Slide

  433. 26.09.23 //// Seite 718
    WPS – Workplace Solutions

    View Slide

  434. 26.09.23 //// Seite 719
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    class Account
    {
    }

    View Slide

  435. 26.09.23 //// Seite 720
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    use PHPMolecules\DDD\Attribute\Entity;
    #[Entity]
    class Account
    {
    }

    View Slide

  436. 26.09.23 //// Seite 721
    WPS – Workplace Solutions
    A BANK ACCOUNT—FIRST DRAFT
    #[Entity]
    class Account
    {
    private int $balance;
    public function getBalance(): int
    {
    return $this->balance;
    }
    public function setBalance(int $balance): void
    {
    $this-> balance = $balance;
    }
    }

    Bad: The
    account balance
    can be set to
    any value

    View Slide

  437. 26.09.23 //// Seite 722
    WPS – Workplace Solutions
    A BANK ACCOUNT—SECOND VERSION
    #[Entity]
    class Account
    {
    private int $balance;
    public funtion balance(): int
    {
    return $this->balance;
    }
    public function deposit(int amount): void
    {
    $this->balance += amount;
    }
    public function withdraw(int amount): void
    {
    $this->balance -= amount;
    }
    }
    Better:
    Operations with
    domain-specific
    behavior and
    names

    View Slide

  438. 26.09.23 //// Seite 723
    WPS – Workplace Solutions
    A BANK ACCOUNT—THIRD VERSION
    Even better:
    Preconditions
    can be checked
    #[Entity]
    class Account
    {
    private int $balance;
    public funtion balance(): int
    {
    return $this->balance;
    }
    public function deposit(int amount): void
    {
    $this->balance += amount;
    }
    public function withdraw(int amount): void
    {
    assert(amount <= $this->balance());
    $this->balance -= amount;
    }

    View Slide

  439. 26.09.23 //// Seite 725
    WPS – Workplace Solutions
    DESIGN BY CONTRACT
    Foto: Urheber Fuchsias/Bertrand Meyer/Wikipedia/CC BY-SA 4.0

    View Slide

  440. 26.09.23 //// Seite 726
    WPS – Workplace Solutions
    DESIGN BY CONTRACT IN PHP
    § https://github.com/php-deal
    § http://www.terminally-incoherent.com/blog/2008/06/05/design-by-contract-in-php-with-assertions/

    View Slide

  441. 26.09.23 //// Seite 727
    WPS – Workplace Solutions
    GREAT, BUT…
    #[Entity]
    class Account {
    // ...
    public function withdraw(int $amount): void
    {
    assert($amount <= $this->balance());
    $this->balance -= $amount;
    }
    }
    Can I withdraw a
    negative
    amount?
    In EUR or
    GBP or…?

    View Slide

  442. 26.09.23 //// Seite 729
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    class Amount {
    }

    View Slide

  443. 26.09.23 //// Seite 730
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    use PHPMolecules\DDD\Attribute\ValueObject;
    #[ValueObject]
    class Amount
    {
    }

    View Slide

  444. 26.09.23 //// Seite 731
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    class Amount
    {
    private int $amount;
    private Currency $currency;
    }

    View Slide

  445. 26.09.23 //// Seite 732
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    class Amount
    {
    private readonly int $amount;
    private readonly Currency $currency;
    }

    View Slide

  446. 26.09.23 //// Seite 734
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    class Amount
    {
    private readonly int $amount;
    private readonly Currency $currency;
    public function __construct(int $amount, Currency $currency)
    {
    $this->amount = $amount;
    $this->currency = $currency;
    }
    }

    View Slide

  447. 26.09.23 //// Seite 735
    WPS – Workplace Solutions
    AN AMOUNT TYPE—PHP 8.1 AND HIGHER
    #[ValueObject]
    class Amount
    {
    public function __construct(
    private readonly int $amount,
    private readonly Currency $currency
    ) {}
    }

    View Slide

  448. 26.09.23 //// Seite 736
    WPS – Workplace Solutions
    AN AMOUNT TYPE—PHP 8.2 AND HIGHER
    #[ValueObject]
    readonly class Amount
    {
    public function __construct(
    private int $amount;
    private Currency $currency;
    ) {}
    }

    View Slide

  449. 26.09.23 //// Seite 737
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    readonly class Amount
    {
    private function __construct(
    private int $amount,
    private Currency $currency
    ) {}
    public static function of(int $amount, Currency $currency): Amount
    {
    return new Amount($amount, $currency);
    }
    }

    View Slide

  450. 26.09.23 //// Seite 738
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    readonly class Amount
    {
    public function __construct(
    private int $amount;
    private Currency $currency;
    ) {}
    public function equals(Amount $other): bool
    {
    return $this->amount === $other->amount
    && $this->currency->equals($other->currency);
    }
    }

    View Slide

  451. 26.09.23 //// Seite 741
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    readonly class Amount
    {
    public function __construct(
    private int $amount,
    private Currency $currency
    ) {}
    public function add(Amount $otherAmount): Amount
    {
    return Amount.of($this->amount + $otherAmount->amount, $this->currency);
    }
    }
    The new value
    object type
    behaves
    correctly

    View Slide

  452. 26.09.23 //// Seite 742
    WPS – Workplace Solutions
    AN AMOUNT TYPE
    #[ValueObject]
    readonly class Amount
    {
    public function __construct(
    private int $amount,
    private Currency $currency
    ) {}
    public function add(Amount $otherAmount): Amount
    {
    assert($this->hasSameCurrencyAs($otherAmount));
    return Amount.of($this->amount + $otherAmount->amount, $this->currency);
    }
    public function hasSameCurrencyAs(Amount $otherAmount): bool {
    return $this->currency === $otherAmount->currency;
    }
    }
    ... and contracts
    ensure the
    correct currency

    View Slide

  453. 26.09.23 //// Seite 743
    WPS – Workplace Solutions
    A BANK ACCOUNT—FOURTH VERSION
    Now we can use
    the amount type
    #[Entity]
    class Account
    {
    private Amount $balance;
    public function balance(): Amount
    {
    return $this->balance;
    }
    public function deposit(Amount $amount): void
    {
    $this->balance = $this->balance->add($amount);
    }
    public function withdraw(Amount $amount): void
    {
    assert($amount.isLessOrEqual($this->balance());
    $this->balance = $this->balance->subtract($amount);
    }

    View Slide

  454. 26.09.23 //// Seite 744
    WPS – Workplace Solutions
    #[Entity]
    class Account
    {
    private Amount $balance;
    public function balance(): Amount
    {
    return $this->balance;
    }
    public function deposit(Amount $amount): void
    {
    $this->balance = $this->balance->add($amount);
    }
    public function withdraw(Amount $amount): void
    {
    assert $amount.hasSameCurrency($this->balance());
    assert($amount.isLessOrEqual($this->balance());
    $this->balance = $this->balance->subtract($amount);
    A BANK ACCOUNT—FOURTH VERSION
    ... and define
    new contracts

    View Slide

  455. 26.09.23 //// Seite 745
    WPS – Workplace Solutions
    A BANK ACCOUNT—EVENT SOURCED
    @Entity
    public class Account {
    private List _transactions;
    public Amount balance() {
    // _transactions aufsummieren
    }
    public void deposit(Amount amount) {
    _transactions.append(new Deposition(amount));
    }
    public void withdraw(Amount amount) {
    assert amount.hasSameCurrency(balance());
    assert amount.isLessOrEqual(balance());
    _transactions.append(new Withdrawal(amount));
    }
    }
    History
    instead of
    current
    status

    View Slide

  456. 26.09.23 //// Seite 748
    WPS – Workplace Solutions
    IDENTITY OF ENTITIES
    § In Java, every object has an identity
    § Namely its reference.
    § It's a technical identity.
    § An entity has a identity from a domain perspective
    § Different technical objects can represent the same entity
    § In different processes: e.g. on the client / on the server
    § When they are stored and later read from the database.
    § The identity can be implemented in two ways:
    § explicitly (e.g. IBAN) by using a unique key the user is aware of
    § implicitly (e.g. UUID / GUID) by using a technical key hidden
    from the user
    ?

    View Slide

  457. 26.09.23 //// Seite 749
    WPS – Workplace Solutions
    A BANK ACCOUNT—USING A UNIQUE KEY
    @Entity
    public class Account {
    @Identity
    private final IBAN _iban;
    public Account(IBAN iban) {
    _iban = iban;
    }
    @Override
    public boolean equals(Object other) {
    return _iban.equals(((Account)other)._iban);
    }
    // ...
    }
    The identity is
    immutable
    The key is of
    course a value
    object
    The implementation
    of equals () is based
    on the unique key

    View Slide

  458. 26.09.23 //// Seite 750
    WPS – Workplace Solutions
    HOW CAN A NEW UNIQUE KEY BE CREATED?
    § Database
    § Pro: Central
    § Can sometimes be created “lazily”
    § A system-wide value type factory
    § Domain-specific keys known to the user
    § Often implemented by the repository of a corresponding aggregate type the key is used for
    § A UUID / GUID generator
    § Pro: can be generated independently on any server or client
    § Con: String in a special format

    View Slide

  459. 26.09.23 //// Seite 751
    WPS – Workplace Solutions
    BANK TRANSFER—BY DOMAIN SERVICE
    @Service
    public class CreditTransferService {
    public void transfer(Account source, account destination, Amount amount) {
    source.withdraw(amount);
    destination.deposit(amount);
    }
    }
    No fields à
    stateless

    View Slide

  460. EXERCISE:
    MOBPROGRAMMING
    @ THE CINEMA

    View Slide

  461. 26.09.23 //// Seite 754
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN GLOSSARY—REVISITED
    DEPTH
    DEPTH AT A SPECIFIC
    GEO POSITION CONSIDERING
    THE CURRENT TIDAL LEVEL
    DEPTH
    DEPTH BELOW SEA LEVEL
    MEASURED BY ECHO
    SOUNDER
    MANEUVER-
    PLANNING
    SOUNDING SERVICE

    View Slide

  462. 26.09.23 //// Seite 755
    WPS – Workplace Solutions
    EVERY BOUNDED CONTEXT HAS ITS OWN IMPLEMENTATION
    public struct Depth
    {
    public Depth(
    int depthBelowSeaLevel,
    int tideLevel)
    {
    // ...
    }
    }
    MANEUVER-
    PLANNING
    SOUNDING SERVICE
    public class Depth {
    public Depth(
    int centimeter,
    LocalDate soundingDate) {
    // ...
    }
    }

    View Slide

  463. View Slide

  464. @hschwentner
    What now?

    View Slide

  465. @hschwentner
    Further topics:
    Ubiquitous Language
    Tactical Design
    (Entity, Value Object,
    Aggregate, Repository,
    Service)

    View Slide

  466. @hschwentner
    Consulting

    View Slide

  467. Workshop
    Domain-Driven Design konkret
    wps.de/ddd

    View Slide

  468. View Slide

  469. @hschwentner
    LeasingNinja.io

    View Slide

  470. @hschwentner
    Literature

    View Slide

  471. Foto: H. Schwentner

    View Slide

  472. Conways Law Blue Book
    Red Book
    2015 ’16 ’17
    ’14
    ’13
    ’12
    2010 ’11
    ’09
    ’08
    ’07
    ’06
    2005
    ’04
    ’03
    ’02
    2000 01
    1968
    ...
    ’99
    ’98 ’19 2020
    ’18

    View Slide

  473. View Slide

  474. Buy it online:
    https://hschwentner.io

    View Slide

  475. View Slide

  476. View Slide

  477. @hschwentner
    FEEDBACK

    View Slide

  478. View Slide

  479. View Slide

  480. @hschwentner
    Bibliography
    Brandolini, Alberto. Introducing EventStorming. Self-published, Leanpub.
    Cockburn, Alistair. “Hexagonal Architecture.” January 4, 2005.
    https://alistair.cockburn.us/hexagonal-architecture/.
    Conway, Melvin E. “How Do Committees Invent?” Datamation 14, no. 5 (April 1968):
    28–31.
    Evans, Eric. Domain-Driven Design: Tackling Complexity in the Heart of Software.
    Boston: Addison-Wesley, 2004.
    Foote, Brian and Joseph Yoder. “Big Ball of Mud.” PLoP ’97, Monticello, IL, September
    1997.
    Fowler, Martin. Patterns of Enterprise Application Architecture. Boston: Addison-
    Wesley, 2003.
    Fowler, Martin. “Strangler Fig Application.” Bliki, June 29, 2004.
    Hofer, Stefan and Henning Schwentner. Domain Storytelling: a Collaborative, Visual,
    and Agile Way to Develop Domain-Driven Software. Boston: Addison-Wesley, 2022.

    View Slide

  481. View Slide

  482. Henning Schwentner
    ⌂ https://hschwentner.io
    @hschwentner
    [email protected]

    View Slide