前言

简单整理一下Mediator。

正文

Mediator 名字是中介者的意思。

那么它和中介者模式有什么关系呢?前面整理设计模式的时候,并没有去介绍具体的中介者模式的代码实现。

如下:

https://www.cnblogs.com/aoximin/p/13600464.html

之所以没写代码就是因为它现在是一种思想,以前的简单的已经很难满足我们现在开发的需求了。

那么看下Mediator 是如何做的吧。

Mediator 这个服务,是如何让我们的命令查询职责分离的。

首先安装一下包:

Mediator 核心接口为:

  1. IMediator

  2. IRequest,IRequest

  3. IRequestHandler<in IRequest,TResponse>

一般从接口就能看到其设计思想,其余的实现,各有各的想法,那么就来看下IMediator吧。

/// <summary>
/// Defines a mediator to encapsulate request/response and publishing interaction patterns
/// </summary>
public interface IMediator : ISender, IPublisher
{
}

这个接口的作用是定义了一个中介者,这个中介者用来装入请求或者响应和实现一些交互模式。

那么看来分别就是ISender和IPublisher来实现了。

看下ISender:

/// <summary>
/// Send a request through the mediator pipeline to be handled by a single handler.
/// </summary>
public interface ISender
{
/// <summary>
/// Asynchronously send a request to a single handler
/// </summary>
/// <typeparam name="TResponse">Response type</typeparam>
/// <param name="request">Request object</param>
/// <param name="cancellationToken">Optional cancellation token</param>
/// <returns>A task that represents the send operation. The task result contains the handler response</returns>
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default); /// <summary>
/// Asynchronously send an object request to a single handler via dynamic dispatch
/// </summary>
/// <param name="request">Request object</param>
/// <param name="cancellationToken">Optional cancellation token</param>
/// <returns>A task that represents the send operation. The task result contains the type erased handler response</returns>
Task<object?> Send(object request, CancellationToken cancellationToken = default);
}

这个接口用来通过由单个处理程序的中介者管道中发送请求。

IPublisher:

/// <summary>
/// Publish a notification or event through the mediator pipeline to be handled by multiple handlers.
/// </summary>
public interface IPublisher
{
/// <summary>
/// Asynchronously send a notification to multiple handlers
/// </summary>
/// <param name="notification">Notification object</param>
/// <param name="cancellationToken">Optional cancellation token</param>
/// <returns>A task that represents the publish operation.</returns>
Task Publish(object notification, CancellationToken cancellationToken = default); /// <summary>
/// Asynchronously send a notification to multiple handlers
/// </summary>
/// <param name="notification">Notification object</param>
/// <param name="cancellationToken">Optional cancellation token</param>
/// <returns>A task that represents the publish operation.</returns>
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
where TNotification : INotification;
}

这个接口用来通过多个处理程序的中介者管道中发送通知或者事件。

好了,现在我们得到的信息有"发布"、"事件"、"请求"、"管道" 这几个名词了。

那么实际上我们也能大致的猜出它是怎么实现的。

接下来查看IRequest:

/// <summary>
/// Marker interface to represent a request with a void response
/// </summary>
public interface IRequest : IRequest<Unit> { } /// <summary>
/// Marker interface to represent a request with a response
/// </summary>
/// <typeparam name="TResponse">Response type</typeparam>
public interface IRequest<out TResponse> : IBaseRequest { } /// <summary>
/// Allows for generic type constraints of objects implementing IRequest or IRequest{TResponse}
/// </summary>
public interface IBaseRequest { }

这些上面的英文已经提示了,标志性作用,用来做定义的。

最后来看下IRequestHandler接口:

/// <summary>
/// Defines a handler for a request
/// </summary>
/// <typeparam name="TRequest">The type of request being handled</typeparam>
/// <typeparam name="TResponse">The type of response from the handler</typeparam>
public interface IRequestHandler<in TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
/// <summary>
/// Handles a request
/// </summary>
/// <param name="request">The request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Response from the request</returns>
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}

现在我们在来整理一下名词:"发布"、"事件"、"请求"、"管道" 、"处理请求"

那么大概猜测大概通过接口来处理请求,然后还可以发布事件处理的。处理请求有处理请求的管道,且这个处理是单个处理程序,而处理事件是多个处理程序。

接下来操作一遍:

async static Task Main(string[] args)
{
var services = new ServiceCollection(); services.AddMediatR(typeof(Program).Assembly); var serviceProvider = services.BuildServiceProvider(); var mediator = serviceProvider.GetService<IMediator>(); await mediator.Send(new SelfCommand{ CommandName ="zhangsan"});
} internal class SelfCommand : IRequest<long>
{
public string CommandName { get; set; }
} internal class SelfCommandHandler : IRequestHandler<SelfCommand, long>
{
public Task<long> Handle(SelfCommand request, CancellationToken cancellationToken)
{
Console.WriteLine($"处理 {nameof(SelfCommand)}请求:{request.CommandName}");
return Task.FromResult(10L);
}
}

这里就有一个疑问了,为啥SelfCommandHandler会自动称为处理程序?我们并没有设置啊。

那么就来看一下send在干什么吧:

public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
} var requestType = request.GetType(); var handler = (RequestHandlerWrapper<TResponse>)_requestHandlers.GetOrAdd(requestType,
t => (RequestHandlerBase)Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse)))); return handler.Handle(request, cancellationToken, _serviceFactory);
}

上面可以看到生成了一个handle,然后调用了Handle 方法。

然后进RequestHandlerWrapperImpl 查看:

internal class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWrapper<TResponse>
where TRequest : IRequest<TResponse>
{
public override Task<object?> Handle(object request, CancellationToken cancellationToken,
ServiceFactory serviceFactory)
{
return Handle((IRequest<TResponse>)request, cancellationToken, serviceFactory)
.ContinueWith(t =>
{
if (t.IsFaulted)
{
ExceptionDispatchInfo.Capture(t.Exception.InnerException).Throw();
}
return (object?)t.Result;
}, cancellationToken);
} public override Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
ServiceFactory serviceFactory)
{
Task<TResponse> Handler() => GetHandler<IRequestHandler<TRequest, TResponse>>(serviceFactory).Handle((TRequest) request, cancellationToken); return serviceFactory
.GetInstances<IPipelineBehavior<TRequest, TResponse>>()
.Reverse()
.Aggregate((RequestHandlerDelegate<TResponse>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next))();
}
}

这里埋一个坑,因为看了一下,有很多细节的地方比如说Activator.CreateInstance的机制、一些管道细节,还设计到注册IPipelineBehavior<TRequest, TResponse>的实现类的机制,整理到该系列的细节篇中,将会比较详细的介绍。

现在只需要看Handle的ContinueWith,如果失败的话,那么会返回一个异常,否则返回结果。

然后根据上面的,我们指定Request对应的RequstHandle 必须名字有如下规律:如SelfCommand,只需要SelfCommand加长一些即可,比如说SelfCommandHandler,比如说SelfCommandHandlerV2,还可以SelfCommand2都行。

但是呢,最好改好名字,后面加Handle。

同时,如果我们写两个SelfCommandHandler和SelfCommandHandlerV2,那么是否两个都会执行?不是的,只会执行一个。

internal class SelfCommandHandler2 : IRequestHandler<SelfCommand, long>
{
public Task<long> Handle(SelfCommand request, CancellationToken cancellationToken)
{
Console.WriteLine($"处理 {nameof(SelfCommand)} V2请求:{request.CommandName}");
return Task.FromResult(10L);
}
} internal class SelfCommandHandler : IRequestHandler<SelfCommand, long>
{
public Task<long> Handle(SelfCommand request, CancellationToken cancellationToken)
{
Console.WriteLine($"处理 {nameof(SelfCommand)}请求:{request.CommandName}");
return Task.FromResult(10L);
}
}

比如说这样,那么会执行。

也就是说会执行SelfCommandHandler2。

那么请求就算介绍完了,那么看下事件。

调用事件这样调用即可:

 await mediator.Publish(new SelfEvent { EventName = "SelfEvent" });

具体类:

internal class SelfEvent : INotification
{
public string EventName { get; set; }
} internal class SelfEventHandler : INotificationHandler<SelfEvent>
{
public Task Handle(SelfEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine($"SelfEventHandler 执行:{notification.EventName}"); return Task.CompletedTask;
}
} internal class SelfEventHandlerV2 : INotificationHandler<SelfEvent>
{
public Task Handle(SelfEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine($"SelfEventHandlerV2 执行:{notification.EventName}"); return Task.CompletedTask;
}
}

效果:

那么和requst不同的是,注册几个就会调用几个。

好了现在回到中介者模式中来。

中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。

那么Mediator 这个模块呢,帮助我们解决了request和requesthandle之间的耦合,和 Event与EventHandle 之间的耦合。

一开始我认为是命令模式,后来一想,命令模式解决“行为请求者”与“行为实现者”的耦合。

命令模式如下:

https://www.cnblogs.com/aoximin/p/13616558.html

上面只是命令模式的一种形式哈。

下一节领域事件的处理。以上只是个人整理,如有错误,望请指点。

重新整理 .net core 实践篇—————Mediator实践[二十八]的更多相关文章

  1. 重新整理 .net core 实践篇—————领域事件[二十九]

    前文 前面整理了仓储层,工作单元模式,同时简单介绍了一下mediator. 那么就mediator在看下领域事件启到了什么作用吧. 正文 这里先注册一下MediatR服务: // 注册中间者:Medi ...

  2. 重新整理 .net core 实践篇—————异常中间件[二十]

    前言 简单介绍一下异常中间件的使用. 正文 if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } 这样写入中间件哈,那么在env环 ...

  3. 重新整理 .net core 实践篇————cookie 安全问题[三十八]

    前言 简单整理一下cookie的跨站攻击,这个其实现在不常见,因为很多公司都明确声明不再用cookie存储重要信息,不过对于老站点还是有的. 正文 攻击原理: 这种攻击要达到3个条件: 用户访问了我们 ...

  4. 重新整理 .net core 实践篇——— UseEndpoints中间件[四十八]

    前言 前文已经提及到了endponint 是怎么匹配到的,也就是说在UseRouting 之后的中间件都能获取到endpoint了,如果能够匹配到的话,那么UseEndpoints又做了什么呢?它是如 ...

  5. 重新整理 .net core 实践篇—————静态中间件[二十一]

    前言 简单整理一下静态中间件. 正文 我们使用静态文件调用: app.UseStaticFiles(); 那么这个默认会将我们根目录下的wwwroot作为静态目录. 这个就比较值得注意的,可能刚开始学 ...

  6. 重新整理 .net core 实践篇————缓存相关[四十二]

    前言 简单整理一下缓存. 正文 缓存是什么? 缓存是计算结果的"临时"存储和重复使用 缓存本质是用空间换取时间 缓存的场景: 计算结果,如:反射对象缓存 请求结果,如:DNS 缓存 ...

  7. 重新整理 .net core 实践篇—————配置系统之间谍[八](文件监控)

    前言 前文提及到了当我们的配置文件修改了,那么从 configurationRoot 在此读取会读取到新的数据,本文进行扩展,并从源码方面简单介绍一下,下面内容和前面几节息息相关. 正文 先看一下,如 ...

  8. 重新整理 .net core 实践篇————重定向攻击[三十九]

    前言 简单介绍一下重定向攻击. 正文 攻击思路: 看着上面挺复杂的,其实是一些很简单的步骤. 攻击者通过某些手段,让用户打开了一个好站点,打开的这个地址里面带有重定向信息,重定向信息就是自己伪造的站点 ...

  9. 重新整理 .net core 实践篇————配置应用[一]

    前言 本来想整理到<<重新整理.net core 计1400篇>>里面去,但是后来一想,整理 .net core 实践篇 是偏于实践,故而分开. 因为是重新整理,那么就从配置开 ...

随机推荐

  1. SpringBoot简单尝试

    一.spring boot核心 配置在类路径下autoconfigure下(多瞅瞅) @SpringBootApplication里的重要注解(@Configuration,@EnableAutoCo ...

  2. JVM虚拟机 类加载过程与类加载器

    目录 前言 类的生命周期 类加载过程 加载 连接 验证 准备 解析 初始化 类加载器 三大类加载器 双亲委派模型 概念 为什么要使用双亲委派模型 源码分析 反双亲委派模型 参考 前言 类装载器子系统是 ...

  3. GPUImage移植总结

    项目github地址: aoce 我是去年年底才知道有GPUImage这个项目,以前也一直没有在移动平台开发过,但是我在win平台有编写一个类似的项目oeip(不要关注了,所有功能都移植或快移植到ao ...

  4. Docker镜像讲解

    Docker镜像讲解 镜像是什么 镜像是一种轻量级的,可执行的独立软件包,用来打包软件运行环境和基于运行环境的开发软件,它包含运行某个软件做需要的所有的内容,包括代码,运行时,库,环境变量和配置文件. ...

  5. Linux性能调优命令之free

    功能说明 free 命令显示系统使用和空闲的内存情况,包括物理内存.交互区内存(swap)和内核缓冲区内存.共享内存将被忽略 语法 free [参数] 参数 -b : 以Byte为单位显示内存使用情况 ...

  6. (原创)高DPI适配经验系列:(三)字体与字号、缩放锚点

    一.前言 程序最基本的元素,就是文本,也就是字体.如果程序未进行高DPI的适配,最直观的感受便是字体的模糊.所以本篇便来说一下高DPI适配中的字体问题. 高DPI的适配,简单来说便是便是根据不同的DP ...

  7. mybatis-plus批量插入saveBatch太慢?我愿意称rewriteBatchedStatements为神

    最近在做项目优化,代码优化之后,测试接口,好家伙.一个定时任务接口执行要10秒左右. 一点点追踪,给每个方法打上执行时间,一点点缩小范围.好家伙,终于让我锁定了目标. 这是mybatis-plus的批 ...

  8. echo -n -e "请输入重启间隔的时间(分钟):\t"

    echo -n -e "请输入重启间隔的时间(分钟):\t"read interval##echo -n "Your choice is " # 加上 -n 可 ...

  9. Ansible命令行方式执行

    Ansible ad-hoc 什么是ad-hoc? 临时命令,执行完不会保存,类似于批量执行命令. ansible的选项 -i # 指定主机清单 ansible rsync -m ping -i 1. ...

  10. 007.kubernets的headless service配置和ingress的简单配置

    前面配置了servcie的nodepoint和clusterIP附在均衡 一 headless service配置 1.1 默认下的DNS配置 [root@docker-server1 deploym ...