Slide 1

Slide 1 text

Michael Plöd, @bitboss Implementing with the Spring ecosystem DDD

Slide 2

Slide 2 text

Disclaimer Michael Plöd - innoQ
 @bitboss Most of these ideas do not come from me personally. I have to thank Eric Evans and Vaughn Vernon for all the inspiration and ideas. If you haven’t: go out and get their amazing books!

Slide 3

Slide 3 text

Feel free to replace developer by architect, team lead, team member or domain expert. Nothing in this talk is a silver bullet.

Slide 4

Slide 4 text

Bounded Context Spring Spring Core Spring Boot Spring Data Spring Cloud Spring MVC Spring Events Good OO 
 practice Aggregates Entities Value Objects Factories Services Repositories TOPICS I will talk about today

Slide 5

Slide 5 text

Model your Microservices along business capabilities

Slide 6

Slide 6 text

Bounded Context Context Map Shared Kernel Customer / 
 Supplier Conformist Anticorruption
 Layer Separate W ays Open / Host 
 Service Published 
 Language STRATEGIC DESIGN helps us with regards to business capabilities

Slide 7

Slide 7 text

Bounded Context Every sophisticated business (sub-) domain consists of a bunch of Bounded Contexts Each Bounded Context contains a domain model, don’t nest Bounded Contexts The Bounded Context is also a boundary for the meaning of a given model

Slide 8

Slide 8 text

Example - You at spring.io Reservations Event
 Management Badges spring.io
 Visitor Name Payment Details Address Company Session Registrations Lunch Preferences Name Job Description Twitter Handle

Slide 9

Slide 9 text

A Bounded Context is the 
 BOUNDARY 
 for the meaning of a model

Slide 10

Slide 10 text

Don’t Repeat Yourself INSIDE of a Bounded Context

Slide 11

Slide 11 text

Repeat Yourself BETWEEN several Bounded Contexts

Slide 12

Slide 12 text

Microservice architectures should be evolvable

Slide 13

Slide 13 text

Aggregates (Internal) 
 Building Blocks Entities Value Objects Factories Services Repositories TACTICAL DESIGN helps us with regards to evolvability of microservices

Slide 14

Slide 14 text

Building
 Blocks Entities Entities represent the core business objects of a bounded context’s model Each Entity has a constant identity Each Entity has its own lifecycle Customer Credit Application Shipment

Slide 15

Slide 15 text

Building
 Blocks Value Objects Value Objects derive their identity from their values Value Objects do not have their own lifecycle, they inherit it from Entities that are referencing them You should always consider value objects for your domain model Color Monetary Amount Customer

Slide 16

Slide 16 text

Building
 Blocks Is „Customer“ an Entity or 
 a Value Object If an object can be considered an Entity or a Value Object always depends on the (Bounded Context) it resides in. Customer Example: A customer is an entity in a CRM-like microservice but not in a microservice that prints badges, there we are just interested in name, job description and Twitter handle

Slide 17

Slide 17 text

Your DDD Entity is NOT your JPA Entity

Slide 18

Slide 18 text

Building
 Blocks Aggregates Do not underestimate the power of the Aggregate

Slide 19

Slide 19 text

Building
 Blocks Aggregates SelfDisclosure Address RedemptionDetail Loan Customer LoanApplicationForm Aggregates group Entities. The Root Entity is the lead in terms of access to the object graph and lifecycle.

Slide 20

Slide 20 text

Best Practices for Aggregates Hints Small Prefer small aggregates that usually only contain an Entity and some Value Objects. Consistency Boundaries Take a look which parts of your model must be updated in an atomically consistent manner One TX per Aggregate Aggregates should be updated in separate transations which leads to eventual consistency Reference by Identity Do not implement direct references to other Root Entities. Prefer referencing to Identity Value Objects

Slide 21

Slide 21 text

Implementing Aggregates

Slide 22

Slide 22 text

Building
 Blocks Aggregate Example Battery of an electric car Battery Cells Status Charger Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell General Information

Slide 23

Slide 23 text

Building
 Blocks Aggregate Example Battery of an electric car Think about good a good old OO practice: information hiding Personal opionion: In the advent of POJOs we have unlearned information hiding A blind „Genereate Getters and Setters“ in your IDE is your enemy Battery Cells Status Charger Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell General Information

Slide 24

Slide 24 text

Building
 Blocks Aggregate Example Battery of an electric car Do we need an external access to the cells? Stuff we want to do from the outside: - Start charging - Get current charge level - Get information about the charging Battery Cells Status Charger Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell Cell General Information

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Hints Visibility Use the power of your programming language, private Attributes with public getters and setters are no information hiding. Yes, you can have classes with a package visibility level. Packages Place each aggregate in its own package and work with package level visibility References I prefer Aggregates that do not reference themselves, they are hooked together with a few shared value objects which leads to more decoupling

Slide 27

Slide 27 text

Applicant Address Account
 Balance Scoring
 Result Scoring Calculations Score Color AgencyResult KoCriteria Warning
 Message Financial
 Situation Incomings Outgoings PersonId Application
 Number

Slide 28

Slide 28 text

Root Entity directly Explicit Aggregate Class Implementation Options Aggregates

Slide 29

Slide 29 text

Keep your Aggregates Spring free

Slide 30

Slide 30 text

Adapter Adapter Adapter Adapter Adapter Adapter Adapter Adapter Web Mobile Cloud Messaging Database Document GraphDB Messaging Core
 
 
 
 Domain

Slide 31

Slide 31 text

The hexagonal architecture is not your only option and is not suitable for everything „The domain should be the heart of a system“ Hexagonal ➡ For bounded contexts with a lot of logic and calculations ➡ Complex ➡ Many mappings ➡ Good fit for the DDD internal building blocks CRUD ➡ For bounded contexts that just store and read data ➡ Keep it simple ➡ Spring Data Rest may be your friend Query Driven ➡ For bounded contexts that perform complex queries ➡ Mostly based on a read model ➡ Sometimes the „Q“ in distributed CQRS

Slide 32

Slide 32 text

Communication between Bounded Contexts

Slide 33

Slide 33 text

Orchestration vs Choreography Building Microservices by Sam Newman, O'Reilly Event Stream Choreography Orchestration

Slide 34

Slide 34 text

Choreography Credit
 Application Scoring Credit Decision Customer Credit
 Application
 Submitted Event

Slide 35

Slide 35 text

Credit
 Application
 Submitted Event We can use Domain Events for the communication between Bounded Contexts. This leads us to Event-driven Applications

Slide 36

Slide 36 text

! Model information about activity in the domain as a series of discrete events.

Slide 37

Slide 37 text

An event is something 
 that happened in the past t now Event Event Event Event Event

Slide 38

Slide 38 text

Options for Event Payload Options Full Payload The event carries complete Entitiy-Graphs or Aggregates Mix The event contains data that is usually of interest to many other contexts. For special purposes there is also a URL to a RESTful HTTP Ressource Empty The event is empty or contains only minimal data and is being used to trigger pulling from a feed (eg. Atom) REST URL The event only carries a URL to a RESTful HTTP Ressource

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Spring Cloud Stream Broker Very often the de-facto choice for many distributed systems. Typical choices nowadays would be Apache Kafka or RabbitMQ. High degree of decoupling. Internal 
 Event Bus Only JVM internal with synchronous and / or asynchronous options. Highly recommended for building decoupled monoliths. HTTP Feeds REST + Events = Feeds
 
 Often ignored option in which you won’t need a dedicated broker. Spring MVC + Quartz 
 + Rome Spring Application Events

Slide 41

Slide 41 text

Very useful!

Slide 42

Slide 42 text

I’m writing a book

Slide 43

Slide 43 text

Implementing with the Spring ecosystem Michael Plöd
 @bitboss https://github.com/mploed
 https://speakerdeck.com/mploed
 https://leanpub.com/ddd-by-example DDD