diff --git a/docs/application/application.md b/docs/application/application.md index ab534e8..25b912d 100644 --- a/docs/application/application.md +++ b/docs/application/application.md @@ -695,4 +695,84 @@ suspend fun StateStoredAggregate.handle(command: C): S = -Feel free to use these two extension modules, or create your own by using these two as a fine example. \ No newline at end of file + +An example (taken from FModel `application-arrow` library), in case you prefer typed errors: + + + + + +```kotlin +fun EventSourcingAggregate.handleWithEffect(command: C): Flow> = + command + .fetchEvents() + .computeNewEvents(command) + .save() + .map { either { it } } + .catch { emit(either { raise(CommandHandlingFailed(command)) }) } +``` + + + + +```kotlin +suspend fun I.handleWithEffect(command: C): Either where I : StateComputation, + I : StateRepository { + /** + * Inner function - Computes new State based on the previous State and the [command] or fails. + * + * @param command of type [C] + * @return [Either] (either the newly computed state of type [S] or [Error]) + */ + suspend fun S?.computeNewStateWithEffect(command: C): Either = + either { + catch({ + computeNewState(command) + }) { + raise(CalculatingNewStateFailed(this@computeNewStateWithEffect, command, it)) + } + } + + /** + * Inner function - Fetch state - either version + * + * @receiver Command of type [C] + * @return [Either] (either [Error] or the State of type [S]?) + */ + suspend fun C.fetchStateWithEffect(): Either = + either { + catch({ + fetchState() + }) { + raise(FetchingStateFailed(this@fetchStateWithEffect, it)) + } + } + + /** + * Inner function - Save state - either version + * + * @receiver State of type [S] + * @return [Either] (either [Error] or the newly saved State of type [S]) + */ + suspend fun S.saveWithEffect(): Either = + either { + catch({ + save() + }) { + raise(StoringStateFailed(this@saveWithEffect, it)) + } + } + + return either { + command + .fetchStateWithEffect().bind() + .computeNewStateWithEffect(command).bind() + .saveWithEffect().bind() + } +} +``` + + + + +Feel free to use these two extension modules, or create your own by using these two as a good example. \ No newline at end of file