Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for handler resolution/invocation without knowing response type #163

Open
CocoDico78 opened this issue Dec 2, 2024 · 0 comments

Comments

@CocoDico78
Copy link

CocoDico78 commented Dec 2, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant