Open
Description
In a situation where commands are serialized and pushed on a command queue, how can we call the appropriate handler after de-serialization?
// In publisher process
var command = new CreateUserCommand.Command("[email protected]");
commandQueue.Push((JsonSerializer.Serialize(command), command.GetType()));
// ...
// In consumer process
(string commandRaw, Type commandType) = commandQueue.Pop();
var command = JsonSerializer.Deserialize(commandRaw, commandType);
await handlerInvoker.InvokeHandlerAsync(command); // <- suggested API
A work-around would be to try to resolve a IHandler<TCommand, ValueTuple>
from the DI container since we know TCommand
but it gets ugly. Also there is no guarantee that the handler was actually implemented as a Command handler so the response type might be unknown (and irrelevant, we just want to run the handler).
This is close to the MediatR
API where you can execute a handler simply by knowing the command object: mediator.Send(command)
.
Metadata
Assignees
Labels
No labels
Activity