ASP.NET Core MVC 新手入门到熟练 - 3. 应用MediatR

ASP.NET Core MVC 新手入门到熟练 - 3. 应用MediatR

自然环境:

  • .NET 5
  • ASP.NET Core MVC (project)

1. MediatR

MediatR .NET中的简易中介公司者方式完成,一种过程内消息传递体制(无别的外界依靠)。适用以同歩或多线程的方式开展要求/回应,指令,查看,通告和事件的消息传递,并根据C#泛型适用信息的智能化生产调度。

Simple mediator implementation in .NET
In-process messaging with no dependencies.
Supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.

另:中介公司者方式 - 界定一个中介公司目标来封裝一系列目标中间的互动,使原来目标中间的藕合疏松,且能够单独地更改他们中间的互动。中介公司者方式又叫调解方式,它是迪米特法则的典型性运用。

2. 安裝 & 配备

针对.NET5 (.net core), 应用nuget 安裝MediatR.Extensions.Microsoft.DependencyInjection.

配备:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddMediatR(typeof(Startup));
}

3. MediatR信息种类

3.1. Notifications 通告方式

Notifications 通告方式用以经营者推送通告,顾客(能够好几个)接受到通告后,开展事后解决。
例:一个APS.NET 网页页面,浏览时,推送Notifications通告;顾客简易纪录接到通告的時间。

3.1.1. 界定根据INotification的通告类

public class Ping : INotification { }

3.1.2. 界定顾客(关心通告的解决方式 )

public class Pong1 : INotificationHandler<Ping>
{
    public Task Handle(Ping notification, CancellationToken cancellationToken)
    {
        Debug.WriteLine($"Pong1, {DateTime.Now}");
        return Task.CompletedTask;
    }
}

public class Pong2 : INotificationHandler<Ping>
{
    public Task Handle(Ping notification, CancellationToken cancellationToken)
    {
        Debug.WriteLine($"Pong2, {DateTime.Now}");
        return Task.CompletedTask;
    }
}

3.1.3. 推送消息通知

// 根据dotnet core的依赖注入,引入IMediator目标
private readonly IMediator _mediator;
public HomeController(ILogger<HomeController> logger, IMediator mediator)
{
    _logger = logger;
    _mediator = mediator;
}


public async Task<IActionResult> IndexAsync()
{
    // e.g. 浏览主页时,推送通告
    await _mediator.Publish(new Ping());
    return View();
}

3.1.4. 輸出

Pong1, 5/27/2021 4:37:18 PM
Pong2, 5/27/2021 4:37:18 PM

3.2. Request/Response 要求回应方式

request/response用以指令和查看的情景。

3.2.1. 建立要求类:

public class RequestModel: IRequest<string>
{
}

3.2.2. 建立要求解决类

有别于通告方式,request/response只有有一个要求解决。

public class RequestHandeler : IRequestHandler<RequestModel, string>
{
    public Task<string> Handle(RequestModel request, CancellationToken cancellationToken)
    {
        return Task.FromResult($"Pong {DateTime.Now}"); // 检测,回到內容给request
    }
}

3.2.3. 网页页面中推送要求

private readonly ILogger<HomeController> _logger;
private readonly IMediator _mediator;

public HomeController(ILogger<HomeController> logger, IMediator mediator)
{
    _logger = logger;
    _mediator = mediator;
}

public async Task<IActionResult> IndexAsync()
{
    // send request, and show Response
    var response = await _mediator.Send(new RequestModel());
    Debug.WriteLine("Got response in controller: "  response);

    return View();
}

3.2.4. 輸出

Got response in controller: Pong 5/28/2021 2:04:26 PM

4. 汇总

  • MediatR是一种过程内消息传递体制
  • 适用以同歩或多线程的方式开展要求/回应,指令,查看(CQRS),通告和事件的消息传递,并根据C#泛型适用信息的智能化生产调度。
  • 其关键是信息的解耦。
  • 应用领域: 完成CQRS、EventBus等。

5. 参照 & 编码

  • 参照: https://GitHub.com/jbogard/MediatR
  • 编码: https://github.com/jackniu81/dotnet/tree/main/MediatR-Demo

评论(0条)

刀客源码 游客评论