Slide 1

Slide 1 text

Starring With vs Special Appearances by

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Java old! C# ABAP PHP Python

Slide 5

Slide 5 text

Kolleg:in gesucht (Deutschlandweit)

Slide 6

Slide 6 text

Buy it on Amazon.com: https://hschwentner.io

Slide 7

Slide 7 text

Photo: Henning Schwentner Let me tell you a story

Slide 8

Slide 8 text

@hschwentner Example: A Banking Domain bank account customer teller computer transaction

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

to deposit to withdraw bank account money amount IBAN to pay interest

Slide 11

Slide 11 text

@hschwentner Example: A Banking Domain computer Banking 3000

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

@hschwentner Software != End in itself

Slide 14

Slide 14 text

@hschwentner ! ! " Software is build Tech people business people # " for for by

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Software Domain

Slide 17

Slide 17 text

Foto: Rosie the Riveter/Wikimedia/CC-PD-Mark

Slide 18

Slide 18 text

@hschwentner Programming is Model Building

Slide 19

Slide 19 text

@hschwentner Rules of thumb:

Slide 20

Slide 20 text

Objects from real world turn into objects in software

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Actions from real world turn into operations in software

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

=> Object-orientation

Slide 25

Slide 25 text

Object-Oriented Programming Ole-Johan Dahl Photo: Stein Krogdahl, University of Oslo Kristen Nygaard Photo: Jorge Stolfi/Wikipedia !

Slide 26

Slide 26 text

Photo: P. Campbell/Wikipedia Alan Kay Everything is an object "

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Foto: P. Campbell/Wikipedia James Gosling Everything is an object #

Slide 29

Slide 29 text

Anders Hejlsberg Everything is an object

Slide 30

Slide 30 text

Are those objects?

Slide 31

Slide 31 text

@hschwentner In the real world not everything is an object

Slide 32

Slide 32 text

@hschwentner Domain-Driven Design Eric Evans " Entity Value Object Aggregate Service Factory Repository

Slide 33

Slide 33 text

@hschwentner Entity vs. Value

Slide 34

Slide 34 text

Foto: P. Campbell/Wikipedia James Gosling Everything is an object int short long byte char float double boolean … except:

Slide 35

Slide 35 text

Anders Hejlsberg Everything is an object …except structs

Slide 36

Slide 36 text

Values from real world turn into values in software

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

@hschwentner What is an object/entity?

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Identity

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Foto: Th.omas G.Graf/Wikipedia oto: H. Schwentner

Slide 44

Slide 44 text

Grafik: Pixabay

Slide 45

Slide 45 text

Foto: Bernd Schwabe in Hannover/Wikipedia

Slide 46

Slide 46 text

@hschwentner Object: *Identity *Lifecycle *(mutable) state

Slide 47

Slide 47 text

@hschwentner What is a value?

Slide 48

Slide 48 text

Grafik: Public Domain/Pixabay

Slide 49

Slide 49 text

Foto: A. Markiewicz

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

@hschwentner

Slide 52

Slide 52 text

@hschwentner Value: *No identity *immutable

Slide 53

Slide 53 text

@hschwentner Example

Slide 54

Slide 54 text

% & '

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

'

Slide 57

Slide 57 text

' '

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

% & '

Slide 60

Slide 60 text

' ' & ' & '

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Foto: Bibi Saint-Pol/Wikipedia

Slide 63

Slide 63 text

@hschwentner Objects in Languages like Java or C#

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Foto: Tiia Monto/Wikipedia

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

@hschwentner Identity of an Object

Slide 77

Slide 77 text

3

Slide 78

Slide 78 text

3 3

Slide 79

Slide 79 text

3

Slide 80

Slide 80 text

@hschwentner Values in Languages like Java or C#

Slide 81

Slide 81 text

built-in value types primitive types simple types

Slide 82

Slide 82 text

int boolean/bool double char

Slide 83

Slide 83 text

User-defined value types

Slide 84

Slide 84 text

LocalDate/DateTime String

Slide 85

Slide 85 text

object types that behave like value types value-based class (class with) value equality

Slide 86

Slide 86 text

@hschwentner Values in Java

Slide 87

Slide 87 text

int boolean double char

Slide 88

Slide 88 text

unsigned complex

Slide 89

Slide 89 text

iban postcode amount

Slide 90

Slide 90 text

Foto: Public Domain/Pixabay

Slide 91

Slide 91 text

Grafik: Gemeinfrei/Wikipedia

Slide 92

Slide 92 text

value-based class

Slide 93

Slide 93 text

@hschwentner Only final fields

Slide 94

Slide 94 text

@hschwentner No state-changing methods

Slide 95

Slide 95 text

@hschwentner equals() does not depend on identity

Slide 96

Slide 96 text

value-based class <= 13

Slide 97

Slide 97 text

LocalDate String

Slide 98

Slide 98 text

record >= 16

Slide 99

Slide 99 text

record: all fields final field-based equal() [and hashCode()] getters for all fields >= 16

Slide 100

Slide 100 text

value type >= ??

Slide 101

Slide 101 text

@hschwentner Values in C#

Slide 102

Slide 102 text

int bool double char

Slide 103

Slide 103 text

struct <= 7.1

Slide 104

Slide 104 text

@hschwentner Only readonly fields

Slide 105

Slide 105 text

@hschwentner No state-changing methods

Slide 106

Slide 106 text

readonly struct >= 7.2

Slide 107

Slide 107 text

@hschwentner equals() ! == "

Slide 108

Slide 108 text

record <= 9

Slide 109

Slide 109 text

@hschwentner Value equality

Slide 110

Slide 110 text

@hschwentner equals() ! == !

Slide 111

Slide 111 text

@hschwentner Beware: Can be mutable

Slide 112

Slide 112 text

@hschwentner On the heap

Slide 113

Slide 113 text

record struct <= 10

Slide 114

Slide 114 text

@hschwentner Beware: Is mutable

Slide 115

Slide 115 text

readonly record struct <= 10 !

Slide 116

Slide 116 text

@hschwentner Why aren’t int and string enough?

Slide 117

Slide 117 text

@hschwentner Objects/Entities in Java — Example

Slide 118

Slide 118 text

public class Account { }

Slide 119

Slide 119 text

import org.jmolecules.ddd.annotation.Entity; @Entity public class Account { }

Slide 120

Slide 120 text

@Entity public class Account { private int _balance; public int getBalance() { return _balance; } public void setBalance(int balance) { _balance = balance; } } ✘ Bad: The account balance can be set to any value

Slide 121

Slide 121 text

@Entity public class Account { private int _balance; public int balance() { return _balance; } public void deposit(int amount) { _balance += amount; } public void withdraw(int amount) { _balance -= amount; } Better: Operations with domain- specific behavior and names

Slide 122

Slide 122 text

@Entity public class Account { private int _balance; public int balance() { return _balance; } public void deposit(int amount) { _balance += amount; } public void withdraw(int amount) { if (amount > balance()) { throw new IllegalArgumentException("Amount too big"); Even better: Preconditions can be checked

Slide 123

Slide 123 text

@Entity public class Account { // ... public void withdraw(int amount) { assert amount <= balance(); _balance -= amount; } } Assertion using keyword assert

Slide 124

Slide 124 text

GREAT, BUT… @Entity public class Account { // ... public void withdraw(int amount) { assert amount <= balance(); _balance -= amount; } } Can I withdraw a negative amount? In EUR or GBP or…?

Slide 125

Slide 125 text

@hschwentner Values in Java — Example

Slide 126

Slide 126 text

public class Amount { }

Slide 127

Slide 127 text

import org.jmolecules.ddd.annotation.ValueObject; @ValueObject public class Amount { }

Slide 128

Slide 128 text

@ValueObject public class Amount { private int _amount; private Currency _currency; }

Slide 129

Slide 129 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; }

Slide 130

Slide 130 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } }

Slide 131

Slide 131 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; private Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public static Amount of(int amount, Currency currency) { return new Amount(amount, currency); } }

Slide 132

Slide 132 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } @Override public boolean equals(Object other) { return _amount == ((Amount) other)._amount && _currency.equals(((Amount) other)._currency); }

Slide 133

Slide 133 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } @Override public boolean equals(Object other) { return _amount == ((Amount) other)._amount && _currency.equals(((Amount) other)._currency); }

Slide 134

Slide 134 text

@ValueObject public record Amount(int amount, Currency currency) {}

Slide 135

Slide 135 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount otherAmount) { return Amount.of(_amount + otherAmount._amount, _currency); } } Der neue Typ hat richtiges fachliches Verhalten

Slide 136

Slide 136 text

@ValueObject public class Amount { private final int _amount; private final Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount otherAmount) { assert hasSameCurrency(otherAmount); return Amount.of(_amount + otherAmount._amount, _currency); } … und Verträge, die vor falschen Währungen schützen

Slide 137

Slide 137 text

@ValueObject public record Amount(int amount, Currency currency) { public Amount add(Amount otherAmount) { assert hasSameCurrency(otherAmount); return new Amount(amount + otherAmount.amount(), currency); } public boolean hasSameCurrency(Amount otherAmount) { return otherAmount.currency() == currency; } }

Slide 138

Slide 138 text

@hschwentner Values in C# — Example

Slide 139

Slide 139 text

public class Amount { }

Slide 140

Slide 140 text

using NMolecules.DDD; [ValueObject] public class Amount { }

Slide 141

Slide 141 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; }

Slide 142

Slide 142 text

[ValueObject] public class Amount { private int _amount; private Currency _currency; }

Slide 143

Slide 143 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } }

Slide 144

Slide 144 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; // ... public override bool Equals(object other) { return other is Amount && _amount == ((Amount) other)._amount && _currency.Equals(((Amount) other)._currency); } }

Slide 145

Slide 145 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; // ... public override bool Equals(object other) => other is Amount && _amount == ((Amount) other)._amount && _currency.Equals(((Amount) other)._currency); }

Slide 146

Slide 146 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; // ... public override bool Equals(object other) => other is Amount && _amount == ((Amount) other)._amount && _currency.Equals(((Amount) other)._currency); // GetHashCode() }

Slide 147

Slide 147 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; // ... public static bool operator ==(Amount a, Amount b) => a.Equals(b); }

Slide 148

Slide 148 text

[ValueObject] public class Amount { private readonly int _amount; private readonly Currency _currency; // ... public static bool operator ==(Amount a, SignDate b) => a.Equals(b); public static bool operator !=(Amount a, SignDate b) => !a.Equals(b); }

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

3

Slide 151

Slide 151 text

3

Slide 152

Slide 152 text

[ValueObject] public struct Amount { private readonly int _amount; private readonly Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } }

Slide 153

Slide 153 text

[ValueObject] public struct Amount { private readonly int _amount; private readonly Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } // Equals() does not have to be overridden }

Slide 154

Slide 154 text

[ValueObject] public struct Amount { private readonly int _amount; private readonly Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } // but the operators have to be overridden }

Slide 155

Slide 155 text

@hschwentner equals() ! == "

Slide 156

Slide 156 text

[ValueObject] public record Amount(int amount, Currency currency);

Slide 157

Slide 157 text

[ValueObject] public record Amount(int amount, Currency currency); // automatically readonly // Equals(), GetHashCode(), ToString(), operators // do not have to be overloaded and work as expected

Slide 158

Slide 158 text

[ValueObject] public record struct Amount(int amount, Currency currency);

Slide 159

Slide 159 text

[ValueObject] public readonly record struct Amount(int amount, Currency currency);

Slide 160

Slide 160 text

@hschwentner Values in Java — Example continued

Slide 161

Slide 161 text

But

Slide 162

Slide 162 text

3 3

Slide 163

Slide 163 text

@hschwentner equals() ! == "

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

3

Slide 166

Slide 166 text

by value

Slide 167

Slide 167 text

by reference

Slide 168

Slide 168 text

3

Slide 169

Slide 169 text

No content

Slide 170

Slide 170 text

Memory*2

Slide 171

Slide 171 text

3

Slide 172

Slide 172 text

3

Slide 173

Slide 173 text

No content

Slide 174

Slide 174 text

No content

Slide 175

Slide 175 text

int[]

Slide 176

Slide 176 text

3 5 10 5 0

Slide 177

Slide 177 text

Object[]

Slide 178

Slide 178 text

3 5 10 5 0

Slide 179

Slide 179 text

3 10 5 0

Slide 180

Slide 180 text

@hschwentner A Concrete Example

Slide 181

Slide 181 text

@hschwentner LeasingNinja https://leasingninja.io

Slide 182

Slide 182 text

@hschwentner Conclusion

Slide 183

Slide 183 text

@hschwentner Key take aways: *value/object are different *only records yet * value types are still to come (*domain model and performance)

Slide 184

Slide 184 text

Bild: Gemeinfrei

Slide 185

Slide 185 text

@hschwentner Further Reading

Slide 186

Slide 186 text

project valhalla

Slide 187

Slide 187 text

https://domainstorytelling.org

Slide 188

Slide 188 text

Workshop Domain-Driven Design concrete wps.de/ddd

Slide 189

Slide 189 text

No content

Slide 190

Slide 190 text

No content

Slide 191

Slide 191 text

No content

Slide 192

Slide 192 text

No content

Slide 193

Slide 193 text

Bibliography Beck, Kent et al. Manifesto for Agile Software Development. 2001. Evans, Eric. Domain-Driven Design: Tackling Complexity in the Heart of Software. Boston: Addison-Wesley, 2004. Hofer, Stefan and Henning Schwentner. Domain Storytelling: a Collaborative, Visual, and Agile Way to Develop Domain- Driven Software. Boston: Addison-Wesley, 2022.

Slide 194

Slide 194 text

No content

Slide 195

Slide 195 text

Henning Schwentner ⌂ https://hschwentner.io @hschwentner [email protected] Kolleg:in gesucht (Deutschlandweit)