Pro Yearly is on sale from $80 to $50! »

Domain-Driven Design & REST

Domain-Driven Design & REST

Slides of the talk I gave at JAX 2016, Mayence.

@springcentral

977c74bb044a9d4fa90b305824eda390?s=128

Oliver Drotbohm

April 19, 2016
Tweet

Transcript

  1. DDD & REST Domain-Driven APIs for the web / olivergierke

    Oliver Gierke
  2. 2

  3. Background 3

  4. 4 Spring Data Repositories &
 Aggregates Spring HATEOAS Hypermedia
 for

    Spring MVC Spring Data REST
  5. REST ≠ 
 CRUD via HTTP 5

  6. What does it take to bridge the worlds of DDD

    & REST? “
  7. None
  8. 8

  9. 8

  10. Value objects 9

  11. Stringly typed code 10 public class Customer { private Long

    id; private String firstname, lastname, email; … }
  12. Stringly typed code 11 public class SomeService { public void

    createUser(String firstname,
 String lastname, String email) { … } }
  13. 12 public class Customer { private Long id; private Firstname

    firstname; private Lastname lastname; private EmailAddress emailAddress; … }
  14. Value Objects are a
 PITA to build in
 some languages.

    13
  15. Still, they’re worth it. 14 See „Power Use of Value

    Objects in DDD“ by Dan Bergh Johnsson.
  16. Lombok — putting the spice back into Java. 15

  17. 16 @Value public class Customer { UUID id = UUID.randomUUID();

    Firstname firstname; Lastname lastname; EmailAddress email; @Value static class EmailAddress { String value; } }
  18. AutoValue 17 Project website on GitHub.

  19. Key opponents: Mapping libraries
 that need to
 (de)serialize them. 18

  20. Entities & Repositories 19

  21. 20 Order LineItem Product Invoice Customer Payment Address Email

  22. 20 Order LineItem Product Invoice Customer Payment Address Email

  23. Entity +
 Repository = 
 Aggregate 21

  24. Aggregates form nice representation boundaries. 22

  25. Aggregates become
 the key things
 to refer to. 23

  26. Don’t get trapped by datastore thinking. 24

  27. Try to avoid 
 bi-directional relationships. 25

  28. Domain Events 26

  29. 27 Level 0: No events at all

  30. 27 Level 0: No events at all Level 1: Explicit

    operations
  31. If you’re calling two setters in a row, you’re missing

    a concept. 28
  32. 29 Level 0: No events at all Level 1: Explicit

    operations
  33. 29 Level 0: No events at all Level 1: Explicit

    operations Level 2: Some operations as events
  34. Domain events as
 state transitions. 30

  35. Expose important
 events to interested parties via feeds. 31

  36. 32 Level 0: No events at all Level 1: Explicit

    operations Level 2: Some operations as events
  37. 32 Level 0: No events at all Level 1: Explicit

    operations Level 2: Some operations as events Level 3: Event Sourcing
  38. REST 33

  39. Representation design matters 34

  40. Aggregates Identifiable
 Referable
 Scope of consistency 35

  41. Resources Identifiable
 Referable
 Scope of consistency 36

  42. Hypermedia 37

  43. Serving data and navigation information
 at the same time. 38

  44. Trading domain knowledge with protocol complexity in clients. 39

  45. Reducing decisions in clients to whether a
 link is present

    or not. 40
  46. Prefer explicit 
 state transitions over poking at your resources

    using PATCH. 41
  47. Translate domain concepts into web- appropriate ones. 42

  48. 43 Aggregate Root / Repository Collection / Item Resources IDs

    URIs @Version ETags Last Modified Property Last Modified Header Relations Links
  49. RESTBucks payment expected preparing cancelled ready completed 1 2 3

    4 5 6
  50. Method URI Action Step POST /orders Create new order 1

    POST/PATCH /orders/{id} Update the order (only if "payment expected") 2 DELETE /orders/{id} Cancel order (only if "payment expected") 3 PUT /orders/{id}/payment Pay order (only if "payment expected") 4 Barista preparing the order GET /orders/{id} Poll order state 5 GET /orders/{id}/receipt Access receipt DELETE /orders/{id}/receipt Conclude the order process 6
  51. Method Resource type Action Step POST orders Create new order

    1 POST/PATCH update Update the order 2 DELETE cancel Cancel order 3 PUT payment Pay order 4 Barista preparing the order GET order Poll order state 5 GET receipt Access receipt DELETE receipt Conclude the order process 6
  52. Spring RESTBucks 47

  53. Web Service Repository - Orders Spring Data Spring Data
 REST

    Payment Spring Data Manual
 implementation Manual
 implementation
  54. JacksonCustomizations Externalize tweaks to the general JSON design 49

  55. Spring Data REST
 for the CRUDdy parts. 50

  56. ResourceProcessor To conditionally sneak links into the default representation. 51

  57. Code 52 Spring RESTBucks - https://github.com/olivergierke/spring-restbucks

  58. Questions? 53