Slide 1

Slide 1 text

CQRS meets modern Java Simon Martinelli @simas_ch martinelli.ch

Slide 2

Slide 2 text

About Me Web https://martinelli.ch E-Mail simon@martinelli.ch X/Twitter @simas_ch

Slide 3

Slide 3 text

Client/Server Communication Client Server

Slide 4

Slide 4 text

REST Controller DTO Mapper Entity Service Repository Mapping Waterfall

Slide 5

Slide 5 text

Example Project • https://github.com/simasch/ cqrs-meets-modern-java

Slide 6

Slide 6 text

Issue 1: Too Much • Over fetching • Loading data that is not needed • Not all data can be changed • Too much data is sent in the update request • Potential bugs if blindly mapped

Slide 7

Slide 7 text

Issue 2: Too Little • Under fetching • Results in multiple requests • Produces the N+1 select problem on the client-side • Under storing • Updates/inserts not within the same transaction

Slide 8

Slide 8 text

CQRS Source: https://cqrs.wordpress.com/wp-content/uploads/2010/11/cqrs_documents.pdf

Slide 9

Slide 9 text

Start Simple Command Query DB Client

Slide 10

Slide 10 text

Example CreateOrder FindOrders DB Client Command Handler Query Handler

Slide 11

Slide 11 text

Modern Java: Records • Provide a compact syntax for declaring classes as transparent holders for immutable data Preview: 14 Final: 16 record AddItemCommand(long orderId, long productId, int quantity) { }

Slide 12

Slide 12 text

Modern Java: Sealed Classes • Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them Preview: 15 Final: 17 abstract sealed class Shape permits Circle, Rectangle, Square { }

Slide 13

Slide 13 text

Modern Java: Switch Expression • Exhaustive: the switch expression must cover all cases • Preview in Java 12 and finalized in Java 14 String result = switch (color) { case RED -> "Red"; case GREEN -> "Green"; case BLUE -> "Blue"; }; Preview: 12 Final: 14

Slide 14

Slide 14 text

Modern Java: Pattern Matching • instanceof operator • switch expression and statement double area = switch (shape) { case Circle c -> Math.PI * c.radius * c.radius; case Rectangle r -> r.length * r.width; default -> throw new IllegalArgumentException(); }; Preview: 14/17 Final: 17/21

Slide 15

Slide 15 text

Modern Java: Record Patterns • Use to test whether a value is an instance of a record class type and, if it is, recursively perform pattern matching on its component values record Pair(int left, int right) {} if (p instanceof Pair(int left, int right)) { ... } Preview: 20 Final: 21

Slide 16

Slide 16 text

DB jOOQ Generator Java Classes jOOQ DSL What is jOOQ?

Slide 17

Slide 17 text

Why jOOQ? Commands • Usually, insert, update, or delete data in the database • Use jOOQ to generate minimal updates Queries • Often return nested structures like Order → Item • jOOQ provides the MULTISET value constructor

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Conclusion The separation of commands and queries • Improves the understandability • Helps to avoid over- and under- fetching • Can make mapping Entity <-> DTO obsolete • Enables an event-driven approach

Slide 20

Slide 20 text

Thank you! Web https://martinelli.ch E-Mail simon@martinelli.ch X/Twitter @simas_ch