Skip to content

Commit

Permalink
Merge pull request #347 from DomenicDev/bugfix/state-stored-orchestra…
Browse files Browse the repository at this point in the history
…ting-aggregate

Fix orchestrated/recursive state computation to ensure proper state updates
  • Loading branch information
idugalic authored Jan 13, 2025
2 parents dd4d9eb + 506c78c commit 882bc5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumber
import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumberStateRepository
import com.fraktalio.fmodel.application.examples.numbers.numberStateRepository
import com.fraktalio.fmodel.domain.IDecider
import com.fraktalio.fmodel.domain.ISaga
import com.fraktalio.fmodel.domain.combine
import com.fraktalio.fmodel.domain.examples.numbers.api.Description
import com.fraktalio.fmodel.domain.examples.numbers.api.EvenNumberState
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberCommand.EvenNumberCommand.AddEvenNumber
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberValue
import com.fraktalio.fmodel.domain.examples.numbers.api.OddNumberState
import com.fraktalio.fmodel.domain.examples.numbers.even.command.evenNumberDecider
import com.fraktalio.fmodel.domain.examples.numbers.numberSaga
import com.fraktalio.fmodel.domain.examples.numbers.odd.command.oddNumberDecider
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
Expand All @@ -38,6 +40,16 @@ private suspend fun <C, S, E, V> IDecider<C, S, E>.given(
stateRepository = repository
).handleOptimistically(command())

private suspend fun <C, S, E> IDecider<C, S, E>.given(
repository: StateRepository<C, S>,
saga: ISaga<E, C>,
command: () -> C
) = StateStoredOrchestratingAggregate(
decider = this,
saga = saga,
stateRepository = repository
).handle(command())

/**
* DSL - When
*/
Expand All @@ -61,6 +73,7 @@ class StateStoredAggregateTest : FunSpec({
val evenNumberStateRepository = evenNumberStateRepository() as EvenNumberStateRepository
val evenNumberLockingStateRepository = evenNumberLockingStateRepository() as EvenNumberLockingStateRepository
val numberStateRepository = numberStateRepository() as NumberStateRepository
val numberSaga = numberSaga()

test("State-stored aggregate - add even number") {
with(evenDecider) {
Expand Down Expand Up @@ -105,4 +118,16 @@ class StateStoredAggregateTest : FunSpec({
)
}
}

test("Orchestrated state-stored aggregate - add even and odd number") {
with(combinedDecider) {
numberStateRepository.deleteAll()
given(numberStateRepository, numberSaga) {
whenCommand(AddEvenNumber(Description("4"), NumberValue(4)))
} thenState Pair(
EvenNumberState(Description("4"), NumberValue(4)),
OddNumberState(Description("3"), NumberValue(3))
)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ interface StateOrchestratingComputation<C, S, E> : ISaga<E, C>, StateComputation
override suspend fun S?.computeNewState(command: C): S {
val currentState = this ?: initialState
val events = decide(command, currentState)
val newState = events.fold(currentState) { s, e -> evolve(s, e) }
events.flatMapConcat { react(it) }.onEach { newState.computeNewState(it) }.collect()
var newState = events.fold(currentState) { s, e -> evolve(s, e) }
events.flatMapConcat { react(it) }.collect {
newState = newState.computeNewState(it)
}
return newState
}
}
Expand Down

0 comments on commit 882bc5a

Please sign in to comment.