handle<TCommand extends Object> method
FutureOr<AggregateCommandResult<TEvent, TValue, TId, TState, TAggregate> >
handle<TCommand extends Object>(
- TCommand command
The generic command handler. Call this function from your edge (API).
Use parameter command
to execute
Returns the AggregateCommandResult
of the execution
- Type parameter
TCommand
- command type
Implementation
FutureOr<AggregateCommandResult<TEvent, TValue, TId, TState, TAggregate>>
handle<TCommand extends Object>(
TCommand command,
) async {
TAggregate? aggregate;
final handler = _handlers[typeOf<TCommand>()];
if (handler == null) {
throw CommandHandlerNotFoundException(typeOf<TCommand>());
}
try {
switch (handler.expected) {
case ExpectedState.notExists:
aggregate = await _create<TCommand>(command);
break;
case ExpectedState.exists:
aggregate = await _load<TCommand>(command);
break;
case ExpectedState.any:
aggregate = await _tryLoad<TCommand>(command);
break;
default:
return _toError(ArgumentError.value(
handler.expected,
'$handler',
'Unknown expected state',
));
}
await _handlers[typeOf<TCommand>()]!(
command,
aggregate,
);
return aggregate.isChanged
? _fromResult(
aggregate,
await store.save(aggregate),
)
: _toOk(aggregate);
} on Exception catch (error) {
return _toError(
error,
aggregate,
);
}
}