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

Domain-Driven Design & REST

Domain-Driven Design & REST

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

@springcentral

Oliver Drotbohm

April 19, 2016
Tweet

More Decks by Oliver Drotbohm

Other Decks in Programming

Transcript

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

    View Slide

  2. 2

    View Slide

  3. Background
    3

    View Slide

  4. 4
    Spring Data
    Repositories &

    Aggregates
    Spring HATEOAS
    Hypermedia

    for Spring MVC
    Spring Data REST

    View Slide

  5. REST ≠ 

    CRUD via HTTP
    5

    View Slide

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

    View Slide

  7. View Slide

  8. 8

    View Slide

  9. 8

    View Slide

  10. Value objects
    9

    View Slide

  11. Stringly typed code
    10
    public class Customer {
    private Long id;
    private String firstname, lastname, email;

    }

    View Slide

  12. Stringly typed code
    11
    public class SomeService {
    public void createUser(String firstname,

    String lastname, String email) {

    }
    }

    View Slide

  13. 12
    public class Customer {
    private Long id;
    private Firstname firstname;
    private Lastname lastname;
    private EmailAddress emailAddress;

    }

    View Slide

  14. Value Objects are a

    PITA to build in

    some languages.
    13

    View Slide

  15. Still, they’re worth it.
    14
    See „Power Use of Value Objects in DDD“ by Dan Bergh Johnsson.

    View Slide

  16. Lombok — putting the
    spice back into Java.
    15

    View Slide

  17. 16
    @Value
    public class Customer {
    UUID id = UUID.randomUUID();
    Firstname firstname;
    Lastname lastname;
    EmailAddress email;
    @Value
    static class EmailAddress {
    String value;
    }
    }

    View Slide

  18. AutoValue
    17
    Project website on GitHub.

    View Slide

  19. Key opponents:
    Mapping libraries

    that need to

    (de)serialize them.
    18

    View Slide

  20. Entities &
    Repositories
    19

    View Slide

  21. 20
    Order
    LineItem
    Product
    Invoice
    Customer
    Payment Address
    Email

    View Slide

  22. 20
    Order
    LineItem
    Product
    Invoice
    Customer
    Payment Address
    Email

    View Slide

  23. Entity +

    Repository = 

    Aggregate
    21

    View Slide

  24. Aggregates form nice
    representation
    boundaries.
    22

    View Slide

  25. Aggregates become

    the key things

    to refer to.
    23

    View Slide

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

    View Slide

  27. Try to avoid 

    bi-directional
    relationships.
    25

    View Slide

  28. Domain Events
    26

    View Slide

  29. 27
    Level 0: No events at all

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. Domain events as

    state transitions.
    30

    View Slide

  35. Expose important

    events to interested
    parties via feeds.
    31

    View Slide

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

    View Slide

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

    View Slide

  38. REST
    33

    View Slide

  39. Representation
    design matters
    34

    View Slide

  40. Aggregates
    Identifiable

    Referable

    Scope of consistency
    35

    View Slide

  41. Resources
    Identifiable

    Referable

    Scope of consistency
    36

    View Slide

  42. Hypermedia
    37

    View Slide

  43. Serving data and
    navigation information

    at the same time.
    38

    View Slide

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

    View Slide

  45. Reducing decisions in
    clients to whether a

    link is present or not.
    40

    View Slide

  46. Prefer explicit 

    state transitions over
    poking at your resources
    using PATCH.
    41

    View Slide

  47. Translate domain
    concepts into web-
    appropriate ones.
    42

    View Slide

  48. 43
    Aggregate Root /
    Repository
    Collection / Item
    Resources
    IDs URIs
    @Version ETags
    Last Modified
    Property
    Last Modified
    Header
    Relations Links

    View Slide

  49. RESTBucks
    payment
    expected
    preparing
    cancelled
    ready completed
    1
    2
    3
    4
    5 6

    View Slide

  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

    View Slide

  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

    View Slide

  52. Spring
    RESTBucks
    47

    View Slide

  53. Web
    Service
    Repository
    -
    Orders
    Spring Data
    Spring Data

    REST
    Payment
    Spring Data
    Manual

    implementation
    Manual

    implementation

    View Slide

  54. JacksonCustomizations
    Externalize tweaks to the
    general JSON design
    49

    View Slide

  55. Spring Data REST

    for the CRUDdy parts.
    50

    View Slide

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

    View Slide

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

    View Slide

  58. Questions?
    53

    View Slide