Domain-Driven Design

Domain-Driven Design

Talk given at PHP Serbia 2017:
(http://www.conf2017.phpsrbija.rs/)

Be9645e99392d37fd0e8018f22122fc3?s=128

Andrew Cassell

May 28, 2017
Tweet

Transcript

  1. Domain-Driven Design Deconstructed Domain-Driven Design Deconstructed Andrew Cassell @alc277 andrewcassell.com

  2. None
  3. None
  4. None
  5. None
  6. Domain-Driven Design

  7. Domain-Driven Design

  8. None
  9. None
  10. None
  11. None
  12. 12

  13. None
  14. Domain

  15. Domain

  16. Penn State Pattee Library

  17. book user book_inventory book_inventory_user_link ROLE_USER ROLE_ADMIN

  18. Source: http://jonclaytonbiz.com/

  19. None
  20. None
  21. People don't want to buy a quarter-inch drill, they want

    a quarter-inch hole. Theodore Levitt
  22. None
  23. None
  24. Common Sense Software Development

  25. DDD !=

  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. DDD CRUD Correctness Testability Usability Maintainability Modifiability

  33. DDD BALL OF MUD / CRUD Correctness Testability Usability Maintainability

    Modifiability
  34. None
  35. None
  36. None
  37. None
  38. Ubiquitous Language

  39. Ubiquitous Language Developers Domain Experts

  40. Ubiquitous Language

  41. User Customer Client Member Employee

  42. None
  43. 1. Naming Things 2. Cache Invalidation 3. Off By One

    Errors Top 10 Reasons Programming is Hard
  44. CRUD/MUD DDD Users Members BookInventoryRepository LibraryCatalog String ISBN Float(7,3) DeweyDecimal

  45. $user = User::findOrFail($uuid);
 $book = Books::where('isbn', '=', $isbn)->take(1)->get();
 
 


    if (!$user->userBookLinks()->contains($book->id) && $user->borrowLinks->count() < 5) {
 
 $user->userBookLinks()->attach($book->id, ['date_due' => new DateTime('+2 Weeks')];
 
 }
 
 
 $user->save();
  46. None
  47. $catalog = new LibraryCatalog(…); $book = $catalog->findOneBookByISBN($isbn); $members = new

    Members(…); $borrower = $members->findOneByAccountNumber($acctNumber); $borrower->borrow($book);
  48. $borrower->borrow($book); $borrower->loan($book); $borrower->obtain($book); $borrower->rent($book);

  49. @library @loan Feature: A member should be able to borrow

    a book Scenario: Member borrows a book Given I am authenticated as a librarian And I am on “/member/behat-123" And I press "Borrow Book" And I fill out form with: | ISBN | | 978-032-112521-7 | Then I see "Domain-driven Design"
  50. @library @loan Feature: A member should be able to borrow

    a book Scenario: Member borrows a book Given I am authenticated as a librarian And I am on “/member/behat-123" And I press "Borrow Book" And I fill out form with: | ISBN | | 978-032-112521-7 | Then I see "Domain-driven Design"
  51. […], we want to establish the idea that a computer

    language is not just a way of getting a computer to perform operations but rather that it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute.
  52. DDD BALL OF MUD/CRUD Correctness ✓ Testability Usability Maintainability Modifiability

  53. None
  54. None
  55. None
  56. None
  57. Sales Marketing Finance Production

  58. Customer Lead Payee ShipTo

  59. Slide: Vaughn Vernon

  60. None
  61. Billing Loan Catalog

  62. Billing Loan Catalog

  63. Billing Loan Catalog Message

  64. Billing Loan Catalog Domain Event

  65. None
  66. None
  67. None
  68. None
  69. None
  70. 7 Dirty Words When Meeting With a Domain Expert 1.Session

    2.Repository 3.Abstract 4.Interface 5.Class 6.Database 7.Foreign Key
  71. User Research

  72. Photo: Mathias Verraes

  73. None
  74. None
  75. None
  76. None
  77. None
  78. None
  79. DDD BALL OF MUD/CRUD Correctness ✓✓ Testability Usability Maintainability Modifiability

  80. Domain Objects Value Objects Entities Aggregates Domain Events

  81. Value Object Immutable No Identity (Only Values) Book Title Amount

    Paid ISBN Number Entity Identifiable Mutable Lifecycle Contains Value Objects Book Librarian Invoice Aggregate Entity Responsible For Child Entities Transaction Boundary Member Library
  82. Book Title Dewey Decimal Number Date Borrowed ISBN Number Email

    Address Value Objects
  83. BookTitle DeweyDecimalNumber DateBorrowed ISBNNumber EmailAddress Value Objects

  84. Not Just Static Typing https://github.com/Fiedzia/type-system-research/blob/master/README.md Value Objects

  85. Immutable Avoid Spooky Action at a Distance

  86. ALWAYS IN A VALID STATE Value Objects

  87. Value Objects

  88. “Gateway Drug to Test Driven Development” Value Objects

  89. “Gateway Drug to Test Driven Development” Value Objects #MYTESTSDONTPASS

  90. Plain PHP Objects (POPOs) Value Objects

  91. Plain PHP Objects (POPOs) • Declare Class Properties as Private

    • No Setters • No References to Mutable Objects • Throw Exceptions in Constructor Value Objects
  92. BookTitle Example

  93. None
  94. None
  95. None
  96. None
  97. None
  98. None
  99. None
  100. DateBorrowed Example

  101. None
  102. RenewalsRemaining Example

  103. None
  104. None
  105. Example LoanPeriod

  106. None
  107. None
  108. DDD CRUD/MUD BookTitle String BookCondition String ISBN String DeweyDecimal Float(7,3)

    DateBorrowed DateTime
  109. DDD BALL OF MUD/CRUD Correctness ✓✓ Testability ✓ Usability Maintainability

    Modifiability
  110. None
  111. Librarian Library Book Entities

  112. Identifiable Have State and are Mutable Never Invalid State Operate

    Using Value Objects No Security or Permission Checks Storage Agnostic Entities
  113. None
  114. None
  115. None
  116. None
  117. Doctrine

  118. None
  119. None
  120. None
  121. SETTERS ARE BAD

  122. None
  123. None
  124. None
  125. None
  126. None
  127. • Special Kind of Entity • Manages Child Entities •

    Transaction Boundary Aggregates
  128. Aggregates Member FinesInvoice

  129. None
  130. None
  131. None
  132. None
  133. None
  134. Task Based User Interface

  135. None
  136. None
  137. DDD BALL OF MUD/CRUD Correctness ✓✓ Testability ✓ Usability ✓

    Maintainability Modifiability
  138. None
  139. Security

  140. Domain (Business Logic) Application Services Controller Persistence Event/Command Bus Security

    Templates
  141. Controller Domain Framework

  142. None
  143. None
  144. None
  145. None
  146. Controller Domain Framework

  147. Mathias Verraes - Decoupling the Model from the Framework at

    Laracon EU 2014 https://www.youtube.com/watch?v=QaIGN_cTcc8
  148. Ruby Midwest 2011 - Keynote: Architecture the Lost Years by

    Robert Martin https://www.youtube.com/watch?v=WpkDN78P884
  149. Domain (Business Logic) Application Services Controller Persistence Event/Command Bus Security

    Templates
  150. DDD BALL OF MUD/CRUD Correctness ✓✓ Testability ✓ Usability ✓

    Maintainability ✓ Modifiability
  151. Domain Events

  152. Domain Events • Part of the Core Domain • Happened

    In The Past • Important Enough To Record (Persist) • Important Enough To Concern Other Bounded Contexts • Immutable Value Objects • Do Not Contain Entities or Other Mutable Objects • Can Be Created in an Entity
  153. None
  154. None
  155. None
  156. None
  157. DamagedBookWasRemovedFromCirculation

  158. DamagedBookWasRemovedFromCirculation

  159. None
  160. None
  161. None
  162. None
  163. None
  164. DDD BALL OF MUD/CRUD Correctness ✓✓ Testability ✓ Usability ✓

    Maintainability ✓ Modifiability ✓
  165. Command Query Responsibility Segregation

  166. CQRS

  167. Write Read

  168. Write Model Read Model(s)

  169. Write Domain Model Read Model(s)

  170. Controller Command Bus Handler Handler Handler

  171. Controller Command Bus Handler Handler Handler Request

  172. Controller Command Bus Handler Handler Handler Command

  173. None
  174. Controller Command Bus Handler Handler Handler Command

  175. Controller Command Bus Handler Handler Handler Command

  176. Controller Command Bus Handler Handler Handler Command

  177. Controller Command Bus Handler Handler Handler Response

  178. Controller Command Bus Handler Handler Handler Response

  179. Controller Command Bus Handler Handler Handler Response

  180. Controller Domain Framework

  181. https://gnugat.github.io/ 2016/05/11/towards-cqrs- command-bus.html

  182. None
  183. Event Sourcing

  184. None
  185. None
  186. None
  187. • Avoids Data Mapping • Avoids Object-relational Impedance Mismatch •

    Reduces Database Table Counts (Related Tables) • Potentially Reduces Model Counts Event Sourcing
  188. Event Sourcing • Object Properties are Not Persisted • Events

    Are Persisted to Append Only Event Storage
  189. None
  190. None
  191. None
  192. Libraries • https://github.com/broadway • https://github.com/prooph • https://github.com/szjani/predaddy

  193. Learning More

  194. None
  195. DEV BOOK CLUB https://www.youtube.com/user/devbookclub

  196. None
  197. None
  198. None
  199. None
  200. DDDinPHP Google Group http://DDDinPHP.org

  201. None
  202. None
  203. https://joind.in/talk/e8752 DDD Topics Covered •Ubiquitous Language •Event Storming •Modelling •Value

    Objects •Entities •Aggregates •Hexagonal Architecture •CQRS •Event Sourcing