question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MediatR distinguish handlers by Attribute

See original GitHub issue

Considering 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jbogardcommented, Aug 17, 2020

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.

0reactions
apylypchukcommented, Dec 15, 2020

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.

    [Area(Action.Module.Name)]
    public class ModuleFilterHandler : ModuleFilterHandlerBase, IQueryHandler<FilteringQuery, FilteringQueryResult>
    [Area(Incident.Module.Name)]
    public class ModuleFilterHandler : ModuleFilterHandlerBase, IQueryHandler<FilteringQuery, FilteringQueryResult>

and then

        [HttpPost("[action]")]
        public async Task<ActionResult<FilteringQueryResult>> FilterResult([FromBody]FilteringQuery query)
        {
            FilteringQueryResult result = await _queryDispatcher.SendToArea<FilteringQuery, FilteringQueryResult>(query.Module, query);
            return result;
        }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found