Questions tagged [mediatr]

MediatR is a simple, open source mediator implementation in .NET.

MediatR is a simple, open source, mediator implementation in .NET

A low-ambition library trying to solve a simple problem - decoupling the in-proc sending of messages from handling messages. Cross-platform, supporting .NET 4.5, .NET Standard 1.3, and .NET Standard 2.0.

Supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.

See https://github.com/jbogard/MediatR/wiki for more details.

507 questions
6
votes
2 answers

MediatR generic handlers

I use a MediatR library in my Asp .Net core 6 app. And I have a the same logic for a group of requests, so I want to use the same handler for all of them. The problem is not new, but the solutions that I found did not help me. I have my own solution…
JamesBondCaesar
  • 193
  • 2
  • 8
6
votes
1 answer

How do I register a request with a generic type in MediatR

I have the following class SearchQuery And it's used with MediatR like this on a controller: await Mediator.Send( new SearchQuery( new PageInformation(pageNumber, pageSize), Request.Query)); And I've registered…
James
  • 1,363
  • 19
  • 28
6
votes
3 answers

Is there a way to skip MediatR Pipeline?

I would like to cache some responses from CommandsHandlers. I Already did this using IPipelineBehaviour, but only 5% of my requests really must have cache, and the other 95% must skip this Pipeline. Is there a way to do that? Below follows my…
raphael-melo
  • 73
  • 1
  • 5
6
votes
6 answers

Mediatr with generic handler and query

I am working on a ASP.NET Core 2.2 Web API application with Mediatr. I have a handler that looks like - public class MyQueryHandler : IRequestHanlder, IQueryable> { public Task> Handle(MyQuery myquery,…
Bryan
  • 5,065
  • 10
  • 51
  • 68
6
votes
2 answers

How to decorate a MediatR Handler

I want to decorate just one MediatR Handler. I tried using Behaviours, but Behaviours inject the decorator for every handler that implements IRequestHandler public class ProcessFirstCommand : IRequest { public string…
Rupesh
  • 103
  • 1
  • 4
6
votes
2 answers

MediatR fluent validation response from pipeline behavior

I have a MediatR Pipeline behavior for validating commands with the FluentValidation library. I've seen many examples where you throw a ValidationException from the behavior, and that works fine for me. However in my scenario I want to update my…
INNVTV
  • 3,155
  • 7
  • 37
  • 71
6
votes
1 answer

Mediatr: Unit Testing behaviors/validation

I have a command/handler that saves an entity to the db, but in my code, it goes through validation first (validation pipeline) using fluentvalidation. I was able to create a success test to test the handler, but now I would like to make sure the…
Zoinky
  • 4,083
  • 11
  • 40
  • 78
6
votes
1 answer

MediatR - Handling current user in query

What is the best way to model the current user in a query? I'm creating a razor page application. I need to be able to attach the current user when I am executing queries and commands. Is there a recommended approach for doing this?
Zeus82
  • 6,065
  • 9
  • 53
  • 77
6
votes
3 answers

CQRS with MediatR and re-usability of commands

Does it make sense to create commands that just hold objects? For example: public class CreateCommand : IRequest { SomeDTO SomeDTO { get; set; } } public class UpdateCommand : IRequest { SomeDTO SomeDTO { get; set; } } Or perhaps something…
Konrad
  • 6,385
  • 12
  • 53
  • 96
6
votes
1 answer

Return meaningful errors in API using CQRS pattern

When using the Mediatr pattern I find quite challenging to return meaningful errors to an API controller. Let's take the OrdersController.CancelOrder method as an example (src). In this example, they "only" return Ok() and BadRequest(). In that case…
user4478810
6
votes
1 answer

Autofac, MediatR & multiple DLL projects

I have several (eventually 100+) small DLL projects all based on MediatR. This means that the interfaces in use are just the IMediatR interfaces (IRequest, IRequestHandler, TResult>). Since a lot of these do not have a…
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
6
votes
1 answer

Autofac resolves multiple instances of the same type

I have a slightly modified version of Mediatr handing command processing in my application. I have implemented a MediatorPipeline that allows me to have pre and post processors. public class AsyncMediatorPipeline :…
NotMyself
  • 29,209
  • 17
  • 56
  • 74
6
votes
3 answers

How do I use MediatR with Autofac in ASP MVC 5?

The author provides an example of how to use MediatR in a console application using Autofac: var builder = new ContainerBuilder(); builder.RegisterSource(new ContravariantRegistrationSource()); builder.RegisterAssemblyTypes(typeof…
Caster Troy
  • 2,796
  • 2
  • 25
  • 45
5
votes
2 answers

REST Patch in dotnet/meditr best practices

I have used PUT/POST to update the applications I'm working with, which has been straightforward until now. Now I'm working on an API that should expose a way to update data in a database, and my initial thought was that I could replace the row (or…
XRaycat
  • 1,051
  • 3
  • 16
  • 28
5
votes
1 answer

Is dependency injection and Mediatr in combination with Azure Functions inefficient?

I'm in early steps on my Azure journey. The way I understand functions is that they're a serverless solution that is only run when actually needed. After performing their logic they "shut down" again. This is cheaper compared to something like an…
yesman
  • 7,165
  • 15
  • 52
  • 117