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

КОМЕНТАРІ • 81

  • @user-nq8ln7ps7x
    @user-nq8ln7ps7x Рік тому +25

    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)

  • @acidrazor
    @acidrazor Рік тому +36

    Would love to see the streaming capabilities linked up to a JS frontend updating a store or a table or something

  • @prom3theu581
    @prom3theu581 Рік тому +5

    You can actully write a custom execution strategy for notifications, which allow execution in parallel

  • @brianm1864
    @brianm1864 Рік тому +11

    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.

  • @carstenberggreen7509
    @carstenberggreen7509 Рік тому +8

    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.

    • @abhinavkapoor321
      @abhinavkapoor321 Місяць тому

      exactly!! thats a great usecase and its possible to rollback the cause if the effect has not been applied.

  • @tofu1687
    @tofu1687 Рік тому +2

    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.

  • @PelFox
    @PelFox Рік тому +5

    Great video! What's the actual use case of IAsyncEnumerable and when would you use it compared to maybe SignalR?

  • @homeropata
    @homeropata Рік тому

    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.

  • @alexanderbadewitz1288
    @alexanderbadewitz1288 Рік тому

    Great Video. Thank you Nick for your permanent great work 👍 Always up to date and interesting

  • @nove1398
    @nove1398 Рік тому

    Way to go. Thanks for bringing these scenarios to light and the gotcha's associated

  • @CesarDemi81
    @CesarDemi81 Рік тому +1

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

  • @codewkarim
    @codewkarim Рік тому

    Wouh, the streaming!!
    I knew about the notification but not of the fact that it's sequential :o!
    Thanks NICK!

  • @andreast.1373
    @andreast.1373 Рік тому

    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! :)

  • @branislavpetrovic7486
    @branislavpetrovic7486 Рік тому

    Great video! I pretty like streaming capability of MediatR.
    Thanks!

  • @tera.
    @tera. Рік тому +10

    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.

    • @nickchapsas
      @nickchapsas  Рік тому +6

      Because records don’t play nice with several packages and I don’t wanna point people to potentially something that won’t work.

    • @tera.
      @tera. Рік тому +1

      @@nickchapsas Understandable, though I haven't come across any issues myself and I use them wherever appropriate.

  • @fifty6737
    @fifty6737 Рік тому +1

    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

  • @dayv2005
    @dayv2005 Рік тому

    I really like the notifications aspect of it. Jason Taylor uses this pretty extensively in his clean architecture template.

  • @KonstantinKuda
    @KonstantinKuda Рік тому

    Great video. Thanks!

  • @marcusmajarra
    @marcusmajarra Рік тому

    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.

  • @ilyakurmaz
    @ilyakurmaz Рік тому +6

    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?

    • @DerekWelton
      @DerekWelton Рік тому

      I'm interested in the concerns and alternatives as well

  • @thomai5287
    @thomai5287 Рік тому +1

    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.

  • @pablocom
    @pablocom Рік тому +1

    Looking forward to the IAsyncEnumerable video ;)

  • @mustafizurrohman88
    @mustafizurrohman88 Рік тому +1

    Awesome content! Thanks .. Cant wait to see your video on IAsyncEnumerable .. Could you please explain how EF Core works with IAsyncEnumerable? Thanks

  • @pdevito
    @pdevito Рік тому +2

    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

  • @kondziossj3
    @kondziossj3 Рік тому +2

    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

    • @nickchapsas
      @nickchapsas  Рік тому

      EF supported multithreaded contexts

    • @kondziossj3
      @kondziossj3 Рік тому

      @@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'

    • @paulkoopmans4620
      @paulkoopmans4620 Рік тому +2

      ​@@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.

  • @ChristopherHaws90
    @ChristopherHaws90 Рік тому

    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.

    • @chris-pee
      @chris-pee Рік тому

      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.

  • @steve-wright-uk
    @steve-wright-uk Рік тому +1

    Looking forward to your IAsyncEnumerable video

  • @antonmartyniuk
    @antonmartyniuk Рік тому +1

    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

    • @cwq704
      @cwq704 Рік тому

      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

  • @techbuterbrod
    @techbuterbrod Рік тому

    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?

  • @expertreviews1112
    @expertreviews1112 Рік тому

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

  • @techman6713
    @techman6713 Рік тому

    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?

  • @MXDMND_
    @MXDMND_ Рік тому

    Nick, can you recommend a better alternative for the mediators notification feature? Thank you

  • @SibTiger33
    @SibTiger33 Рік тому

    Notifications can run in parallel in latest version of mediatr 🎉🎉🎉

  • @blackbird700
    @blackbird700 Рік тому

    we use notifications for publishing messages to rabbitmq (eg same notification from various parts of service)

  • @iliqngorki3310
    @iliqngorki3310 Рік тому

    Can you made a video how to handle Stream on some Front End technology ? Thank you for the great video's.

  • @carl-henrikkristoffersen2313
    @carl-henrikkristoffersen2313 Рік тому +2

    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?

    • @krccmsitp2884
      @krccmsitp2884 Рік тому +1

      SignalR is for web apps, MediatR is "backend-only" and in-process messaging.

  • @camoman1000
    @camoman1000 Рік тому

    Does mediatr support observables in system.reactive?

  • @TheAzerue
    @TheAzerue Рік тому

    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.

  • @lucademarco5969
    @lucademarco5969 Рік тому +1

    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

  • @NiamorH
    @NiamorH Рік тому

    I prefer ReactiveExtensions to send 'notifications'. I can subscribe on separate threads, etc.

  • @fifty6737
    @fifty6737 Рік тому

    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

    • @camoman1000
      @camoman1000 Рік тому +3

      System.Reactive with observable could possibly be a super elegant solution here.

  • @AhmedMohammed23
    @AhmedMohammed23 3 місяці тому

    is there a way to excute notifications in the background as a background job or something and not wait on it?!

  • @SibTiger33
    @SibTiger33 Рік тому

    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.

  • @codeforme8860
    @codeforme8860 Рік тому

    I love notifications

  • @chrislamont7529
    @chrislamont7529 Рік тому

    You had me excited until the non parallel notifications.

  • @ZhenyaKolobov
    @ZhenyaKolobov Рік тому

    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

    • @nickchapsas
      @nickchapsas  Рік тому +2

      DbContext can be parallelised. Hamstringing your design because of poor db sdk features is bad

  • @alexisfibonacci
    @alexisfibonacci Рік тому

    Why do you have to await the Notification? Why not just fire and forget?

    • @pilotboba
      @pilotboba Рік тому

      Will that keep the request thread from returning to the pool?

  • @spacerecluse
    @spacerecluse Рік тому

    -20? the people live at -58 in my country ))), I lived where +58 for a whole month

  • @speakoutloud7293
    @speakoutloud7293 Рік тому +1

    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.

    • @nickchapsas
      @nickchapsas  Рік тому +1

      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

  • @amirhosseinahmadi3706
    @amirhosseinahmadi3706 Рік тому +8

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

    • @paulkoopmans4620
      @paulkoopmans4620 Рік тому +5

      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.

    • @nanvlad
      @nanvlad Рік тому +4

      @@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).

    • @spirits_
      @spirits_ Рік тому +1

      @@paulkoopmans4620 Are you copy pasting your response to everyone who has a different opinion on mediatr than you?

    • @SvMsk
      @SvMsk Рік тому

      @@paulkoopmans4620 your response is as shallow as the original message, neither of you are posting any actual arguments for/against MediatR

    • @paulkoopmans4620
      @paulkoopmans4620 Рік тому +1

      ​ @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.

  • @smiffy68
    @smiffy68 Рік тому +5

    Are mediatr passing you to promote their garbage? You're doing your community a disservice by promoting it at every turn. Just stop.

    • @paulkoopmans4620
      @paulkoopmans4620 Рік тому +5

      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.

    • @spirits_
      @spirits_ Рік тому +1

      @@paulkoopmans4620 Yes, it's quite overused and have ruined many a codebase.

    • @paulkoopmans4620
      @paulkoopmans4620 Рік тому +3

      @@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.

    • @AJax2012
      @AJax2012 Рік тому +2

      @@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.

    • @muhamedkarajic
      @muhamedkarajic Рік тому +1

      @@AJax2012 nailed it.