MediatR Guide + sample (#2218)
* Add guide for MediatR * Add sample for MediatR * Fix exposed token in program.cs * Fix review points * Remove newline in MediatrDiscordEventListener.cs
This commit is contained in:
1
docs/guides/other_libs/samples/MediatrConfiguringDI.cs
Normal file
1
docs/guides/other_libs/samples/MediatrConfiguringDI.cs
Normal file
@@ -0,0 +1 @@
|
||||
.AddMediatR(typeof(Bot))
|
||||
@@ -0,0 +1,16 @@
|
||||
// MessageReceivedNotification.cs
|
||||
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
|
||||
namespace MediatRSample.Notifications;
|
||||
|
||||
public class MessageReceivedNotification : INotification
|
||||
{
|
||||
public MessageReceivedNotification(SocketMessage message)
|
||||
{
|
||||
Message = message ?? throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
public SocketMessage Message { get; }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// DiscordEventListener.cs
|
||||
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
using MediatRSample.Notifications;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatRSample;
|
||||
|
||||
public class DiscordEventListener
|
||||
{
|
||||
private readonly CancellationToken _cancellationToken;
|
||||
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IServiceScopeFactory _serviceScope;
|
||||
|
||||
public DiscordEventListener(DiscordSocketClient client, IServiceScopeFactory serviceScope)
|
||||
{
|
||||
_client = client;
|
||||
_serviceScope = serviceScope;
|
||||
_cancellationToken = new CancellationTokenSource().Token;
|
||||
}
|
||||
|
||||
private IMediator Mediator
|
||||
{
|
||||
get
|
||||
{
|
||||
var scope = _serviceScope.CreateScope();
|
||||
return scope.ServiceProvider.GetRequiredService<IMediator>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
_client.MessageReceived += OnMessageReceivedAsync;
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task OnMessageReceivedAsync(SocketMessage arg)
|
||||
{
|
||||
return Mediator.Publish(new MessageReceivedNotification(arg), _cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// MessageReceivedHandler.cs
|
||||
|
||||
using System;
|
||||
using MediatR;
|
||||
using MediatRSample.Notifications;
|
||||
|
||||
namespace MediatRSample;
|
||||
|
||||
public class MessageReceivedHandler : INotificationHandler<MessageReceivedNotification>
|
||||
{
|
||||
public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine($"MediatR works! (Received a message by {notification.Message.Author.Username})");
|
||||
|
||||
// Your implementation
|
||||
}
|
||||
}
|
||||
4
docs/guides/other_libs/samples/MediatrStartListener.cs
Normal file
4
docs/guides/other_libs/samples/MediatrStartListener.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
// Program.cs
|
||||
|
||||
var listener = services.GetRequiredService<DiscordEventListener>();
|
||||
await listener.StartAsync();
|
||||
Reference in New Issue
Block a user