SH.Mediator
2.0.0
dotnet add package SH.Mediator --version 2.0.0
NuGet\Install-Package SH.Mediator -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SH.Mediator" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SH.Mediator" Version="2.0.0" />
<PackageReference Include="SH.Mediator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SH.Mediator --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SH.Mediator, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SH.Mediator@2.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SH.Mediator&version=2.0.0
#tool nuget:?package=SH.Mediator&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
安装 NuGet 包
dotnet add package SHMediator
注册服务
using SHMediator;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddSHMediator(typeof(Program).Assembly, options =>
{
options.UseLoggingInterceptor = true;
options.UseValidationInterceptor = true;
});
三种 接口
1. IRequest -- IRequestHandler 请求无应答
2. IRequest<,> -- IRequestHandler<,> 请求有应答
3. INotification -- INotificationHandler 通知/广播
1. IRequest -- IRequestHandler
public class MyRequest : IRequest
{
public string Name { get; set; }
}
public class MyRequestHandler : IRequestHandler<MyRequest>
{
public Task Handle(MyRequest request, CancellationToken cancellationToken)
{
Console.WriteLine($"Hello, {request.Name}!");
return Task.CompletedTask;
}
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
await mediator.Send(new MyRequest { Name = "World" }); // 输出: Hello, World!
2. IRequest<,> -- IRequestHandler<,>
public class MyRequest : IRequest<MyResponse>
{
public string Name { get; set; }
}
public class MyResponse
{
public string Message { get; set; }
}
public class MyRequestHandler : IRequestHandler<MyRequest, MyResponse>
{
public Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(new MyResponse { Message = $"Hello, {request.Name}!" });
}
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
var response = await mediator.Send(new MyRequest { Name = "World" });
Console.WriteLine(response.Message); // 输出: Hello, World!
3. INotification -- INotificationHandler
public class MyNotify : INotification
{
public string Name { get; set; }
}
public class MyNotify01Handlder : INotificationHandler<MyNotify>
{
public Task Handle(MyNotify notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Notification 01 received: {notification.Name}");
return Task.CompletedTask;
}
}
public class MyNotify02Handlder : INotificationHandler<MyNotify>
{
public Task Handle(MyNotify notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Notification 02 received: {notification.Name}");
return Task.CompletedTask;
}
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new MyNotify { Name = "Test Notification" });
// 输出: Notification 01 received: Test Notification
// 输出: Notification 02 received: Test Notification
文档目录
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- FluentValidation.DependencyInjectionExtensions (>= 12.1.1)
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.