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
“ What does it take to bridge the worlds of DDD & REST?
7
7
Value objects 8
Value Objects are a PITA to build in some languages. 9
Still, they’re worth it. See „Power Use of V alue Objects in DDD“ by Dan Bergh Johnsson. 10
Lombok — putting the spice back into Java. 11
Key opponents: Mapping libraries that need to (de)serialize them. 12
Entities & Repositories 13
14
Email Customer Payment Address 14
Email Customer Payment Address Invoice 14
Email Customer Payment Address Order Invoice LineItem 14
Email Customer Payment Address Product Order Invoice LineItem 14
Email Customer Payment Address Product Order Invoice LineItem 14
Entity + Repository = Aggregate 15
Aggregates form nice representation boundaries. 16
Aggregates become the key things to refer to. 17
Don’t get trapped by datastore thinking. 18
Try to avoid bi-directional relationships. 19
Domain Events 20
Level 0: No events at all 21
Level 1: Explicit operations Level 0: No events at all 21
If you’re calling two setters in a row, you’re missing a concept. 22
Level 2: Some operations as events Level 1: Explicit operations Level 0: No events at all 23
Domain events as state transitions. 24
Expose important events to interested parties via feeds. 25
Level 3: Event Sourcing Level 2: Some operations as events Level 1: Explicit operations Level 0: No events at all 26
REST 27
Representation design matters 28
Aggregates Identifiable Referable Scope of consistency 29
Resources Identifiable Referable Scope of consistency 30
Hypermedia 31
Serving data and navigation information at the same time. 32
Trading domain knowledge with protocol complexity in clients. 33
Reducing decisions in clients to whether a link is present or not. 34
Prefer explicit state transitions over poking at your resources using PATCH. 35
Translate domain concepts into web- appropriate ones. 36
Aggregate Root / Collection / Item Repository Resources Relations Links IDs URIs @Version ETags Last Modified Last Modified Property Header 37
RESTBucks
5 6 preparing ready completed 2 4 1 RESTBucks payment expected 3 cancelled
Method URI Action Step POST /orders Create new order 1 Update the order POST/PATCH /orders/{id} 2 (only if "payment expected") Cancel order DELETE /orders/{id} 3 (only if "payment expected") Pay order PUT /orders/{id}/payment 4 (only if "payment expected") 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
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
Spring RESTBucks 41
Orders Payment Spring Data Manual Web REST implementation Manual - Service implementation Repository Spring Data Spring Data
JacksonCustomizations Externalize tweaks to the general JSON design 43
Spring Data REST for the CRUDdy parts. 44
ResourceProcessor To conditionally sneak links into the default representation. 45
Code Spring RESTBucks - https://github.com/olivergierke/spring-restbucks 46
Questions? 47
Recommend
More recommend