This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
forked from cultureamp/kestrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregate.kt
337 lines (295 loc) · 17.3 KB
/
Aggregate.kt
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.cultureamp.eventsourcing
import arrow.core.*
import org.joda.time.DateTime
import java.util.UUID
import kotlin.reflect.KClass
interface SimpleAggregate<UC : UpdateCommand, UE : UpdateEvent> {
fun updated(event: UE): SimpleAggregate<UC, UE>
fun update(command: UC): Either<DomainError, List<UE>>
}
interface SimpleAggregateConstructor<CC : CreationCommand, CE : CreationEvent, UC : UpdateCommand, UE : UpdateEvent> {
fun created(event: CE): SimpleAggregate<UC, UE>
fun create(command: CC): Either<DomainError, CE>
fun aggregateType(): String = this::class.companionClassName
}
interface SimpleAggregateWithProjection<UC : UpdateCommand, UE : UpdateEvent, P> {
fun updated(event: UE): SimpleAggregateWithProjection<UC, UE, P>
fun update(projection: P, command: UC): Either<DomainError, List<UE>>
fun partial(projection: P): SimpleAggregate<UC, UE> {
return object : SimpleAggregate<UC, UE> {
override fun updated(event: UE): SimpleAggregate<UC, UE> {
return [email protected](event).partial(projection)
}
override fun update(command: UC): Either<DomainError, List<UE>> {
return update(projection, command)
}
}
}
}
interface SimpleAggregateConstructorWithProjection<CC : CreationCommand, CE : CreationEvent, UC : UpdateCommand, UE : UpdateEvent, P> {
fun created(event: CE): SimpleAggregateWithProjection<UC, UE, P>
fun create(projection: P, command: CC): Either<DomainError, CE>
fun aggregateType(): String = this::class.companionClassName
fun partial(projection: P): SimpleAggregateConstructor<CC, CE, UC, UE> {
return object : SimpleAggregateConstructor<CC, CE, UC, UE> {
override fun created(event: CE): SimpleAggregate<UC, UE> {
return [email protected](event).partial(projection)
}
override fun create(command: CC): Either<DomainError, CE> {
return create(projection, command)
}
override fun aggregateType(): String = [email protected]()
}
}
}
interface Aggregate<UC : UpdateCommand, UE : UpdateEvent, Err : DomainError, M : EventMetadata, out Self : Aggregate<UC, UE, Err, M, Self>> {
fun updated(event: UE): Self
fun update(command: UC, metadata: M): Either<Err, List<UE>>
companion object {
fun <UC : UpdateCommand, UE : UpdateEvent, Err : DomainError, M : EventMetadata, A : Any> from(
aggregate: A,
update: A.(UC, M) -> Either<Err, List<UE>>,
updated: A.(UE) -> A = { _ -> this }
): Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
return object : Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
override fun updated(event: UE): Aggregate<UC, UE, Err, M, *> {
val updatedAggregate = aggregate.updated(event)
return from(updatedAggregate, update, updated)
}
override fun update(command: UC, metadata: M): Either<Err, List<UE>> {
return aggregate.update(command, metadata)
}
}
}
fun <UC : UpdateCommand, UE : UpdateEvent, Err : DomainError, M : EventMetadata, A : Any> from(
aggregate: A,
update: A.(UC) -> Either<Err, List<UE>>,
updated: A.(UE) -> A = { _ -> this }
): Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
return object : Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
override fun updated(event: UE): Aggregate<UC, UE, Err, M, *> {
val updatedAggregate = aggregate.updated(event)
return from(updatedAggregate, update, updated)
}
override fun update(command: UC, metadata: M): Either<Err, List<UE>> {
return aggregate.update(command)
}
}
}
fun <UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, A : Any> from(
aggregate: SimpleAggregate<UC, UE>,
): Aggregate<UC, UE, DomainError, M, Aggregate<UC, UE, DomainError, M, *>> {
return object : Aggregate<UC, UE, DomainError, M, Aggregate<UC, UE, DomainError, M, *>> {
override fun updated(event: UE): Aggregate<UC, UE, DomainError, M, *> {
val updatedAggregate = aggregate.updated(event)
return from<UC, UE, M, A>(updatedAggregate)
}
override fun update(command: UC, metadata: M): Either<DomainError, List<UE>> {
return aggregate.update(command)
}
}
}
}
}
interface AggregateWithProjection<UC : UpdateCommand, UE : UpdateEvent, Err : DomainError, P, M : EventMetadata, Self : AggregateWithProjection<UC, UE, Err, P, M, Self>> {
fun updated(event: UE): Self
fun update(projection: P, command: UC, metadata: M): Either<Err, List<UE>>
fun partial(projection: P): Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
return object : Aggregate<UC, UE, Err, M, Aggregate<UC, UE, Err, M, *>> {
override fun updated(event: UE): Aggregate<UC, UE, Err, M, *> {
return [email protected](event).partial(projection)
}
override fun update(command: UC, metadata: M): Either<Err, List<UE>> {
return update(projection, command, metadata)
}
}
}
}
interface AggregateConstructor<CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, Self : Aggregate<UC, UE, Err, M, Self>> {
fun created(event: CE): Self
fun create(command: CC, metadata: M): Either<Err, Pair<CE, List<UE>>>
fun aggregateType(): String = this::class.companionClassName
companion object {
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> from(
noinline create: (CC, M) -> Either<Err, CE>,
noinline update: A.(UC, M) -> Either<Err, List<UE>>,
noinline created: (CE) -> A,
noinline updated: A.(UE) -> A = { _ -> this },
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
val createReturningMultipleEvents: (CC, M) -> Either<Err, Pair<CE, List<UE>>> = { cc, m -> create(cc, m).map { it to emptyList() } }
return from(createReturningMultipleEvents, update, created, updated, aggregateType)
}
@JvmName("fromCreationCommandReturningMultipleEvents")
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> from(
noinline create: (CC, M) -> Either<Err, Pair<CE, List<UE>>>,
noinline update: A.(UC, M) -> Either<Err, List<UE>>,
noinline created: (CE) -> A,
noinline updated: A.(UE) -> A = { _ -> this },
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
return object : AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
override fun created(event: CE): Aggregate<UC, UE, Err, M, *> {
val createdAggregate = created(event)
return Aggregate.from(createdAggregate, update, updated)
}
override fun create(command: CC, metadata: M): Either<Err, Pair<CE, List<UE>>> = create(command, metadata)
override fun aggregateType() = aggregateType()
}
}
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> from(
noinline create: (CC) -> Either<Err, CE>,
noinline update: A.(UC) -> Either<Err, List<UE>>,
noinline created: (CE) -> A,
noinline updated: A.(UE) -> A = { _ -> this },
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
val createReturningMultipleEvents: (CC) -> Either<Err, Pair<CE, List<UE>>> = { cc -> create(cc).map { it to emptyList() } }
return from(createReturningMultipleEvents, update, created, updated, aggregateType)
}
@JvmName("fromCreationCommandReturningMultipleEvents")
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> from(
noinline create: (CC) -> Either<Err, Pair<CE, List<UE>>>,
noinline update: A.(UC) -> Either<Err, List<UE>>,
noinline created: (CE) -> A,
noinline updated: A.(UE) -> A = { _ -> this },
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
return object : AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
override fun created(event: CE): Aggregate<UC, UE, Err, M, *> {
val createdAggregate = created(event)
return Aggregate.from(createdAggregate, update, updated)
}
override fun create(command: CC, metadata: M): Either<Err, Pair<CE, List<UE>>> = create(command)
override fun aggregateType() = aggregateType()
}
}
inline fun <CC : CreationCommand, CE : CreationEvent, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> from(
simpleAggregateConstructor: SimpleAggregateConstructor<CC, CE, UC, UE>
): AggregateConstructor<CC, CE, DomainError, UC, UE, M, Aggregate<UC, UE, DomainError, M, *>> {
return object : AggregateConstructor<CC, CE, DomainError, UC, UE, M, Aggregate<UC, UE, DomainError, M, *>> {
override fun created(event: CE): Aggregate<UC, UE, DomainError, M, *> {
val createdAggregate = simpleAggregateConstructor.created(event)
return Aggregate.from<UC, UE, M, SimpleAggregate<UC, UE>>(createdAggregate)
}
override fun create(command: CC, metadata: M): Either<DomainError, Pair<CE, List<UE>>> = simpleAggregateConstructor.create(command).map { it to emptyList() }
override fun aggregateType() = simpleAggregateConstructor.aggregateType()
}
}
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> fromStateless(
noinline create: (CC, M) -> Either<Err, CE>,
noinline update: (UC, M) -> Either<Err, List<UE>>,
instance: A,
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
val createReturningMultipleEvents: (CC, M) -> Either<Err, Pair<CE, List<UE>>> = { cc, m -> create(cc, m).map { it to emptyList() } }
return fromStateless(createReturningMultipleEvents, update, instance, aggregateType)
}
@JvmName("fromStatelessCreationCommandReturningMultipleEvents")
inline fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata, reified A : Any> fromStateless(
noinline create: (CC, M) -> Either<Err, Pair<CE, List<UE>>>,
noinline update: (UC, M) -> Either<Err, List<UE>>,
instance: A,
noinline aggregateType: () -> String = { A::class.simpleName!! }
): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
return from(create, { uc: UC, m: M -> update(uc, m) }, { instance }, { instance }, aggregateType)
}
}
}
interface AggregateConstructorWithProjection<CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, P, M : EventMetadata, Self : AggregateWithProjection<UC, UE, Err, P, M, Self>> {
fun created(event: CE): Self
fun create(projection: P, command: CC, metadata: M): Either<Err, Pair<CE, List<UE>>>
fun aggregateType(): String = this::class.companionClassName
fun partial(projection: P): AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
return object : AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>> {
override fun created(event: CE): Aggregate<UC, UE, Err, M, *> {
return [email protected](event).partial(projection)
}
override fun create(command: CC, metadata: M): Either<Err, Pair<CE, List<UE>>> {
return create(projection, command, metadata)
}
override fun aggregateType(): String = [email protected]()
}
}
}
internal fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata>
AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>>.create(
creationCommand: CC,
metadata: M,
eventStore: EventStore<M>
): Either<CommandError, Long> {
return create(creationCommand, metadata).map { initialEvents ->
created(initialEvents.first).updated(initialEvents.second) // called to ensure the create event handler doesn't throw any exceptions
val domainEvents = listOf(initialEvents.first) + initialEvents.second
val events = domainEvents.mapIndexed { index, domainEvent ->
Event(
id = UUID.randomUUID(),
aggregateId = creationCommand.aggregateId,
aggregateSequence = index + 1L,
aggregateType = aggregateType(),
createdAt = DateTime.now(),
metadata = metadata,
domainEvent = domainEvent
)
}
eventStore.sink(events, creationCommand.aggregateId)
}.flatten()
}
internal fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, UC : UpdateCommand, UE : UpdateEvent, M : EventMetadata>
AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>>.update(
updateCommand: UC,
metadata: M,
events: List<Event<M>>,
eventStore: EventStore<M>
): Either<CommandError, Long> {
val upcastedEvents = events.map {
val domainEvent = it.domainEvent
val upcastEvent = domainEvent::class.annotations.filterIsInstance<UpcastEvent>()
if(upcastEvent.size == 1) {
it.copy(domainEvent = upcastEvent.first().upcasting(domainEvent, metadata))
}else{
it
}
}
val aggregate = rehydrate(upcastedEvents)
return aggregate.flatMap {
it.update(updateCommand, metadata).flatMap { domainEvents ->
it.updated(domainEvents) // called to ensure the update event handler doesn't throw any exceptions
val offset = upcastedEvents.last().aggregateSequence + 1
val createdAt = DateTime()
val storableEvents = domainEvents.withIndex().map { (index, domainEvent) ->
Event(
id = UUID.randomUUID(),
aggregateId = updateCommand.aggregateId,
aggregateSequence = offset + index,
aggregateType = aggregateType(),
createdAt = createdAt,
metadata = metadata,
domainEvent = domainEvent
)
}
eventStore.sink(storableEvents, updateCommand.aggregateId)
}
}
}
@Suppress("UNCHECKED_CAST")
private fun <CC : CreationCommand, CE : CreationEvent, Err : DomainError, M : EventMetadata, UC : UpdateCommand, UE : UpdateEvent>
AggregateConstructor<CC, CE, Err, UC, UE, M, Aggregate<UC, UE, Err, M, *>>.rehydrate(
events: List<Event<M>>
): Either<ConstructorTypeMismatch, Aggregate<UC, UE, Err, M, *>> {
val creationEvent = events.first()
val creationDomainEvent = creationEvent.domainEvent as CE
val aggregate = if (creationEvent.aggregateType == aggregateType()) {
created(creationDomainEvent).right()
} else {
ConstructorTypeMismatch(aggregateType(), creationDomainEvent::class).left()
}
val updateEvents = events.drop(1).map { it.domainEvent as UE }
return aggregate.map { it.updated(updateEvents) }
}
@Suppress("UNCHECKED_CAST")
private fun <UC : UpdateCommand, UE : UpdateEvent, Err : DomainError, M : EventMetadata> Aggregate<UC, UE, Err, M, *>.updated(updateEvents: List<UE>): Aggregate<UC, UE, Err, M, *> {
return updateEvents.fold(this) { aggregate, updateEvent -> aggregate.updated(updateEvent) as Aggregate<UC, UE, Err, M, *> }
}
val <T : Any> KClass<T>.companionClassName get() = if (isCompanion) this.java.declaringClass.simpleName!! else this::class.simpleName!!
data class ConstructorTypeMismatch(val aggregateType: String, val eventType: KClass<out CreationEvent>) : CommandError