/
EventSourcingAggregate.kt
348 lines (328 loc) · 17.8 KB
/
EventSourcingAggregate.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
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2023 Fraktalio D.O.O. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fraktalio.fmodel.application
import com.fraktalio.fmodel.domain.IDecider
import com.fraktalio.fmodel.domain.ISaga
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
/**
* `EventComputation` interface formalizes the `Event Computation` algorithm / event sourced system by using a `decider` of type [IDecider]<[C], [S], [E]> to handle commands based on the current events, and produce new events.
*/
interface EventComputation<C, S, E> : IDecider<C, S, E> {
fun Flow<E>.computeNewEvents(command: C): Flow<E> = flow {
val currentState = fold(initialState) { s, e -> evolve(s, e) }
val resultingEvents = decide(command, currentState)
emitAll(resultingEvents)
}
}
/**
* `EventOrchestratingComputation` interface formalizes the `Event Computation` algorithm / event sourced system by using a `decider` of type [IDecider]<[C], [S], [E]> to handle commands based on the current events, and produce new events.
* Additionally, the computation is using the [ISaga]<[E], [C]> to further orchestrate the process in were Decider could be triggered by a new Command.
*/
interface EventOrchestratingComputation<C, S, E> : ISaga<E, C>, IDecider<C, S, E> {
@ExperimentalCoroutinesApi
fun Flow<E>.computeNewEventsByOrchestrating(command: C, fetchEvents: (C) -> Flow<E>): Flow<E> = flow {
val currentState = fold(initialState) { s, e -> evolve(s, e) }
var resultingEvents = decide(command, currentState)
resultingEvents
.flatMapConcat { react(it) }
.onEach { c ->
val previousEvents = resultingEvents.toList().asFlow()
val newEvents = flowOf(fetchEvents(c), previousEvents)
.flattenConcat()
.computeNewEventsByOrchestrating(c, fetchEvents)
resultingEvents = flowOf(resultingEvents, newEvents).flattenConcat()
}.collect()
emitAll(resultingEvents)
}
}
/**
* Event sourcing aggregate is using/delegating a `decider` of type [IDecider]<[C], [S], [E]>/ [EventComputation]<[C], [S], [E]> to handle commands and produce events.
* In order to handle the command, aggregate needs to fetch the current state (represented as a list of events) via [EventRepository.fetchEvents] function, and then delegate the command to the `decider` which can produce new event(s) as a result.
* Produced events are then stored via [EventRepository.save] suspending function.
*
* [EventSourcingAggregate] extends [EventComputation] and [EventRepository] interfaces,
* clearly communicating that it is composed out of these two behaviours.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that this aggregate can publish
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
interface EventSourcingAggregate<C, S, E> : EventComputation<C, S, E>, EventRepository<C, E>
/**
* Locking Event sourcing aggregate is using/delegating a `decider` of type [IDecider]<[C], [S], [E]>/ [EventComputation]<[C], [S], [E]> to handle commands and produce events.
* In order to handle the command, aggregate needs to fetch the current state (represented as a list of events) via [EventLockingRepository.fetchEvents] function, and then delegate the command to the `decider` which can produce new event(s) as a result.
* Produced events are then stored via [EventLockingRepository.save] suspending function.
*
* Locking Event sourcing aggregate enables `optimistic locking` mechanism more explicitly.
* If you fetch events from a storage, the application records the `version` number of that event stream.
* You can append new events, but only if the `version` number in the storage has not changed.
* If there is a `version` mismatch, it means that someone else has added the event(s) before you did.
*
* [EventSourcingLockingAggregate] extends [EventComputation] and [EventLockingRepository] interfaces,
* clearly communicating that it is composed out of these two behaviours.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that this aggregate can publish
* @param V Version
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
interface EventSourcingLockingAggregate<C, S, E, V> : EventComputation<C, S, E>, EventLockingRepository<C, E, V>
/**
* Orchestrating Event sourcing aggregate is using/delegating a `decider` of type [IDecider]<[C], [S], [E]> to handle commands and produce events.
* In order to handle the command, aggregate needs to fetch the current state (represented as a list of events) via [EventRepository.fetchEvents] function, and then delegate the command to the `decider` which can produce new event(s) as a result.
* If the `decider` is combined out of many deciders via `combine` function, an optional `saga` of type [ISaga] could be used to react on new events and send new commands to the 'decider` recursively, in single transaction.
* This behaviour is formalized in [EventOrchestratingComputation].
* Produced events are then stored via [EventRepository.save] suspending function.
*
* [EventSourcingOrchestratingAggregate] extends [EventOrchestratingComputation] and [EventRepository] interfaces,
* clearly communicating that it is composed out of these two behaviours.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that this aggregate can publish
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
interface EventSourcingOrchestratingAggregate<C, S, E> : EventOrchestratingComputation<C, S, E>, EventRepository<C, E>
/**
* Orchestrating Locking Event sourcing aggregate is using/delegating a `decider` of type [IDecider]<[C], [S], [E]> to handle commands and produce events.
* In order to handle the command, aggregate needs to fetch the current state (represented as a list of events) via [EventLockingRepository.fetchEvents] function, and then delegate the command to the `decider` which can produce new event(s) as a result.
* If the `decider` is combined out of many deciders via `combine` function, an optional `saga` of type [ISaga] could be used to react on new events and send new commands to the 'decider` recursively, in single transaction.
* This behaviour is formalized in [EventOrchestratingComputation].
* Produced events are then stored via [EventLockingRepository.save] suspending function.
*
* Locking Orchestrating Event sourcing aggregate enables `optimistic locking` mechanism more explicitly.
* If you fetch events from a storage, the application records the `version` number of that event stream.
* You can append new events, but only if the `version` number in the storage has not changed.
* If there is a `version` mismatch, it means that someone else has added the event(s) before you did.
*
* [EventSourcingLockingOrchestratingAggregate] extends [EventOrchestratingComputation] and [EventLockingRepository] interfaces,
* clearly communicating that it is composed out of these two behaviours.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that this aggregate can publish
* @param V Version
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
interface EventSourcingLockingOrchestratingAggregate<C, S, E, V> : EventOrchestratingComputation<C, S, E>,
EventLockingRepository<C, E, V>
/**
* Event Sourced aggregate factory function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventRepository]<[C], [E]>
* @return An object/instance of type [EventSourcingAggregate]<[C], [S], [E]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
@Deprecated(
message = "Use EventSourcingAggregate constructor-like function instead",
replaceWith = ReplaceWith("EventSourcingAggregate(decider, eventRepository)")
)
fun <C, S, E> eventSourcingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventRepository<C, E>
): EventSourcingAggregate<C, S, E> =
object : EventSourcingAggregate<C, S, E>,
EventRepository<C, E> by eventRepository,
IDecider<C, S, E> by decider {}
/**
* Event Sourced aggregate constructor-like function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventRepository]<[C], [E]>
* @return An object/instance of type [EventSourcingAggregate]<[C], [S], [E]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
fun <C, S, E> EventSourcingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventRepository<C, E>
): EventSourcingAggregate<C, S, E> =
object : EventSourcingAggregate<C, S, E>,
EventRepository<C, E> by eventRepository,
IDecider<C, S, E> by decider {}
/**
* Event Sourced Locking aggregate factory function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param V Version
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventLockingRepository]<[C], [E], [V]>
* @return An object/instance of type [EventSourcingLockingAggregate]<[C], [S], [E], [V]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
@Deprecated(
message = "Use EventSourcingLockingAggregate constructor-like function instead",
replaceWith = ReplaceWith("EventSourcingLockingAggregate(decider, eventRepository)")
)
fun <C, S, E, V> eventSourcingLockingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventLockingRepository<C, E, V>
): EventSourcingLockingAggregate<C, S, E, V> =
object : EventSourcingLockingAggregate<C, S, E, V>,
EventLockingRepository<C, E, V> by eventRepository,
IDecider<C, S, E> by decider {}
/**
* Event Sourced Locking aggregate constructor-like function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param V Version
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventLockingRepository]<[C], [E], [V]>
* @return An object/instance of type [EventSourcingLockingAggregate]<[C], [S], [E], [V]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
fun <C, S, E, V> EventSourcingLockingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventLockingRepository<C, E, V>
): EventSourcingLockingAggregate<C, S, E, V> =
object : EventSourcingLockingAggregate<C, S, E, V>,
EventLockingRepository<C, E, V> by eventRepository,
IDecider<C, S, E> by decider {}
/**
* Event Sourced Orchestrating aggregate factory function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventRepository]<[C], [E]>
* @param saga A saga component of type [ISaga]<[E], [C]> - orchestrates the deciders
* @return An object/instance of type [EventSourcingOrchestratingAggregate]<[C], [S], [E]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
@Deprecated(
message = "Use EventSourcingOrchestratingAggregate constructor-like function instead",
replaceWith = ReplaceWith("EventSourcingOrchestratingAggregate(decider, eventRepository, saga)")
)
fun <C, S, E> eventSourcingOrchestratingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventRepository<C, E>,
saga: ISaga<E, C>
): EventSourcingOrchestratingAggregate<C, S, E> =
object : EventSourcingOrchestratingAggregate<C, S, E>,
EventRepository<C, E> by eventRepository,
IDecider<C, S, E> by decider,
ISaga<E, C> by saga {}
/**
* Event Sourced Orchestrating aggregate constructor-like function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventRepository]<[C], [E]>
* @param saga A saga component of type [ISaga]<[E], [C]> - orchestrates the deciders
* @return An object/instance of type [EventSourcingOrchestratingAggregate]<[C], [S], [E]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
fun <C, S, E> EventSourcingOrchestratingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventRepository<C, E>,
saga: ISaga<E, C>
): EventSourcingOrchestratingAggregate<C, S, E> =
object : EventSourcingOrchestratingAggregate<C, S, E>,
EventRepository<C, E> by eventRepository,
IDecider<C, S, E> by decider,
ISaga<E, C> by saga {}
/**
* Event Sourced Locking Orchestrating aggregate factory function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param V Version
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventLockingRepository]<[C], [E], [V]>
* @param saga A saga component of type [ISaga]<[E], [C]> - orchestrates the deciders
* @return An object/instance of type [EventSourcingLockingOrchestratingAggregate]<[C], [S], [E] ,[V]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
@Deprecated(
message = "Use EventSourcingLockingOrchestratingAggregate constructor-like function instead",
replaceWith = ReplaceWith("EventSourcingLockingOrchestratingAggregate(decider, eventRepository, saga)")
)
fun <C, S, E, V> eventSourcingLockingOrchestratingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventLockingRepository<C, E, V>,
saga: ISaga<E, C>
): EventSourcingLockingOrchestratingAggregate<C, S, E, V> =
object : EventSourcingLockingOrchestratingAggregate<C, S, E, V>,
EventLockingRepository<C, E, V> by eventRepository,
IDecider<C, S, E> by decider,
ISaga<E, C> by saga {}
/**
* Event Sourced Locking Orchestrating aggregate constructor-like function.
*
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
*
* @param C Commands of type [C] that this aggregate can handle
* @param S Aggregate state of type [S]
* @param E Events of type [E] that are used internally to build/fold new state
* @param V Version
* @param decider A decider component of type [IDecider]<[C], [S], [E]>
* @param eventRepository An aggregate event repository of type [EventLockingRepository]<[C], [E], [V]>
* @param saga A saga component of type [ISaga]<[E], [C]> - orchestrates the deciders
* @return An object/instance of type [EventSourcingLockingOrchestratingAggregate]<[C], [S], [E] ,[V]>
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
fun <C, S, E, V> EventSourcingLockingOrchestratingAggregate(
decider: IDecider<C, S, E>,
eventRepository: EventLockingRepository<C, E, V>,
saga: ISaga<E, C>
): EventSourcingLockingOrchestratingAggregate<C, S, E, V> =
object : EventSourcingLockingOrchestratingAggregate<C, S, E, V>,
EventLockingRepository<C, E, V> by eventRepository,
IDecider<C, S, E> by decider,
ISaga<E, C> by saga {}