I have tried the CQRS pattern using MediatR and am loving the clean state in which applications am working on are transforming. In all the examples i have seen and used, I always do
await Mediator.Send(command);
It's been same for the queries as well
var data = await Mediator.Send(queryObject);
I just realized there's Mediator.Publish as well which after searching seems to me to be doing the same thing. I am trying to understand what the difference between the Mediator.Send and Mediator.Publish is. I have read the MediatR library docs and I still don't understand what the difference between these are. Kindly help me understand the difference.
Thanks for your help
MediatR Requests are very simple request-response style messages, where a single request is synchronously handled by a single handler (synchronous from the request point of view, not C# internal async/await).
Most of the time it's used like a glorified Service Locator, which is notoriously an anti-pattern. Actually, no, it's never used as a service locator. No one uses the IMediator interface to locate services. They use it to dispatch requests and notifications to handlers.
Mediatr notification handlers are already fire and forget. Another way to deal with this would be to queue a background task to do the work. Look at something like Hangfire and implement that in either a _mediator.
Source from: Microsoft — Command and Query Responsibility Segregation (CQRS) pattern. MediatR is an open source implementation of the mediator pattern that doesn't try to do too much and performs no magic. It allows you to compose messages, create and listen for events using synchronous or asynchronous patterns.
MediatR facilitates CQRS and Mediator patterns in .NET. It is a low-ambition library trying to solve a simple problem — decoupling the in-process sending of messages from handling messages. MediatR supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.
Using the MediatR package, one sends some data as an object to the mediator object. Depending on the type of data that is sent to the mediator object, it decides which other objects/services to call. MediatR handles two forms of message: Requests - can only send messages to one other object/service.
The first thing we need to do is install the MediatR nuget package. So from your package manager console run : We also need to install a package that allows us to use the inbuilt IOC container in .NET Core to our advantage (We’ll see more of that shortly). So also install the following package : Finally we open up our startup.cs file.
Using Mediat R, one sends some data as an object to a mediator object. Depending on the type of data that is sent, it decides which other objects/services to call. To send a request, you need to create a request and a request handler. Fullstack .NET and JavaScript web developer. Coding teacher and advocate
MediatR has two kinds of messages it dispatches:
- Request/response messages, dispatched to a single handler
- Notification messages, dispatched to multiple handlers
Send
may return a response, but do not have to do it.Publish
never return the result.You are sending requests (sometimes called commands) by _mediator.Send({command})
to exactly one concrete handler. It may be e.g. command that saves a new product to the database. It is often a request from the user (frontend/API) or sometimes it may be internal command in your system given by other service in a synchronous way. It is always expected that the command will be executed immediately and you will receive some proper result or error to immediately inform the client about some failures.
You are publishing notifications (often called events) by _mediator.Publish({event})
to zero, one or many handlers. You used notifications when you want to publish some information and you do not know who needs that. E.g. NewProductEvent
which is published after successfully adding product to your Warehouse Module. Few other contexts want to subscribe the information and e.g. send email to a client that a new product is available or create some default configuration for the product in your Store Module (which payment and delivery are available for the product). You may use notifications in a synchronous way. All data will be saved in one transaction (product and store configuration) or you may use some async pattern with service bus or/and sagas. In the second case (asynchronous) you must manually handle cases when something wrong happens in other services or contexts which subscribe to your notification.
Example scenario: Default configuration was not created.
A good example of using mediatR you will find in e.g. Ordering microservice in EShopOnContainers by Microsoft: github. You will see an example usage of CQRS and DDD with EF core and ASP Net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With