MediatR distinguish handlers by Attribute
See original GitHub issueConsidering to migrate my custom written CQRS framework to MediatR while transitioning from asp.net to asp.net.core.
public interface IQueryDispatcher
{
Task<TResult> Dispatch<TParameter, TResult>(TParameter query = null)
where TParameter : class, IQuery, new()
where TResult : IQueryResult;
Task<TResult> **DispatchForModule**<TParameter, TResult>(MyModule module, TParameter query = null)
where TParameter : class, IQuery, new()
where TResult : IQueryResult;
}
The issue I’m facing is the ability to dispatch queries/commands based on attribute attached to a handler.
[ModuleSpecific(MyModule.Bananas)]
public class RemoveRecordCommandHandler : ICommandHandler<RemoveRecordCommand>
{
...
}
The solution domain is split into modules which all have a base set of commands and queries. I would otherwise call something like
canEdit = _query.DispatchForModule(MyModule.Apples, new CanEditRecordQuery{Id = 10});
...
_command.DispatchForModule(MyModule.Bananas, new RemoveRecordCommand{Id = 20})
How does MediatR deal with multiple handler implementations for the equal type parameters of commands or queries.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
You Probably Don't Need to Worry About MediatR
When you're using a Mediator , you're not introducing dependencies on ALL handlers. Whatever problem this section describes, I've never seen it ...
Read more >MediatR conventions in CQRS
Handler is what you will need to manage/solve a request. Where each request has its own handler. Commands are where you will perform...
Read more >MediatR Pipeline Behaviour in ASP.NET Core - Logging ...
By doing this, we will be able to execute services/ logics like validations even before the Command or Query Handlers know about it....
Read more >Writing decoupled code with MediatR: The Mediator Pattern
To send a request, you need to create two objects: a request and a request handler. The request object should implement the IRequest...
Read more >Response Caching with MediatR in ASP.NET Core
Whenever you send a query to MediatR Handlers, your request and response have to go through an HTTP Pipeline aka MediatR Pipeline.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Handler duplication is no different than any other kind of duplication - if two handlers share the same set of code, refactor that code into some sort of shared function/class. You don’t need to have some additional redirection in a mediator to deal with duplication. It’s no different than two controller actions, or message handlers, or service classes etc. that have duplication.
MediatR enforces a single handler per request which FORCES you to deal with the duplication in any one of the dozen or so vanilla refactoring techniques. Do that, not some additional redirection that’s even harder to connect the dots to than the mediator pattern already.
It’s plugin based system with modules, and nothing to do with code duplication apart from interface signature of the handlers, which will implement conceptually similar but far from duplicate logic.
Each module is then a set of handlers that implement same interfaces.
I was not asking for guidance on avoiding ctrl-c, ctrl-p, but checking if there are other strategies to resolve those implementations, rather then that which strictly looks into the query/result types.
Anyways, ended up with my custom query/command dispatchers with decorators.
and then