Domain-Driven Design - True North PHP 2016

Domain-Driven Design - True North PHP 2016

Be9645e99392d37fd0e8018f22122fc3?s=128

Andrew Cassell

November 04, 2016
Tweet

Transcript

  1. Domain-driven Design Deconstructed Domain-driven Design Deconstructed Andrew Cassell

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

  8. None
  9. None
  10. None
  11. None
  12. Domain-Driven Design

  13. DDD

  14. None
  15. None
  16. None
  17. 18

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

    a quarter-inch hole. Theodore Levitt
  20. Domain

  21. Domain

  22. Penn State Pattee Library

  23. book user book_inventory book_inventory_user_link ROLE_USER ROLE_ADMIN

  24. Source: http://jonclaytonbiz.com/

  25. None
  26. #DDD4ME

  27. None
  28. None
  29. None
  30. None
  31. Common Sense Software Development

  32. http://www.sei.cmu.edu/reports/95tr021.pdf

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

  35. Performance

  36. None
  37. Security

  38. DDD Common Misconceptions

  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. CRUD is okay-ish

  47. None
  48. None
  49. None
  50. None
  51. Ubiquitous Language

  52. Ubiquitous Language Developers Domain Experts

  53. None
  54. None
  55. Ubiquitous Language

  56. User Customer Client Member Affiliate

  57. 1. Naming Things 2. Cache Invalidation 3. Off By One

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

  59. $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();
  60. None
  61. $catalog = new LibraryCatalog(…); $book = $catalog->findOneBookByISBN($isbn); $members = new

    Members(…); $borrower = $members->findOneByAccountNumber($id); $borrower->borrow($book); $members->updateAccount($borrower);
  62. $borrower->borrow($book); $borrower->loan($book); $borrower->obtain($book); $borrower->take($book);

  63. DDD BALL OF MUD/CRUD Correctness ✓ Testability Usability Maintainability Modifiability

  64. None
  65. None
  66. Sales Marketing Finance Production

  67. Customer Lead Payee ShipTo

  68. Slide: Vaughn Vernon

  69. None
  70. None
  71. None
  72. Alberto Brandolini

  73. http://ziobrando.blogspot.be/

  74. None
  75. None
  76. None
  77. None
  78. None
  79. None
  80. 7 Dirty Words When Meeting With a Domain Expert 1.Session

    2.Repository 3.Abstract 4.Interface 5.Class 6.Database 7.Foreign Key
  81. None
  82. None
  83. User Research

  84. Photo: Mathias Verraes

  85. None
  86. None
  87. None
  88. None
  89. None
  90. None
  91. None
  92. None
  93. None
  94. None
  95. None
  96. None
  97. Domain Objects Repositories Commands & Command handlers Business rules/specifications Messages

    Domain events 
 Aggregates Entities Value Objects Invariants Processes
  98. Domain Objects Repositories Commands & Command handlers Business Rules/Specifications Messages

    Domain Events 
 Aggregates Entities Value Objects Invariants Processes
  99. Value Object Immutable No Identity (Only Values) Entity Mutable Has

    A Lifecycle Always Stable Contains Value Objects Aggregate Entity Responsible For Child Entities
  100. Value Objects Amount Paid Email Address BookTitle Dewey Decimal Number

    ISBN Number
  101. Immutability Avoid Spooky Action at a Distance

  102. Value Objects

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

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

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

  106. Value Objects ALWAYS IN A VALID STATE

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

    Setters •All Parameters Are Injected in the Constructor •Do Not Store References to Mutable Objects (Private Scalars Okay) •Throw Exceptions If Something Is Wrong •Functions that Appear to Modify Should Return a New Object •Declare the Class Final so it can’t be Overridden (probably) Value Objects
  108. Example BookTitle

  109. None
  110. None
  111. None
  112. None
  113. None
  114. None
  115. None
  116. Example BookCondition

  117. None
  118. None
  119. None
  120. None
  121. Example DateBorrowed

  122. None
  123. Example DateDue

  124. None
  125. Example LoanPeriod

  126. None
  127. DDD CRUD/MUD BookTitle String BookCondition String ISBN String DeweyDecimal Float(7,3)

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

    Modifiability
  129. None
  130. Identifiable Has State and is Mutable Never Invalid State Required

    Parameters Passed in Constructor Operates Using Value Objects No Security or Permission Checks Storage Agnostic Entities
  131. Entities Librarian Book

  132. None
  133. None
  134. SETTERS ARE BAD

  135. None
  136. None
  137. None
  138. None
  139. None
  140. None
  141. Doctrine

  142. None
  143. •Special Kind of Entity •Manages Child Entities •Transactionally Safe Aggregates

  144. Aggregates Member Fines Invoice

  145. None
  146. None
  147. None
  148. None
  149. None
  150. Task Based User Interface

  151. None
  152. None
  153. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability ✓

    Maintainability Modifiability
  154. Security

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

    Templates
  156. Controller Domain Framework

  157. None
  158. None
  159. None
  160. None
  161. Controller Domain Framework

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

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

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

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

    Maintainability ✓ Modifiability
  166. Domain Events

  167. Domain Events •Something That Happened In The Past •Immutable Value

    Objects •Part of the Core Domain •Usually Created in an Aggregate •Probably Want To Persist Them •Passed to Event Bus (Application Layer) •Listeners are Application Services •Used To Cross Bounded Contexts •Do Not Contain Entities (Only Value Objects)
  168. None
  169. None
  170. None
  171. BookWasOverdue

  172. None
  173. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability ✓

    Maintainability ✓ Modifiability ✓
  174. Command Query Responsibility Segregation

  175. CQRS

  176. Slide: Microsoft

  177. None
  178. None
  179. None
  180. None
  181. https://gnugat.github.io/ 2016/05/11/towards-cqrs- command-bus.html

  182. None
  183. None
  184. None
  185. Controller Domain Framework

  186. Event Sourcing

  187. Event Sourcing •Store the Events That Happen •Append Only Event

    Storage •Object Properties are Not Stored
  188. None
  189. None
  190. None
  191. Event Sourcing •Avoids Data Mapping •Avoids Object-relational Impedance Mismatch •Reduces

    Database Table Counts (Related Tables) •Potentially Reduces Model Counts
  192. Slide: Microsoft

  193. None
  194. None
  195. None
  196. Libraries •https://github.com/szjani/predaddy •https://github.com/prooph •https://github.com/qandidate-labs/broadway

  197. Learning More

  198. None
  199. DevBookClub https://www.youtube.com/user/devbookclub

  200. None
  201. None
  202. None
  203. None
  204. None
  205. DDDinPHP Google Group http://DDDinPHP.org

  206. None
  207. None
  208. Manage Private Internal Dependencies With Composer and Satis

  209. https://joind.in/talk/82ad0 Topics Covered •Ubiquitous Language •Event Storming •Modelling •Value Objects

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