DDD & REST Domain Driven Apis for the Web

69
Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ DDD & REST Domain-Driven APIs for the web Oliver Gierke / olivergierke [email protected]

Transcript of DDD & REST Domain Driven Apis for the Web

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

DDD & RESTDomain-Driven APIs for the web

Oliver Gierke !/" olivergierke

# [email protected]

Background

3

4

Spring Data

Repositories &Aggregates

Spring HATEOAS

Hypermediafor Spring MVC

Spring Data REST

When designing REST APIs, what can we learn from DDD?“

5

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

6

8

http://www.infoq.com/minibooks/domain-driven-design-quickly

9

9

Implementing Domain-Driven

Design10

Value objects

11

Stringly typed code12

public class Customer {

private Long id;private String firstname, lastname, email;

…}

Stringly typed code13

public class SomeService {

public void createUser(String firstname, String lastname, String email) {

…}

}

14

public class Customer {

private Long id;private Firstname firstname;private Lastname lastname;private EmailAddress emailAddress;

…}

Value Objects are a PITA to build in

some languages.

15

Still, they’re worth it.

16

See „Power Use of Value Objects in DDD“ by Dan Bergh Johnsson.

Lombok — putting the spice back into Java.

17

18

@Valuepublic class Customer {

UUID id = UUID.randomUUID();Firstname firstname;Lastname lastname;EmailAddress email;

@Valuestatic class EmailAddress {String value;

}}

AutoValue

19

Project website on GitHub.

Entities & Repositories

20

21

Order

LineItem

Product

Invoice

CustomerPayment Address

Email

21

Order

LineItem

Product

Invoice

CustomerPayment Address

Email

Entity +Repository =

Aggregate

22

Aggregates.Scopes of consistency.

23

24

Order

LineItem

Product

Invoice

CustomerPayment Address

Email

Aggregates.The key things

to refer to.

25

26

Order

LineItem

Product

Invoice

CustomerPayment Address

Email

Don’t get trapped by datastore thinking.

27

28

Order

LineItem

Product

Invoice

CustomerPayment Address

Email

Bounded Context

29

Order

LineItem

Product

Invoice

CustomerPayment Address

31

Shipping

AccountingCatalog

Orders

UserRegistration

31

Shipping

AccountingCatalog

Orders

UserRegistration

Accounting Payment information

Billing Address

Shipping

Shipping address

Customer

31

Shipping

AccountingCatalog

Orders

UserRegistration

Accounting Payment information

Billing Address

Shipping

Shipping address

Customer

Product

Domain Events

32

33

Level 0: No events at all

33

Level 0: No events at all

Level 1: Explicit operations

If you’re calling two setters in a row, you’re

missing a concept.

34

35

Level 0: No events at all

Level 1: Explicit operations

Level 2: Some operations as events

Domain events asstate transitions.

36

Expose importantevents to interested

parties via feeds.

37

38

Level 0: No events at all

Level 1: Explicit operations

Level 2: Some operations as events

Level 3: Event Sourcing

REST

39

REST ≠ CRUD via HTTP

40

Representation design matters

41

Value objects need custom

serializers.

42

Aggregates IdentifiableReferable

Scope of consistency

43

Resources IdentifiableReferable

Scope of consistency

44

Serving data and navigation information

at the same time.

46

Trading domain knowledge with protocol

complexity in clients.

47

48

Amount of domain knowledge in the client

Amount of protocol knowledge in the client

Coupling to the server

Non-hypermediabased systems

Hypermediabased systems

API evolvability is key in a system of systems

49

50

GET /orders

{ „_links“ : { „cancel“ : { „href“ : … }, … „createdDate“ : …, „status“ : „Payment expected“ … }

51

GET /orders

{ „_links“ : { „cancel“ : { „href“ : … }, … „createdDate“ : …, „status“ : „Payment expected“ … }

52

“ Internationalizeall user facing text.

Aaaargh!

53

54

GET /orders

{ „_links“ : { „cancel“ : { „href“ : … }, … „createdDate“ : …, „status“ : „Payment expected“ … }

Reducing decisions in clients to whether a

link is present or not.

55

Prefer explicit state transitions over

poking at your resources using PATCH.

56

Translate domain concepts into web-appropriate ones.

57

58

Aggregate Root / Repository

Collection / Item Resources

IDs URIs

@Version ETags

Last Modified Property

Last Modified Header

Relations Links

RESTBuckspayment expected

preparing

cancelled

ready completed

1

2

3

4

5 6

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

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

62

Web

Service

Repository

-

Orders

Spring Data

Spring DataREST

Payment

Spring Data

Manualimplementation

Manualimplementation

Resources

67

Spring RESTBuckshttps://github.com/olivergierke/spring-restbucks

Benefits of Hypermedia APIshttp://olivergierke.de/2016/04/benefits-of-hypermedia/index.html

Questions?

68