Repository navigation
Expand file tree
/
Copy pathExpEventSourcing.java
More file actions
302 lines (244 loc) · 14.4 KB
/
Copy pathExpEventSourcing.java
File metadata and controls
302 lines (244 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.time.Instant;
import java.util.*;
/**
* Event Sourcing con Java puro.
*
* Concepto: el estado se deriva de una secuencia de eventos inmutables.
* No se guarda "saldo=150", se guarda:
* [CuentaCreada, DepositoRealizado(200), RetiroRealizado(50)] → saldo=150
*
* Componentes:
* - Event: interfaz sellada de eventos de dominio
* - EventStore: log append-only de eventos
* - CuentaBancaria: aggregate que se reconstruye desde eventos
* - Proyector: construye una vista de lectura a partir de eventos
*/
public class ExpEventSourcing {
// ═══════════════════════════════════════════════════════════════
// EVENTOS DE DOMINIO — inmutables, en pasado
// ═══════════════════════════════════════════════════════════════
sealed interface Evento permits
CuentaCreada, DepositoRealizado, RetiroRealizado, CuentaBloqueada {
String cuentaId();
Instant ocurridoEn();
}
record CuentaCreada(String cuentaId, String titular, Instant ocurridoEn) implements Evento {}
record DepositoRealizado(String cuentaId, double cantidad, String concepto, Instant ocurridoEn)
implements Evento {}
record RetiroRealizado(String cuentaId, double cantidad, String concepto, Instant ocurridoEn)
implements Evento {}
record CuentaBloqueada(String cuentaId, String motivo, Instant ocurridoEn) implements Evento {}
// ═══════════════════════════════════════════════════════════════
// EVENT STORE — append-only, nunca se modifica
// ═══════════════════════════════════════════════════════════════
static class EventStore {
// En producción sería una tabla con columnas: id, aggregateId, eventType, payload, version, timestamp
private final List<Evento> log = new ArrayList<>();
private int version = 0;
void append(Evento evento) {
log.add(evento);
version++;
System.out.printf(" [EventStore] v%d → %s%n", version, describir(evento));
}
List<Evento> cargarEventos(String cuentaId) {
return log.stream()
.filter(e -> e.cuentaId().equals(cuentaId))
.toList();
}
List<Evento> cargarEventosHasta(String cuentaId, int maxVersion) {
return log.stream()
.filter(e -> e.cuentaId().equals(cuentaId))
.limit(maxVersion)
.toList();
}
int version() { return version; }
private String describir(Evento e) {
return switch (e) {
case CuentaCreada c -> String.format("CuentaCreada{cuenta='%s', titular='%s'}", c.cuentaId(), c.titular());
case DepositoRealizado d -> String.format("DepositoRealizado{cuenta='%s', +%.2f '%s'}", d.cuentaId(), d.cantidad(), d.concepto());
case RetiroRealizado r -> String.format("RetiroRealizado{cuenta='%s', -%.2f '%s'}", r.cuentaId(), r.cantidad(), r.concepto());
case CuentaBloqueada b -> String.format("CuentaBloqueada{cuenta='%s', motivo='%s'}", b.cuentaId(), b.motivo());
};
}
}
// ═══════════════════════════════════════════════════════════════
// AGGREGATE: CuentaBancaria
// El estado se reconstruye aplicando eventos en orden
// ═══════════════════════════════════════════════════════════════
static class CuentaBancaria {
private String cuentaId;
private String titular;
private double saldo;
private boolean bloqueada;
private int version;
// Constructor privado — se crea siempre desde eventos
private CuentaBancaria() {}
// ── API de comandos — producen eventos ────────────────────────
static List<Evento> crearCuenta(String cuentaId, String titular) {
return List.of(new CuentaCreada(cuentaId, titular, Instant.now()));
}
List<Evento> depositar(double cantidad, String concepto) {
if (bloqueada) throw new IllegalStateException("Cuenta bloqueada");
if (cantidad <= 0) throw new IllegalArgumentException("Cantidad debe ser positiva");
return List.of(new DepositoRealizado(cuentaId, cantidad, concepto, Instant.now()));
}
List<Evento> retirar(double cantidad, String concepto) {
if (bloqueada) throw new IllegalStateException("Cuenta bloqueada");
if (cantidad > saldo) throw new IllegalStateException("Saldo insuficiente");
return List.of(new RetiroRealizado(cuentaId, cantidad, concepto, Instant.now()));
}
List<Evento> bloquear(String motivo) {
return List.of(new CuentaBloqueada(cuentaId, motivo, Instant.now()));
}
// ── Reconstrucción desde eventos ──────────────────────────────
static CuentaBancaria reconstituir(List<Evento> eventos) {
CuentaBancaria cuenta = new CuentaBancaria();
for (Evento evento : eventos) {
cuenta.apply(evento);
}
return cuenta;
}
private void apply(Evento evento) {
switch (evento) {
case CuentaCreada e -> {
this.cuentaId = e.cuentaId();
this.titular = e.titular();
this.saldo = 0;
this.bloqueada = false;
}
case DepositoRealizado e -> this.saldo += e.cantidad();
case RetiroRealizado e -> this.saldo -= e.cantidad();
case CuentaBloqueada e -> this.bloqueada = true;
}
this.version++;
}
// ── Estado actual ──────────────────────────────────────────────
String cuentaId() { return cuentaId; }
String titular() { return titular; }
double saldo() { return saldo; }
boolean bloqueada(){ return bloqueada; }
int version() { return version; }
@Override
public String toString() {
return String.format("CuentaBancaria{id='%s', titular='%s', saldo=%.2f, bloqueada=%s, v=%d}",
cuentaId, titular, saldo, bloqueada, version);
}
}
// ═══════════════════════════════════════════════════════════════
// PROYECTOR: construye una vista de lectura (CQRS)
// Escucha eventos y mantiene una proyección desnormalizada
// ═══════════════════════════════════════════════════════════════
static class MovimientosProyector {
private final List<String> movimientos = new ArrayList<>();
void proyectar(List<Evento> eventos) {
for (Evento e : eventos) {
switch (e) {
case DepositoRealizado d ->
movimientos.add(String.format("+ %.2f€ %-20s [%s]", d.cantidad(), d.concepto(), d.ocurridoEn()));
case RetiroRealizado r ->
movimientos.add(String.format("- %.2f€ %-20s [%s]", r.cantidad(), r.concepto(), r.ocurridoEn()));
default -> {} // ignorar otros eventos en esta proyección
}
}
}
void mostrar() {
System.out.println(" Movimientos:");
if (movimientos.isEmpty()) {
System.out.println(" (sin movimientos)");
} else {
movimientos.forEach(m -> System.out.println(" " + m));
}
}
}
// ═══════════════════════════════════════════════════════════════
// REPOSITORIO — guarda/carga eventos del EventStore
// ═══════════════════════════════════════════════════════════════
static class CuentaRepository {
private final EventStore store;
CuentaRepository(EventStore store) {
this.store = store;
}
void guardar(List<Evento> nuevosEventos) {
nuevosEventos.forEach(store::append);
}
CuentaBancaria cargar(String cuentaId) {
List<Evento> eventos = store.cargarEventos(cuentaId);
if (eventos.isEmpty()) throw new NoSuchElementException("Cuenta no encontrada: " + cuentaId);
return CuentaBancaria.reconstituir(eventos);
}
CuentaBancaria cargarEnVersion(String cuentaId, int version) {
List<Evento> eventos = store.cargarEventosHasta(cuentaId, version);
return CuentaBancaria.reconstituir(eventos);
}
}
// ═══════════════════════════════════════════════════════════════
// MAIN
// ═══════════════════════════════════════════════════════════════
public static void main(String[] args) {
System.out.println("═".repeat(65));
System.out.println(" EVENT SOURCING — Java puro");
System.out.println("═".repeat(65));
EventStore store = new EventStore();
CuentaRepository repo = new CuentaRepository(store);
// ── Fase 1: Crear cuenta y hacer operaciones ───────────────────
System.out.println("\n─ FASE 1: Creando cuenta y operaciones ─");
// Crear cuenta (genera eventos)
repo.guardar(CuentaBancaria.crearCuenta("CUENTA-001", "Ana García"));
// Cargar y depositar
CuentaBancaria cuenta = repo.cargar("CUENTA-001");
repo.guardar(cuenta.depositar(1000.00, "Nómina mayo"));
cuenta = repo.cargar("CUENTA-001");
repo.guardar(cuenta.depositar(200.00, "Freelance"));
cuenta = repo.cargar("CUENTA-001");
repo.guardar(cuenta.retirar(150.00, "Alquiler"));
cuenta = repo.cargar("CUENTA-001");
repo.guardar(cuenta.depositar(50.00, "Devolución"));
cuenta = repo.cargar("CUENTA-001");
repo.guardar(cuenta.retirar(80.00, "Supermercado"));
// ── Fase 2: Estado actual (reconstruido desde eventos) ─────────
System.out.println("\n─ FASE 2: Estado actual (reconstituido desde eventos) ─");
CuentaBancaria estadoActual = repo.cargar("CUENTA-001");
System.out.println(" " + estadoActual);
System.out.printf(" Saldo esperado: 1000 + 200 - 150 + 50 - 80 = 1020.00€%n");
// ── Fase 3: Temporal query — estado en un punto del pasado ─────
System.out.println("\n─ FASE 3: Temporal query — estado tras los primeros 2 eventos ─");
CuentaBancaria estadoV2 = repo.cargarEnVersion("CUENTA-001", 2);
System.out.println(" " + estadoV2);
System.out.printf(" Saldo esperado tras crear + depositar 1000€: 1000.00€%n");
CuentaBancaria estadoV3 = repo.cargarEnVersion("CUENTA-001", 3);
System.out.println(" Estado tras 3 eventos: " + estadoV3);
// ── Fase 4: Proyección de movimientos (CQRS) ───────────────────
System.out.println("\n─ FASE 4: Proyección de movimientos (vista de lectura) ─");
MovimientosProyector proyector = new MovimientosProyector();
proyector.proyectar(store.cargarEventos("CUENTA-001"));
proyector.mostrar();
// ── Fase 5: Bloqueo de cuenta + saldo insuficiente ────────────
System.out.println("\n─ FASE 5: Reglas de negocio en el aggregate ─");
CuentaBancaria cuentaActual = repo.cargar("CUENTA-001");
try {
repo.guardar(cuentaActual.retirar(5000.00, "Retiro imposible"));
} catch (IllegalStateException e) {
System.out.println(" Error esperado: " + e.getMessage());
}
repo.guardar(cuentaActual.bloquear("Actividad sospechosa"));
CuentaBancaria cuentaBloqueada = repo.cargar("CUENTA-001");
try {
repo.guardar(cuentaBloqueada.depositar(100.00, "Intento de depósito"));
} catch (IllegalStateException e) {
System.out.println(" Error esperado: " + e.getMessage());
}
// ── Resumen ────────────────────────────────────────────────────
System.out.println("\n" + "═".repeat(65));
System.out.println(" RESUMEN EVENT SOURCING");
System.out.println("═".repeat(65));
System.out.printf(" Total eventos en el store: %d%n", store.version());
System.out.println(" Estado actual: " + repo.cargar("CUENTA-001"));
System.out.println();
System.out.println(" Ventajas demostradas:");
System.out.println(" ✓ Audit trail completo (fase 4: todos los movimientos)");
System.out.println(" ✓ Temporal queries (fase 3: estado en cualquier punto)");
System.out.println(" ✓ Reglas de negocio en el aggregate (fase 5)");
System.out.println(" ✓ Proyecciones independientes para lectura (fase 4)");
System.out.println("═".repeat(65));
}
}