You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mediatr.md 3.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ---
  2. uid: Guides.OtherLibs.MediatR
  3. title: MediatR
  4. ---
  5. # Configuring MediatR
  6. ## Prerequisites
  7. - A simple bot with dependency injection configured
  8. ## Downloading the required packages
  9. You can install the following packages through your IDE or go to the NuGet link to grab the dotnet cli command.
  10. |Name|Link|
  11. |--|--|
  12. | `MediatR` | [link](https://www.nuget.org/packages/MediatR) |
  13. | `MediatR.Extensions.Microsoft.DependencyInjection` | [link](https://www.nuget.org/packages/MediatR.Extensions.Microsoft.DependencyInjection)|
  14. ## Adding MediatR to your dependency injection container
  15. Adding MediatR to your dependency injection is made easy by the `MediatR.Extensions.Microsoft.DependencyInjection` package. You can use the following piece of code to configure it. The parameter of `.AddMediatR()` can be any type that is inside of the assembly you will have your event handlers in.
  16. [!code-csharp[Configuring MediatR](samples/MediatrConfiguringDI.cs)]
  17. ## Creating notifications
  18. The way MediatR publishes events throughout your applications is through notifications and notification handlers. For this guide we will create a notification to handle the `MessageReceived` event on the `DiscordSocketClient`.
  19. [!code-csharp[Creating a notification](samples/MediatrCreatingMessageNotification.cs)]
  20. ## Creating the notification publisher / event listener
  21. For MediatR to actually publish the events we need a way to listen for them. We will create a class to listen for discord events like so:
  22. [!code-csharp[Creating an event listener](samples/MediatrDiscordEventListener.cs)]
  23. The code above does a couple of things. First it receives the DiscordSocketClient from the dependency injection container. It can then use this client to register events. In this guide we will be focusing on the MessageReceived event. You register the event like any ordinary event, but inside of the handler method we will use MediatR to publish our event to all of our notification handlers.
  24. ## Adding the event listener to your dependency injection container
  25. To start the listener we have to call the `StartAsync()` method on our `DiscordEventListener` class from inside of our main function. To do this, first register the `DiscordEventListener` class in your dependency injection container and get a reference to it in your main method.
  26. [!code-csharp[Starting the event listener](samples/MediatrStartListener.cs)]
  27. ## Creating your notification handler
  28. MediatR publishes notifications to all of your notification handlers that are listening for a specific notification. We will create a handler for our newly created `MessageReceivedNotification` like this:
  29. [!code-csharp[Creating an event listener](samples/MediatrMessageReceivedHandler.cs)]
  30. The code above implements the `INotificationHandler<>` interface provided by MediatR, this tells MediatR to dispatch `MessageReceivedNotification` notifications to this handler class.
  31. > [!NOTE]
  32. > You can create as many notification handlers for the same notification as you desire. That's the beauty of MediatR!
  33. ## Testing
  34. To test if we have successfully implemented MediatR, we can start up the bot and send a message to a server the bot is in. It should print out the message we defined earlier in our `MessageReceivedHandler`.
  35. ![MediatR output](images/mediatr_output.png)
  36. ## Adding more event types
  37. To add more event types you can follow these steps:
  38. 1. Create a new notification class for the event. it should contain all of the parameters that the event would send. (Ex: the `MessageReceived` event takes one `SocketMessage` as an argument. The notification class should also map this argument)
  39. 2. Register the event in your `DiscordEventListener` class.
  40. 3. Create a notification handler for your new notification.