DDD & REST Domain-Driven APIs for the web Oliver Gierke / - - PowerPoint PPT Presentation

ddd rest
SMART_READER_LITE
LIVE PREVIEW

DDD & REST Domain-Driven APIs for the web Oliver Gierke / - - PowerPoint PPT Presentation

DDD & REST Domain-Driven APIs for the web Oliver Gierke / olivergierke 2 Background 3 Spring Data REST Spring Data Spring HATEOAS Repositories & Hypermedia Aggregates for Spring MVC 4 REST CRUD via HTTP 5


slide-1
SLIDE 1

DDD & REST

Domain-Driven APIs for the web / olivergierke Oliver Gierke
slide-2
SLIDE 2 2
slide-3
SLIDE 3

Background

3
slide-4
SLIDE 4 4

Spring Data

Repositories &
 Aggregates

Spring HATEOAS

Hypermedia
 for Spring MVC

Spring Data REST

slide-5
SLIDE 5

REST ≠ 
 CRUD via HTTP

5
slide-6
SLIDE 6

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

slide-7
SLIDE 7 7
slide-8
SLIDE 8 7
slide-9
SLIDE 9

Value objects

8
slide-10
SLIDE 10

Value Objects are a
 PITA to build in
 some languages.

9
slide-11
SLIDE 11

Still, they’re worth it.

10 See „Power Use of V alue Objects in DDD“ by Dan Bergh Johnsson.
slide-12
SLIDE 12

Lombok — putting the spice back into Java.

11
slide-13
SLIDE 13

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

12
slide-14
SLIDE 14

Entities & Repositories

13
slide-15
SLIDE 15 14
slide-16
SLIDE 16 14 Customer Payment Address Email
slide-17
SLIDE 17 14 Invoice Customer Payment Address Email
slide-18
SLIDE 18 14 Order LineItem Invoice Customer Payment Address Email
slide-19
SLIDE 19 14 Order LineItem Product Invoice Customer Payment Address Email
slide-20
SLIDE 20 14 Order LineItem Product Invoice Customer Payment Address Email
slide-21
SLIDE 21

Entity +
 Repository = 
 Aggregate

15
slide-22
SLIDE 22

Aggregates form nice representation boundaries.

16
slide-23
SLIDE 23

Aggregates become
 the key things
 to refer to.

17
slide-24
SLIDE 24

Don’t get trapped by datastore thinking.

18
slide-25
SLIDE 25

Try to avoid 
 bi-directional relationships.

19
slide-26
SLIDE 26

Domain Events

20
slide-27
SLIDE 27 21 Level 0: No events at all
slide-28
SLIDE 28 21 Level 0: No events at all Level 1: Explicit operations
slide-29
SLIDE 29

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

22
slide-30
SLIDE 30 23 Level 0: No events at all Level 1: Explicit operations Level 2: Some operations as events
slide-31
SLIDE 31

Domain events as
 state transitions.

24
slide-32
SLIDE 32

Expose important
 events to interested parties via feeds.

25
slide-33
SLIDE 33 26 Level 0: No events at all Level 1: Explicit operations Level 2: Some operations as events Level 3: Event Sourcing
slide-34
SLIDE 34

REST

27
slide-35
SLIDE 35

Representation design matters

28
slide-36
SLIDE 36

Aggregates

Identifiable
 Referable
 Scope of consistency

29
slide-37
SLIDE 37

Resources

Identifiable
 Referable
 Scope of consistency

30
slide-38
SLIDE 38

Hypermedia

31
slide-39
SLIDE 39

Serving data and navigation information
 at the same time.

32
slide-40
SLIDE 40

Trading domain knowledge with protocol complexity in clients.

33
slide-41
SLIDE 41

Reducing decisions in clients to whether a
 link is present or not.

34
slide-42
SLIDE 42

Prefer explicit 
 state transitions over poking at your resources using PATCH.

35
slide-43
SLIDE 43

Translate domain concepts into web- appropriate ones.

36
slide-44
SLIDE 44 37 Aggregate Root / Repository Collection / Item Resources IDs URIs @Version ETags Last Modified Property Last Modified Header Relations Links
slide-45
SLIDE 45

RESTBucks

slide-46
SLIDE 46

RESTBucks

payment expected preparing cancelled ready completed 1 2 3 4 5 6
slide-47
SLIDE 47 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
slide-48
SLIDE 48 Method Resource type Action Step POST
  • rders
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
  • rder
Poll order state 5 GET receipt Access receipt DELETE receipt Conclude the order process 6
slide-49
SLIDE 49

Spring RESTBucks

41
slide-50
SLIDE 50 Web Service Repository
  • Orders
Spring Data Spring Data
 REST Payment Spring Data Manual
 implementation Manual
 implementation
slide-51
SLIDE 51

JacksonCustomizations Externalize tweaks to the general JSON design

43
slide-52
SLIDE 52

Spring Data REST
 for the CRUDdy parts.

44
slide-53
SLIDE 53

ResourceProcessor To conditionally sneak links into the default representation.

45
slide-54
SLIDE 54

Code

46 Spring RESTBucks - https://github.com/olivergierke/spring-restbucks
slide-55
SLIDE 55

Questions?

47