The 2 MediatR features people don't know about but should
Вставка
- Опубліковано 28 чер 2024
- Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will introduce you to the two MediatR feature that are lesser know compared to the flagship request, handler, response one. Those features are the notification support and the streaming support that MediatR has. The streaming side of things is very interesting because previously it could have been a dealbreaker but not it is possible to implement.
Workshops
NDC Oslo | 26 - 30 Sept | bit.ly/ndcoslo2022workshop
NDC Sydney | 10 - 14 Oct | bit.ly/ndcsydney2022workshop
dotnetdays | 20 - 22 Oct | bit.ly/dotnetdays2022workshop
NDC Minnesota | 15 - 18 Nov | bit.ly/ndcminnesota2022workshop
NDC London | 23-27 January 2023 | bit.ly/ndclondon2023
Timestamps
The flagship feature - 0:00
Notification support - 3:07
Streaming support - 8:30
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet #mediatr
Default sequential execution of notification handlers makes it use really simple for implementing an domain events bus with transactions support. With parallel execution (that you can implement with custom publish strategy), in case of domain event bus, it will require additional scopes for each handler and most likely some retries, so it be not that easy to implement and execution most likely will be fully asynchronous - without possibility to await completion of event execution (because of long retries)
Would love to see the streaming capabilities linked up to a JS frontend updating a store or a table or something
You can actully write a custom execution strategy for notifications, which allow execution in parallel
That's cool that MediatR added the streaming! I've been using IAsyncEnumerable (without MediattR) to stream large response sets back to the front-end in Blazor for a while now. Works great.
Been using Notification for more than a year. Its a great way to "simulate" internal Domain Events in your applications where different parts of the applications wants to know about this or having multiple integrations listening for events using the same interface.
exactly!! thats a great usecase and its possible to rollback the cause if the effect has not been applied.
In my opinion, this video is the best of all - but maybe it's because it fits perfectly with solving a problem that came up 4 hours ago at work - it's like you made it for ME.
Now I'm sad because, even though I know I'm biased, I'll never be able to feel such bliss watching your videos again.
Great video! What's the actual use case of IAsyncEnumerable and when would you use it compared to maybe SignalR?
Great video thanks. I didn't know the streaming support.
As for the Publish, I knew it and used it. In my case, I put side operations into the handlers that are written later. In these handlers, these were put into a background job queue so it doesn't hurt the performance or stability of the main operation.
Great Video. Thank you Nick for your permanent great work 👍 Always up to date and interesting
Way to go. Thanks for bringing these scenarios to light and the gotcha's associated
Sequential execution of the notification handlers is useful when using INotification as domain events and you're using things like EF that are not thread-safe, so it really works well for those cases...
But yeah, it should be some optionally configurable thing in the library...
Wouh, the streaming!!
I knew about the notification but not of the fact that it's sequential :o!
Thanks NICK!
That's really cool! I can see some use cases for the notification feature, if there is a specific order on how they are sent but I doubt that is the case.
Awesome content as always, keep it up! :)
Great video! I pretty like streaming capability of MediatR.
Thanks!
Why don't you use records more often in your videos? You often create classes with a ctor and get only properties, but records remove all that boilerplate.
Because records don’t play nice with several packages and I don’t wanna point people to potentially something that won’t work.
@@nickchapsas Understandable, though I haven't come across any issues myself and I use them wherever appropriate.
months ago i didn't know shit about CQRS, i spent the last weeks trying MediatR and it's very fucking powerful, and now streaming capabilities
I really like the notifications aspect of it. Jason Taylor uses this pretty extensively in his clean architecture template.
Great video. Thanks!
Multi-casting notifications is pretty much fundamental to an event sourcing + CQRS architecture. I've done this long before MediatR was a thing, but it's always nice to have a framework that does this out of the box.
Thanks for the great video, Nick!
I've tried the notification feature for Domain Events recently and it worked great (however it was a relatively small use-case).
What are your concerns about it or maybe alternatives?
I'm interested in the concerns and alternatives as well
I once asked the MediatR guys why they used sequential execution as default. The answer was, that mostly there's something like EFCore involved in most projects which is not threadsafe. Makes sense to me.
Which is such a bad reason
Looking forward to the IAsyncEnumerable video ;)
Awesome content! Thanks .. Cant wait to see your video on IAsyncEnumerable .. Could you please explain how EF Core works with IAsyncEnumerable? Thanks
The quick note at the end about alts for notifications being better was interesting. Do you mean something like MassTransit’s in memory mediator? What gaps to you the the MediatR package has
8:10 mostly because of thinks like Entity Framework, what not allow multiple request at the same time, and that's if you know that you can override publisher method to run as you want
EF supported multithreaded contexts
@@nickchapsas
As default? I dont think
But always we could use trancient scope or other solutions
But each of method have some disadvantages
For the same reason MediatR use method that have the lowest probability to get some 'strange error'
@@nickchapsas As far as I know and understand from the MS documentation it is indeed not safe and have to agree with KondzioSSJ4.
this must probably my 20th time trying to comment so I am not adding any links etc.
I use record types for my requests and notifications. It is much cleaner :) Also, I use notifications in our app. If Jimmy changed the behavior to run them in parallel it would break our app. The reason for that decision is because you are not supposed to spawn multiple threads in a web request. Lots of libraries will break if you try to, for example EF Core. I agree that it would be nice if there were a new mechanism added that supported this for when you are wanting to use mediatr outside of a web scenario. Each notification handler would need to run within it's own lifetime scope and be treated as a separate unit of work though.
You're mixing up the concepts of parallelism and concurency, and you're wrong. Executing handlers at the concurrently doesn't mean spawning multiple threads (you could do that but it would be a classic mistake). For example refer to Task.WhenAll, which doesn't spawn threads.
The EF Core use case has nothing to do with parallelism as well - DbContext just doesn't support concurrency and throws. There is a way around that btw, which would be injecting a factory.
Looking forward to your IAsyncEnumerable video
Streams is a really coo feature. I am using Notifications sometimes but I often fire and forget them so they are like events. I just use _ = mediator.Publish(...); and not await them
I had also used it in the same way. Just that would need create scope and service explicitly in the handlers to avoid context has been destroyed situation. Not sure if any drawbacks though
Hi, Nick! Thx for your video. What do you think about Masstransit? And what do you think about use Masstransit like a Message and Consumers pattern thithout MediatR?
Very nice vid… how do you find out if a feature is being used or not? Is there a way to search people’s repositories on git??
Thanks for video! I have a question: if I want to subscribe a notification from front - end, should I make a request and then, wait for response?
Nick, can you recommend a better alternative for the mediators notification feature? Thank you
Notifications can run in parallel in latest version of mediatr 🎉🎉🎉
we use notifications for publishing messages to rabbitmq (eg same notification from various parts of service)
Can you made a video how to handle Stream on some Front End technology ? Thank you for the great video's.
How does this compare to SignalR in terms of use cases? I've previously implemented SignalR for sending event-driven messages and updates to a web application. As I've understood it, MediatR notification support is useful when clients are polling for updates, while SignalR is useful for pushing updates out to subscribing clients - do you agree?
SignalR is for web apps, MediatR is "backend-only" and in-process messaging.
Does mediatr support observables in system.reactive?
Hi @Nick
This is a little off topic question. I saw yout fast-endpoints video and this. Do you think it makes to sense to use fast-endpoint with MediatR or it will be over engineering.
I suppose you should not even wait for the notifications to be published... Once you send them is someone ekse responsibility to do something with them, it should be a fire and forfet thing... therefore removing the await will save you from waiting 5 seconds. Or is there a reason to await it? Thanks in advance
I prefer ReactiveExtensions to send 'notifications'. I can subscribe on separate threads, etc.
Hey Nick do you have any idea how to accomplish MediatR + Redis + MongoDB with filtering, search & pagination, since redis only support querying by IDs, is there any way to do this elegantly
System.Reactive with observable could possibly be a super elegant solution here.
is there a way to excute notifications in the background as a background job or something and not wait on it?!
A shame u can't run notifications in another thread. For my use case I generally use notifications to abstract event sending to a queue and then implement real work. Sadly u can't do any long running or real work with notifications (I. E sending emails, or some image manipulation due to the delay) so I have a handler to send to a queue and then do real work in whatever listens to messages in the queue. Not ideal but works nicely for my needs. I've also used notifications for domain events.
I love notifications
You had me excited until the non parallel notifications.
running handlers in parallel is not always a good idea. often they contain a call to the database, and this leads to errors, because DbContext is not thread safe
DbContext can be parallelised. Hamstringing your design because of poor db sdk features is bad
Why do you have to await the Notification? Why not just fire and forget?
Will that keep the request thread from returning to the pool?
-20? the people live at -58 in my country ))), I lived where +58 for a whole month
Please Nick, don't name your videos like news articles! You are better than that! Name it as it should for a better understanding and not being like click baits.
I am really not better than that. Videos unfortunately need to be named like that to get the appropriate click through. This is how the game is played
MeditatR is the greatest abomination the world has ever seen, every time I see a video being made on it, I die a little bit inside...
Don't like wine? don't drink it! Don't like cheese? don't eat it!
If you don't like it... then f-ing don't watch.
Obviously 74 million downloads, infers that quite a bit of people do like it and they have their personal reasons for it.
@@paulkoopmans4620 It's not a good analogy. If I don't like wine and like beer - I choose what I want. But if I'm in a company which drinks wine only I can't drink beer - I just need to find a new company. And the more popular wine is - the more companies drink it, more wine is produced to the market whereas beer marketing becomes less and less. And eventually I'm forced to drink wine like others or be alone (or with others who don't like wine).
@@paulkoopmans4620 Are you copy pasting your response to everyone who has a different opinion on mediatr than you?
@@paulkoopmans4620 your response is as shallow as the original message, neither of you are posting any actual arguments for/against MediatR
@kalistrat It's ok that you don't like the analogy but what you are picking up from it is exactly what I meant; so in that sense it's good with me.
I was exactly trying to convey that message. I am not here to convince then to use it. If they don't like it it is fine by me.
To attack Nick that he should not pay attention to it is what I have problems with. He clearly likes it, which is why he makes the videos about it. He is giving his arguments and perspectives.
Like other people commenting on other videos like .. I wish I could use .net6 I am stuck with 4.8. Really there is only one person you can "blame" for your personal professional development; and it's you. If you are working in a company where you are stuck on old tech or where they are using stuff you don't like... move on.
I am sure one could find a company with peers that think alike.
Are mediatr passing you to promote their garbage? You're doing your community a disservice by promoting it at every turn. Just stop.
Don't like wine? don't drink it! Don't like cheese? don't eat it!
If you don't like it... then f-ing don't watch.
Obviously 74 million downloads, infers that quite a bit of people do like it and they have their personal reasons for it.
@@paulkoopmans4620 Yes, it's quite overused and have ruined many a codebase.
@@spirits_ OK. Look... I appreciate your view. I can see that you might have seen usages of MediatR that have ruined several codebases. That does not take away the fact that many other might have opposite experiences.
Nick has been open and honest when he had sponsored content. Based on that I think it is not a question whether or not he gets paid for promoting MediatR.
That leaves us with one of two options. He either likes it or personally has no strong opinion on MediatR but recognizes that likely a big portion of his viewers do care about it and he wants to make content for them.
Nick is not able to please every viewer. There has been videos in the past that I do not necessarily agree with him. I will, in those cases, never put any of these derogatory comments. His channel, his choices, his content.
If one adds a good comment with a legit question about speed or something or you can share actual links or something that helps us all... that is what we are looking for.
@@spirits_ You can literally say this about any technology though - the thing that ruins codebases is rarely the technology itself, but rather how it is used by the developers. I could say the same thing about React, Java, or SQL (or even C#, to be frank) because I've seen extremely poor implementations of each, but that doesn't mean that any of those tools is inherently bad or shouldn't be discussed. Just that the group of people who worked on that code base didn't know how to use the tool properly.
@@AJax2012 nailed it.