Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Domain-Driven Design (Tutorial)

Domain-Driven Design (Tutorial)

Tutorial »Domain-Driven Design«

Henning Schwentner

December 14, 2022
Tweet

More Decks by Henning Schwentner

Other Decks in Programming

Transcript

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

  2. None
  3. None
  4. !

  5. None
  6. Infrastructure:

  7. Remote Etiquette: * Video on * Real name

  8. ! “I can’t hear you.” " “I’d like to say

    something.”
  9. Button “Reactions” à “Raise hand

  10. Discussion encouraged J

  11. @hschwentner Handout and case study: https://speakerdeck.com/hschwentner

  12. @hschwentner Domain-Driven Design

  13. @hschwentner #dddesign

  14. -DRIVEN DESIGN

  15. None
  16. None
  17. None
  18. None
  19. None
  20. »Business-Software die Spaß macht«

  21. None
  22. None
  23. @hschwentner Timetable

  24. Workshop Workshop Workshop Workshop Morning Afternoon Not remote

  25. Morning Afternoon Work shop Work shop Work shop Work shop

    Work shop Work shop Work shop
  26. @hschwentner Contents

  27. None
  28. @hschwentner #unabsteigbar

  29. None
  30. None
  31. None
  32. None
  33. Workshop Domain-Driven Design concrete wps.de/ddd

  34. Ubiquitous Language

  35. 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
  36. Microservices CQRS Hexagonal Architecture Agile Event Sourcing Extreme Programming Scrum

    Cloud Self-Contained Systems Verticals Clean Architecture DevOps AWS Azure Onion Architecture
  37. @hschwentner Structure

  38. None
  39. 14.12.22 //// Seite 81 WPS – Workplace Solutions DOMÄNE DOMÄNE

  40. @hschwentner You

  41. Programming Languages My Questions My life as a developer My

    experiences using DDD My wishes regarding the training family My first computer
  42. None
  43. None
  44. Grafik: Robert Gloth/Wikipedia

  45. None
  46. Foto: Serge Melki/Wikipedia

  47. Foto: Public Domain/Wikipedia

  48. None
  49. @hschwentner How is that supposed to work?

  50. None
  51. None
  52. Foto: Henning Schwentner

  53. None
  54. Detail: Depth

  55. Detail: Tiefenzahl

  56. Foto: Afrank99/Wikipedia

  57. Foto: Henning Schwentner

  58. None
  59. None
  60. None
  61. None
  62. None
  63. 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
  64. @hschwentner Is There an App for It?

  65. None
  66. None
  67. CYBER

  68. None
  69. None
  70. None
  71. None
  72. None
  73. None
  74. @hschwentner Software != End in itself

  75. None
  76. ?

  77. None
  78. IMPORTANT!

  79. None
  80. So#ware Domain

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

  82. None
  83. Domain Knowledge Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

  84. None
  85. Groß Klein

  86. Collaborative Modeling

  87. Domain Expert Developer

  88. Knowledge crunching

  89. None
  90. @hschwentner Methods

  91. None
  92. https://domainstorytelling.org

  93. LANGUAGE

  94. Fach- sprache Domain Expert Developer

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

    dev
  96. Ludwig Wi)genstein “Whereof one cannot speak, thereof one must be

    silent.”
  97. Ludwig Wittgenstein »Wovon man nicht sprechen kann, darüber muss man

    schweigen.«
  98. Depth Scale Silhouette Marking Depth Map to move to measure

    run aground neon yellow
  99. Ubiquitous Language

  100. Jubikjutuss Lengwitsch

  101. 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
  102. SMALL

  103. None
  104. class Silhouette { //... }

  105. None
  106. class Silhouette { public void moveBy(Length length) //... }

  107. None
  108. class Silhouette { public void moveBy(Length length) //... public void

    turnBy(Angle angle) //... }
  109. Erich Gamma “Program to an interface, not to an implementation”

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

  111. WARNING SETTERS CAUSE MODEL ANEMIA

  112. MOVES & TURNS 8 SHIP SILHOUETTE TO FIND ROUTE NAUTICAL

    OFFICER «En$ty» Silhoue(e move() turn()
  113. Tactical Design

  114. None
  115. Entity

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

  118. Value Object

  119. Example: Depth

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

  121. Domain Event

  122. MANEUVER PLANNING DEPTHS SOUNDING DEPTH MAP COMPLETED

  123. BIG

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

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

  126. None
  127. None
  128. None
  129. #

  130. None
  131. None
  132. Bounded Context

  133. #

  134. 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
  135. TIDAL FORE- CAST MANEUVER- PLANNING DEPTH MEASUREMENT

  136. Ubiquitous Language

  137. None
  138. WPS – Workplace Solutions #dddkonkret www.wps.de COLLABORATIVE MODELING Domain-Driven Design

    concrete
  139. 14.12.22 //// Seite 245 WPS – Workplace Solutions INHALT DOMÄNE

    DOMÄNE
  140. None
  141. @hschwentner Tools

  142. None
  143. None
  144. None
  145. Infrastructure: https://www.wps.de/modeler/ www.egon.io github.com/WPS/domain-story-modeler

  146. http://www.domainstorytelling.or

  147. 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
  148. None
  149. None
  150. @hschwentner Explained

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

  152. @hschwentner The Workshop Format

  153. @hschwentner The right people

  154. None
  155. None
  156. None
  157. @hschwentner DEPTH MAP HANDS OVER NAUTICAL OFFICER CARTOGRAPHER TO “The

    cartographer sends the depth map to the nautical officer” SENDS
  158. Active Listening

  159. CONCRETE STORIES VS. ABSTRACT PROCESSES

  160. @hschwentner The Pictographic Language

  161. @hschwentner ßDraw here Leave some empty space there à Annota3ons

    and varia3ons Precondi3ons and assump3ons Name of the Domain Story
  162. @hschwentner actor work object activity 5 sequence number

  163. @hschwentner SHIP SILHOUETTE CAPTAIN MOVES

  164. @hschwentner DRAWS MEASURES DEPTH DEPTH MAP

  165. @hschwentner SENDS DRAWS DEPTH MAP DEPTH MAP

  166. @hschwentner person group IT system

  167. ACTORS ONCE/ WORK OBJECTS SEVERAL TIMES PUTS NAUTICAL OFFICER 7

    SHIP SILHOUETTE MOVES & TURNS 8 SHIP SILHOUETTE
  168. PICTOGRAPHIC LANGUAGE – NO IF/SWITCH/OR actor work object ac3vity 5

    sequence number
  169. Scenario-based modeling

  170. Scenarios Maneuver Planning – The Happy Path Maneuver Planning –

    Storm Flooding Expected Maneuver Planning – All Berths Full
  171. @hschwentner Scope

  172. Scope Factors

  173. @hschwentner Granularity Point in time Domain Purity

  174. Granularity fine- grained coarse- grained x x x x x

    x x x x x x x x x x x x x
  175. @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
  176. Point in Time as-is to-be t

  177. Point in Time now optimized t with new system

  178. Domain Purity pure digitalized

  179. @hschwentner Tools

  180. None
  181. PowerPoint

  182. @hschwentner

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

  184. @hschwentner Purposes

  185. @hschwentner draw boundaries learn language work on requirements implement domain

    model find shadow IT …
  186. @hschwentner More on Domain Storytelling

  187. None
  188. More info: https://hschwentner.io

  189. 14.12.22 //// Seite 339 WPS – Workplace Solutions EXERCISE: Domain

    Storytelling in Groups
  190. WPS – Workplace Solutions #dddkonkret www.wps.de STRATEGIC DESIGN Domain-Driven Design

    concrete
  191. 14.12.22 //// Seite 342 WPS – Workplace Solutions ÜBERSICHT DOMÄNE

    DOMÄNE
  192. ?

  193. ?

  194. None
  195. None
  196. None
  197. None
  198. None
  199. ?

  200. ?

  201. None
  202. @hschwentner “Whoever uses a canonical model has lost control over

    his life.” – Karl Lagerfeld
  203. @hschwentner »Wer ein unternehmensweites Domänenmodell einsetzt, hat die Kontrolle über

    sein Leben verloren« – Karl Lagerfeld
  204. None
  205. !

  206. None
  207. #

  208. None
  209. None
  210. None
  211. None
  212. Bounded Context

  213. #

  214. Bewegung Kosme<k # Gefühle

  215. 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
  216. @hschwentner Another Example

  217. None
  218. None
  219. @hschwentner Do I get a car for this?

  220. None
  221. 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
  222. CUSTOMER SIGNS CONTRACT 3 RISK MANAGER CONTRACT VOTES 7 «Entity»

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

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

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

  226. None
  227. None
  228. @hschwentner Why is that?

  229. None
  230. None
  231. @hschwentner Too big to be understood as a whole

  232. None
  233. None
  234. 14.12.22 //// Seite 385 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 MANAGMENT SALES RISK MANAGMENT
  235. «Entity» Contract sign() vote() ✘

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

  237. «En$ty» Contract sign() vote() ✘

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

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

  240. 14.12.22 //// Seite 391 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
  241. 14.12.22 //// Seite 392 WPS – Workplace Solutions

  242. 14.12.22 //// Seite 393 WPS – Workplace Solutions Team Head

    Team Tail Team Body Team Legs
  243. 14.12.22 //// Seite 394 WPS – Workplace Solutions Team Emma

    Team Berta Team Erna Team Lisl
  244. @hschwentner Code Database Team

  245. @hschwentner Drawing Boundaries with Domain Storytelling

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

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

  248. 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
  249. 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
  250. @hschwentner Naming Subdomains Express what is done Verbs turned into

    nouns Often: -ing-form Anti-pattern: name of work object as name for subdomain
  251. @hschwentner Naming Subdomains— Examples Price calculation Risk assess- ment Maneuver

    planning Customer Car Price Payment Car inspection
  252. 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!
  253. 14.12.22 //// Seite 416 WPS – Workplace Solutions EXERCISE: Find

    Bounded Contexts
  254. 14.12.22 //// Seite 417 WPS – Workplace Solutions BOUNDED CONTEXTS

    FÜR DAS KINO ABLAUF- PLANUNG KARTEN- VERKAUF BUCH- HALTUNG
  255. 14.12.22 //// Seite 439 WPS – Workplace Solutions AND NOW?

    § Core vs Supporting § Event Storming § Taktisches Design § Code/LeasingNinja § Hexagonal/Onion Architecture § Context Mapping § How to split the monolith
  256. @hschwentner How to split the monolith

  257. 1) How should it be? 2) How is it? 3)

    How to move the “is” to the “ideal”? RISK MANAGE- MENT SALES
  258. 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)
  259. old old old old new new new new “just flip

    the lever” 1 2 3 4 5 a.k.a. big bang replacement
  260. old old old new new new new 2 3 4

    5 old 1
  261. Strangler Fig Application

  262. Strangler Fig Application

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

  264. <<En$ty>> Contract sign() SALES RISK ASSESSMENT <<En$ty>> Contract sign() vote()

    … BIG BALL OF MUD <<En$ty>> Contract vote()
  265. Read on at: h#ps://hschwentner.io/domain-driven-refactorings

  266. @hschwentner Always Microservices?

  267. Simon Brown

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

    Of Mud
  269. None
  270. @hschwentner Consulting

  271. 14.12.22 //// Seite 519 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
  272. WPS – Workplace Solutions #dddkonkret www.wps.de UBIQUITOUS LANGUAGE Domain-Driven Design

    konkret
  273. 14.12.22 //// Seite 521 WPS – Workplace Solutions ÜBERSICHT DOMÄNE

    DOMÄNE
  274. “There are only two hard things in Computer Science: cache

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

    invalidation, naming things, and off-by-one errors.” Phil Karlton
  276. 14.12.22 //// Seite 524 WPS – Workplace Solutions TECHNISCHE SPRACHE

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

    X Class Das Bild kann nicht angezeigt werden. Database Server Client O/R-Mapping Inheritance A B Method Interface Linux Windows Eclipse Visual Studio
  278. 14.12.22 //// Seite 526 WPS – Workplace Solutions DOMÄNEN-SPRACHE –

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

    BEISPIEL SCHIFFAHRT Container Containernummer 4300 Kran Twistlock Frachtbrief
  280. 14.12.22 //// Seite 528 WPS – Workplace Solutions DOMÄNEN-SPRACHE –

    BEISPIEL SCHACH Brett König Figuren Spieler Schachuhr
  281. 14.12.22 //// Seite 529 WPS – Workplace Solutions DOMÄNEN-SPRACHE –

    BEISPIEL SCHACH II Remis Zugzwang Schachmatt Partie Zug Rochade
  282. 14.12.22 //// Seite 530 WPS – Workplace Solutions Fach- sprache

    Techno Babble ? MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  283. 14.12.22 //// Seite 531 WPS – Workplace Solutions Fach- sprache

    Techno Babble Fach- sprache MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  284. 14.12.22 //// Seite 532 WPS – Workplace Solutions Fach- sprache

    Techno Babble MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  285. 14.12.22 //// Seite 533 WPS – Workplace Solutions Techno Babble

    Fach- sprache MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  286. 14.12.22 //// Seite 534 WPS – Workplace Solutions Fach- sprache

    { Techno Babble } ? MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  287. 14.12.22 //// Seite 535 WPS – Workplace Solutions Fach- sprache

    { Fach- sprache } MOTIVATION ALLGEGENWÄRTIGE SPRACHE
  288. 14.12.22 //// Seite 536 WPS – Workplace Solutions Fach- sprache

    { Fach- sprache } UBIQUITOUS LANGUAGE public class SchiffsSilhouette { public void verschiebe { … } public void drehe { … } … }
  289. 14.12.22 //// Seite 537 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“
  290. 14.12.22 //// Seite 538 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.
  291. 14.12.22 //// Seite 539 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
  292. 14.12.22 //// Seite 540 WPS – Workplace Solutions DOMÄNEN-SPRACHE Brett

    König Figuren Spieler Schachuhr
  293. 14.12.22 //// Seite 541 WPS – Workplace Solutions UBIQIUTOUS LANGUAGE

    Brett Figuren Spieler
  294. 14.12.22 //// Seite 542 WPS – Workplace Solutions UBIQUITOUS LANGUAGE

    – ITERATION 2 Figuren Spieler Nichtmenschlicher Spieler Brett
  295. 14.12.22 //// Seite 543 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
  296. None
  297. None
  298. None
  299. http://www.domainstorytelling.or

  300. None
  301. 14.12.22 //// Seite 549 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
  302. 14.12.22 //// Seite 550 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
  303. 14.12.22 //// Seite 551 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
  304. 14.12.22 //// Seite 552 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
  305. 14.12.22 //// Seite 553 WPS – Workplace Solutions EXERCISE: FACHLICHE

    KERNBEGRIFFE IM IST IDENTIFIZIEREN
  306. 14.12.22 //// Seite 554 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 ? ?
  307. 14.12.22 //// Seite 555 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
  308. 14.12.22 //// Seite 556 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
  309. 14.12.22 //// Seite 557 WPS – Workplace Solutions REDUKTION AUF

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

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

    BOUNDED CONTEXT »KARTENVERKAUF« Saalplan Vorstellung Reservierungs- nummer Kinokarte Saalplanstapel Liste der Reservierungs- nummern Film Platz/Sitzplatz Saal/Kinosaal
  312. WPS – Workplace Solutions #dddkonkret www.wps.de FACHLICHE HANDLUNGEN Domain-Driven Design

    hands-on
  313. 14.12.22 //// Seite 561 WPS – Workplace Solutions ÜBERSICHT DOMÄNE

    DOMÄNE
  314. 14.12.22 //// Seite 562 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
  315. 14.12.22 //// Seite 563 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
  316. 14.12.22 //// Seite 564 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
  317. 14.12.22 //// Seite 565 WPS – Workplace Solutions EVENT STORMING

    § Eine Methode um Geschäftsprozesse zu modellieren § Modelliert werden: § Domain Events § Commands § Akteure § Aggregates
  318. 14.12.22 //// Seite 566 WPS – Workplace Solutions Akteur Arbeitsgegenstand

    /Arbeitsergebnis Handlung DOMAIN STORYTELLING
  319. 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
  320. CUSTOMER SIGNS CONTRACT 3 RISIK MANAGER CONTRACT VOTES 7 «En$ty»

    LeasingContract sign() vote()
  321. EXERCISE: FACHLICHE HANDLUNGEN IM IST IDENTIFIZIEREN

  322. 14.12.22 //// Seite 570 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
  323. 14.12.22 //// Seite 571 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
  324. WPS – Workplace Solutions #dddkonkret www.wps.de TACTICAL DESIGN Domain-Driven Design

    concrete
  325. 14.12.22 //// Seite 573 WPS – Workplace Solutions OVERVIEW DOMÄNE

    DOMÄNE
  326. 14.12.22 //// Seite 574 WPS – Workplace Solutions ARCHITECTURE WITH

    BOUNDED CONTEXTS TIDAL FORECAST MANEUVER PLANNING DEPTHS SOUNDING
  327. 14.12.22 //// Seite 575 WPS – Workplace Solutions ARCHITECTURE WITHIN

    A BOUNDED CONTEXT MANEUVER PLANNING User Interface Application Domain Infrastructure
  328. 14.12.22 //// Seite 576 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
  329. 14.12.22 //// Seite 577 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
  330. 14.12.22 //// Seite 578 WPS – Workplace Solutions below above

    FROM ABOVE/BELOW TO INSIDE/OUTSIDE out- side inside
  331. 14.12.22 //// Seite 579 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
  332. 14.12.22 //// Seite 580 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”
  333. 14.12.22 //// Seite 581 WPS – Workplace Solutions ONION ARCHITECTURE

    U I “application core” domain services domain model infra app ture struc serv lication ices Jeffrey Palermo !
  334. 14.12.22 //// Seite 582 WPS – Workplace Solutions CLEAN ARCHITECTURE

    Robert C. Martin ”Uncle Bob“
  335. 14.12.22 //// Seite 583 WPS – Workplace Solutions DIE OBJEKTORIENTIERTE

    GRUNDIDEE: FACHLICHE GEGENSTÄNDE ALS AUSGANGSPUNKT
  336. 14.12.22 //// Seite 584 WPS – Workplace Solutions FROM DOMAIN

    STORY TO DOMAIN MODEL Silhouette MOVES SILHOUETTE NAUTICAL OFFICER LENGTH Length moveBy(:Length) BY 8
  337. EXERCISE: BUILD DOMAIN MODEL FROM DOMAIN STUDIES IN CASE STUDY

  338. 14.12.22 //// Seite 586 WPS – Workplace Solutions En##es Value

    Objects Aggregates Services Factories Repositories
  339. 14.12.22 //// Seite 587 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
  340. 14.12.22 //// Seite 589 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
  341. None
  342. 14.12.22 //// Seite 591 WPS – Workplace Solutions ENTITIES «Entity»

    DepthMap depthMapId mark(Marking)
  343. 14.12.22 //// Seite 593 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
  344. Depth Diameter Position

  345. 14.12.22 //// Seite 596 WPS – Workplace Solutions VALUE OBJECTS

    «Value Object» Depth centimeters : int equals()
  346. 14.12.22 //// Seite 599 WPS – Workplace Solutions EXERCISE: ENTITES,

    VALUE OBJECTS
  347. 14.12.22 //// Seite 600 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
  348. 14.12.22 //// 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
  349. WPS – Workplace Solutions #dddkonkret www.wps.de REPOSITORIES AND AGGREGATES

  350. 14.12.22 //// Seite 603 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
  351. 14.12.22 //// Seite 604 WPS – Workplace Solutions REPOSITORY @

    HAMBURG PORT: THE DEPTH MAP CABINET
  352. 14.12.22 //// Seite 605 WPS – Workplace Solutions REPOSITORY: TWO

    PART IMPLEMENTATION «Repository Interface» DepthMapCabinet getDepthMap(Area, Scale) : DepthMap putBack(DepthMap) «Repository Implementation» MySQLDepthMapCabinet
  353. 14.12.22 //// Seite 606 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
  354. 14.12.22 //// Seite 607 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
  355. 14.12.22 //// Seite 608 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
  356. None
  357. 14.12.22 //// Seite 610 WPS – Workplace Solutions Aggregate «Aggregate

    Root» DepthMap «Entity» Marking «Value Object» Depth
  358. 14.12.22 //// Seite 611 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
  359. EXERCISE: DRAW AGGREGATE BOUNDARIES. WHICH REPOSITORIES DO WE NEED?

  360. 14.12.22 //// Seite 613 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
  361. WPS – Workplace Solutions #dddkonkret www.wps.de IMPLEMENTING THE BUILDING BLOCKS

    IN JAVA Domain-Driven Design concrete
  362. 14.12.22 //// Seite 632 WPS – Workplace Solutions

  363. 14.12.22 //// Seite 633 WPS – Workplace Solutions A BANK

    ACCOUNT—FIRST DRAFT public class Account { }
  364. 14.12.22 //// Seite 634 WPS – Workplace Solutions A BANK

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

    ACCOUNT—FIRST DRAFT import org.jmolecules.ddd.annotation.Entity; @Entity public class Account { }
  366. 14.12.22 //// Seite 636 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
  367. 14.12.22 //// Seite 637 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
  368. 14.12.22 //// Seite 638 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
  369. 14.12.22 //// Seite 640 WPS – Workplace Solutions DESIGN BY

    CONTRACT Foto: Urheber Fuchsias/Bertrand Meyer/Wikipedia/CC BY-SA 4.0
  370. 14.12.22 //// Seite 641 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
  371. 14.12.22 //// Seite 642 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
  372. 14.12.22 //// Seite 646 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…?
  373. 14.12.22 //// Seite 648 WPS – Workplace Solutions AN AMOUNT

    TYPE public class Amount { }
  374. 14.12.22 //// Seite 649 WPS – Workplace Solutions AN AMOUNT

    TYPE import org.jmolecules.ddd.annotation.ValueObject; @ValueObject public class Amount { }
  375. 14.12.22 //// Seite 650 WPS – Workplace Solutions AN AMOUNT

    TYPE @ValueObject public class Amount { private int _amount; private Currency _currency; }
  376. 14.12.22 //// Seite 651 WPS – Workplace Solutions AN AMOUNT

    TYPE @ValueObject public class Amount { private final int _amount; private final Currency _currency; }
  377. 14.12.22 //// Seite 652 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; } }
  378. 14.12.22 //// Seite 653 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); } }
  379. 14.12.22 //// Seite 654 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); } }
  380. 14.12.22 //// Seite 655 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() }
  381. 14.12.22 //// Seite 656 WPS – Workplace Solutions AN AMOUNT

    TYPE—JAVA 14 AND HIGHER @ValueObject public record Amount(int amount, Currency currency);
  382. 14.12.22 //// Seite 657 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
  383. 14.12.22 //// Seite 658 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
  384. 14.12.22 //// Seite 662 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
  385. 14.12.22 //// Seite 663 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
  386. 14.12.22 //// Seite 664 WPS – Workplace Solutions A BANK

    ACCOUNT—EVENT SOURCED @Entity public class Account { private List<FinancialTransaction> _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
  387. 14.12.22 //// Seite 667 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 ?
  388. 14.12.22 //// Seite 668 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
  389. 14.12.22 //// Seite 669 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
  390. 14.12.22 //// Seite 670 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
  391. EXERCISE: MOBPROGRAMMING @ THE CINEMA

  392. 14.12.22 //// Seite 673 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
  393. 14.12.22 //// Seite 674 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) { // ... } }
  394. WPS – Workplace Solutions #dddkonkret www.wps.de IMPLEMENTING THE BUILDING BLOCKS

    IN PHP Domain-Driven Design concrete
  395. 14.12.22 //// Seite 676 WPS – Workplace Solutions

  396. 14.12.22 //// Seite 677 WPS – Workplace Solutions A BANK

    ACCOUNT—FIRST DRAFT class Account { }
  397. 14.12.22 //// Seite 678 WPS – Workplace Solutions A BANK

    ACCOUNT—FIRST DRAFT use PHPMolecules\DDD\Attribute\Entity; #[Entity] class Account { }
  398. 14.12.22 //// Seite 679 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
  399. 14.12.22 //// Seite 680 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
  400. 14.12.22 //// Seite 681 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; }
  401. 14.12.22 //// Seite 683 WPS – Workplace Solutions DESIGN BY

    CONTRACT Foto: Urheber Fuchsias/Bertrand Meyer/Wikipedia/CC BY-SA 4.0
  402. 14.12.22 //// Seite 684 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/
  403. 14.12.22 //// Seite 685 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…?
  404. 14.12.22 //// Seite 687 WPS – Workplace Solutions AN AMOUNT

    TYPE class Amount { }
  405. 14.12.22 //// Seite 688 WPS – Workplace Solutions AN AMOUNT

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

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

    TYPE #[ValueObject] class Amount { private readonly int $amount; private readonly Currency $currency; }
  408. 14.12.22 //// Seite 691 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; } }
  409. 14.12.22 //// Seite 692 WPS – Workplace Solutions AN AMOUNT

    TYPE #[ValueObject] class Amount { private readonly int $amount; private readonly Currency $currency; private function __construct(int $amount, Currency $currency) { $this->amount = $amount; $this->currency = $currency; } public static function of(int $amount, Currency $currency): Amount { return new Amount($amount, $currency); } }
  410. 14.12.22 //// Seite 693 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; } public function equals(Amount $other): bool { return $this->amount === $other->amount && $this->currency->equals($other->currency); } }
  411. 14.12.22 //// Seite 696 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; } public function add(Amount $otherAmount): Amount { return Amount.of($this->amount + $otherAmount->amount, $this->currency); } } The new value object type behaves correctly
  412. 14.12.22 //// Seite 697 WPS – Workplace Solutions AN AMOUNT

    TYPE class Amount { private readonly int $amount; private readonly Currency $currency; public function __construct(int $amount, Currency $currency) { $this->amount = $amount; $this->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
  413. 14.12.22 //// Seite 698 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); }
  414. 14.12.22 //// Seite 699 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
  415. 14.12.22 //// Seite 700 WPS – Workplace Solutions A BANK

    ACCOUNT—EVENT SOURCED @Entity public class Account { private List<FinancialTransaction> _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
  416. 14.12.22 //// Seite 703 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 ?
  417. 14.12.22 //// Seite 704 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
  418. 14.12.22 //// Seite 705 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
  419. 14.12.22 //// Seite 706 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
  420. EXERCISE: MOBPROGRAMMING @ THE CINEMA

  421. 14.12.22 //// Seite 709 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
  422. 14.12.22 //// Seite 710 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) { // ... } }
  423. None
  424. @hschwentner What now?

  425. @hschwentner Further topics: Ubiquitous Language Tactical Design (Entity, Value Object,

    Aggregate, Repository, Service)
  426. @hschwentner Consulting

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

  428. None
  429. @hschwentner LeasingNinja.io

  430. @hschwentner Literature

  431. Foto: H. Schwentner

  432. 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
  433. None
  434. Buy it online: https://hschwentner.io

  435. None
  436. None
  437. @hschwentner FEEDBACK

  438. None
  439. None
  440. None
  441. None
  442. None
  443. Henning Schwentner ⌂ https://hschwentner.io @hschwentner [email protected]