vaughn vernon - qcon san francisco · 6 what about business? the business focus is on its domain is...

50
1 RESTful SOA or Domain-Driven Design– A Compromise? Vaughn Vernon vvernon@shiftmethod.com Copyright © 2008-2010 ShiftMETHOD. All rights reserved.

Upload: dinhthuy

Post on 18-Jan-2019

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

1

RESTful SOA orDomain-Driven Design–

A Compromise?

Vaughn [email protected]

Copyright © 2008-2010 ShiftMETHOD. All rights reserved.

Page 2: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

2

Overview

Potential pitfalls of SOA-only thinking.

Strategic Modeling patterns: DDD's “other half” and RESTful SOA

Tactical Modeling patterns: DDD building blocks and integration

Integrating Bounded Contexts: Making models work together

Page 3: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

3

Service-Oriented architecture

DatabaseClient

(app)

SERVICES

Page 4: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

4

SOa PitfallsService concerns and responsibilities overload▪ Services ▪ Transactions▪ Security ▪ Domain Logic▪ Data Access ▪ Translation▪ ▪Transformation Representation▪ Integration TANGLE

DISTORTION

Page 5: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

5

Ask-Decision-Set

Tell-Don't-Ask

Page 6: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

6

What about business?

The business focus is on its domainIs your business domain that of Service-Oriented Architecture?

We want to do Domain-Driven Design, not SOA-Driven Design

Page 7: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

7

RESTful SOA with DDD

Database

RESTful Model

Model based on the language of the business domain

Services based on resources that represent Model state/behavior

Client

(app)

Page 8: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

8

DDD Strategic Design

Strategic Modeling is not really the “other half” of DDD

Strategic Modeling is the most widely applicable part of DDD

“Say again!”

S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !S u r p r I s e !

Page 9: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

9

DDD Strategic Modeling

DDD is not about Entities, Value Objects, Repositories, etc.

DDD is about modeling the business domain in its own language

Bounded

Context

Context

Map

Open

Host

Service

Published

Language

Anti-

corruption

Layer

Important patterns for basic DDD and integration (BC and CM)

Page 10: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

10

Bounded ContextAgile Project Mgmt

Context

Identity and AccessMgmt Context

CollaborationContext

The delimited applicability of a particular model. Gives team members a clear and shared understanding of what has to be consistent and what can develop independently.

Page 11: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

11

Identity & Access Context

Page 12: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

12

Collaboration Context

Page 13: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

13

Agile PM Context

Page 14: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

15

Context Map

All three RESTful

Page 15: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

16

Open Host Service

Define a protocol that gives access to your subsystem as a setof services. Open the protocol so that all who need to integratewith you can use it.

RESTfulClient

(app)

HTTP

Resources as services

Page 16: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

17

Published Language

Use a well-documented shared language that can express the necessary domain information as a common medium ofcommunication, translating as necessary...

HTTP Content NegotiationAccept: application/xml, application/json, ...

@Produces({"application/xml", "application/json"})

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">XML, XML Schema, Xpath, Atom, JSON

//emailAddress Model representationby choice... use case driven

Page 17: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

18

Anticorruption LayerRESTfulClient (app)

ADomain Model (BC)

Domain Service

T

AcL

Create an isolating layer to provide clients with functionality interms of their own domain model.

Value Object

GET

XML

Page 18: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

19

DDD Tactical Design

Building block patterns typically used in a DDD project

Support integration patterns of Strategic Design

Layered

Architecture

Domain

Event

Domain

Service

Aggregate

(uses Entity)

Value

Object

Page 19: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

20

Layered ArchitectureUser Interface Layer

Application Layer

Domain Layer

Infrastructure Layer

Isolated core model

OHS RESTful Services;(and Presentation)Security, transactions;Thin task coordinator

Technical capabilities;persistence, messaging...

Page 20: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

22

Aggregate

Object composition cluster with consistency boundaries

Publishes Domain Events, which indicate significant occurrences

Page 21: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

23

Value Object

Describes something about an object in the domain model

Supports minimalism; concepts translated from other contexts

Page 22: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

24

Domain Event History

Publish notifications to Views when updates occur in the Model

Zero, one, or more updates may occur and View cannot assume

Page 23: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

25

A significant occurrence in the model; defined in past tense

Published largely by Aggregates as their states transition

Domain Events Today

Page 24: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

26

Domain Service

When a significant process or transformation in the domain is not a natural responsibility of an Entity or Value Object, add an oper-ation to the model as a standalone interface declared as a Service.

Page 25: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

27

DDD + RESTful

Page 26: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

28

Collab AcL + RESTful

Page 27: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

30

UserInRoleAdapterpublic Collaborator toCollaborator(...) {

ClientRequest request = this.buildRequest(aTenant, anIdentity, aRoleName);

ClientResponse<String> response = request.get(String.class);

if (response.getStatus() == 200) { collaborator = this.getTranslator().toCollaboratorFromDocument( response.getEntity(), aCollaboratorClass); } ...

GET https://iam/tenants/{tenantId}/users/{username}/inRole/{role}

Page 28: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

31

GET .../inRole/{role}@Path("/tenants/{tenantId}/users")public class UserResource {

@GET @Path("{username}/inRole/{role}") @Produces({ "application/xml" }) public Response getUserInRole( @PathParam("tenantId") String aTenantId, @PathParam("username") String aUsername, @PathParam("role") String aRoleName) { // use Application Layer to interact with Domain Layer... Response response = ...; return response; }}

Page 29: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

32

HTTP GET User-In-RoleGET https://iam/tenants/{tenantId}/users/{username}/inRole/{role}Accept: application/xml

HTTP/1.1 200 OKContent-Type: application/xml...<userInRole> <tenantId>CCA701C2-6490-41B9-B4DA-DB785107C8C8</tenantId> <username>jdoe</username> <firstName>John</firstName> <lastName>Doe</lastName> <emailAddress>[email protected]</emailAddress> <role>Moderator</role></userInRole>

Page 30: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

33

CollaboratorTranslatorpublic Collaborator toCollaboratorFromDocument(...) {

Document doc = this.buildDocument(aUserInRoleDocument);

XPath xpath = XpathFactory.newInstance().newXPath();

String username = xpath.evaluate("//username", doc, ...); String firstName = xpath.evaluate("//firstName", doc, ...); String lastName = xpath.evaluate("//lastName", doc, ...); String emailAddr = xpath.evaluate("//emailAddress", doc, ...);

Collaborator collaborator = this.newCollaborator( username, firstName, lastName, emailAddr, aCollabType);

return collaborator;}

Page 31: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

34

“Simple” Event:Zoe Doe

Gets Married

Service Quality: Eventually names must reflect I&A Context

Service Capability: I&A self service allows Zoe to change name

Page 32: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

35

Problem?

Collaborator Values are created new for each request (by choice)

Changes to users in the I&A Context are not reflected in Collab

Page 33: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

36

Solution (HM not MOM)

Use shared Value Objects that are eventually consistent

Publish event-based notifications out of each Bounded Context

<notifications tenantId="C888F4B2-71F0-48D2-B4A3-0A44BABFA2E1" id="55F03810-09FF-4925-B9FD-7935CC91F31D" archived="false"> <notification type="personNameChanged" occurredOn="2010-08-19T16:38:06-06:00" id="A9D234C6-96EF-4B9A-BAC9-F720A2115B3B"> <personNameChanged> <tenantId>C888F4B2-71F0-48D2-B4A3-0A44BABFA2E1</tenantId> <username>zoe</username> <firstName>Zoe</firstName> <lastName>Jones-Doe</lastName> </personNameChanged> </notification> ...</notifications>

Page 34: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

37

I&A Domain Events

Page 35: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

38

I&A Event TypesGroupGroupAddedGroupGroupRemovedGroupUserAddedGroupUserRemovedPersonContactInformationChangedPersonNameChangedTenantActivatedTenantDeactivatedTenantProvisionedUserEnablementChangedUserPasswordChangedUserRegistered

RoleGroupAssignedRoleGroupUnassignedRoleUserAssignedRoleUserUnassigned

Page 36: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

39

Agile PM Sychronization

Out of band (max-age based)

Updates cache

Page 37: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

40

Notification Logs

Series of logs from the beginning of time; NOT in Core Domain

Current working log and any number of archived logs

Page 38: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

41

Notification ReferencingCURRENT

NotificationLog

NOT ARCHIVED

NotificationLog

ARCHIVED

NotificationLog

ARCHIVED

OLDESTNotification

LogARCHIVED

previous previous previous

next next next

Navigate through previous logs to find latest applied notification

Apply all newer notifications, navigating to current using next

Page 39: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

42

Feeds

Can produce Atom-based notifications from NotificationLogs

Can produce custom notifications from NotificationLogs

application/atom+xml

<notifications tenantId="..." id="..." archived="false"> ...</notifications>

application/xml

Page 40: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

43

NotificationResource (1)@Path("/tenants/{tenantId}/notifications")public class NotificationResource { @GET @Produces({ "application/xml" }) public Response getCurrentNotificationLog( @PathParam("tenantId") String aTenantId, @Context UriInfo aUriInfo) {

NotificationLog currentNotificationLog = this.getNotificationService() .getCurrentNotificationLog(new TenantId(aTenantId));

// ... return tempResponse;}

Custom, current log

RESTful notification service as a resource (OHS producing PL)

Page 41: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

44

NotificationResource (2)@Path("/tenants/{tenantId}/notifications")public class NotificationResource { @GET @Path("{notificationId}") @Produces({ "application/xml" }) public Response getNotificationLog( @PathParam("tenantId") String aTenantId, @PathParam("notificationId") String aNotificationId, @Context UriInfo aUriInfo) {

NotificationLog notificationLog = this.getNotificationService() .getNotificationLog( new TenantId(aTenantId), aNotificationId); // ... return tempResponse;}

Specific log, next/previous

May filter using parameter

Page 42: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

45

HTTP GET NotificationsGET https://iam/tenants/{tenantId}/notificationsAccept: application/xml

HTTP/1.1 200 OKContent-Type: application/xmlLink: <https://iam/tenants/C888.../notifications>;

rel=selfLink: <https://iam/tenants/C888.../notifications/55F03810-...>;

rel=previous...<notifications ...> <notification ...> </notification> ...</notifications>

Page 43: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

46

HTTP GETting It AllHTTP/1.1 200 OKContent-Type: application/xmlLink: <https://iam/tenants/C888.../notifications/72G31419-...>;

rel=selfLink: <https://iam/tenants/C888.../notifications/46A0C283-...>;

rel=nextLink: <https://iam/tenants/C888.../notifications/55F03810-...>;

rel=previousCache-Control: max-age=3600...<notifications ...> <notification id="A9D234C6-96EF-4B9A-BAC9-F720A2115B3B"> </notification> ...</notifications>

Track latest locally

Page 44: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

47

Incoming NotificationTracker

private IncomingNotificationTracker tracker;//...private void applyNotification(... aNotification) { // ... this.getTracker() .recordMostRecent( aNotification.getId(), aNotification.getType(), aNotification.getOccurredOn());}

Page 45: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

48

<notification type="personNameChanged"

occurredOn="2010-08-19T16:38:06-06:00" id="A9D234C6-96EF-4B9A-BAC9-F720A2115B3B"> <personNameChanged> <tenantId>C888F4B2-71F0-48D2-B4A3-0A44BABFA2E1</tenantId> <username>zoe</username> <firstName>Zoe</firstName> <lastName>Jones-Doe</lastName> </personNameChanged></notification>

Notification Element

Child reflects Domain Event from originating Bounded Context

Outer notification element contains common information

Descending date/time order

Page 46: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

49

Agile PM Shared ValuesProductOwner

TeamMember

Anticorruption Layer (notifications adapter) updates

MemberService (Domain) caches single instance in database

Page 47: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

52

Zoe's New NameProductOwner productOwner = DomainRegistry .getMemberService() .getProductOwner(tenant, "zoe"); assertNotNull(productOwner);

assertEquals(productOwner.getName(), "Zoe Jones-Doe");

assertEquals(productOwner.getEmailAddress(), "[email protected]");

Page 48: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

53

Autonomous Services

Notifications allow seeding Model from “beginning of time”

Notifications allow recovery from any service down-time

Notifications from Domain Events of all three Bounded Contexts

Page 49: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

54

Summary

For Business: combine RESTful SOA with Domain-Driven Design

Strategic Modeling patterns: DDD's “better half” for RESTful SOA

Tactical Modeling patterns: Domain Events key building block

Integrating Bounded Contexts: RESTful + Hypermedia

Page 50: Vaughn Vernon - QCon San Francisco · 6 What about business? The business focus is on its domain Is your business domain that of Service-Oriented Architecture? We want to do Domain-Driven

55

ContactVaughn Vernon

[email protected]

Copyright © 2008-2010 ShiftMETHOD. All rights reserved.

Questions?Please fill out evaluation forms!