大家好,我是失业在家,正在找工作的博主Jerry。今天给大家介绍一个能大大减少ASP.Net Minimal WebApi编码量的方法。

我们一般会把微服务的VO和DTO封装成消息类,并作为WebApi的Request和Response参数进行网络传递。

如果使用MediatR,我们封装的消息类就要实现 MediatR Contract 接口 IRequest<> 或者INotification,  例如我的代码如下:

namespace MediatRApplication.CategoryCRUD
{
public class CreateCategory : IRequest<CreateCategoryResult>
{
public string Message { get; set; }
}
public class CreateCategoryResult
{
public string Message { get; set; }
} public class ReadCategory : IRequest<ReadCategoryResult>
{
public string Message { get; set; }
}
public class ReadCategoryResult
{
public string Message { get; set; }
} public class UpdateCategory : IRequest<UpdateCategoryResult>
{
public string Message { get; set; }
}
public class UpdateCategoryResult
{
public string Message { get; set; }
} public class DeleteCategory : IRequest
{
public string Message { get; set; }
}
}

如上代码是对Category业务实体进行CRUD操作封装的DTO消息类,每个消息类都实现了MediatR的IRequest接口。有了消息类,就可以对每个消息类编写处理器(Handler),以实现业务功能。

有了消息类,就需要为每个消息类创建WebApi接口,以实现消息的Request和Response。WebAPI接口中没有业务逻辑,只需要调用MediatR的Send方法将消息类发送给Handler即可。

但是,由于消息类比较多,一个一个创建WebApi接口是一件费时费力,并且容易出错的事情。作为一个架构师,是无法忍受程序员们干这些出力不讨好的事情的。

所以,为了项目,为了大家的Work Life Banlance, 我把创建WebApi这件事情减少成了一行代码。是的,你没看错,就是只要一行代码:

app.MapMediatorWebAPIs(typeof(CreateCategory).Assembly);

只要在ASP.Net Minimal API 项目的Progam文件中加入这一行代码,就可以把指定程序集中所有实现了IRequest<>和INotification的消息类自动生成WebAPI接口。

看起来很神奇,其实也不神奇。主要就是两个字:反射。还有泛型。

简单来说,就是在指定程序集中,通过反射查找那些类实现了IRequest<>或者INotification,然后在通过对泛型映射WebAPI方法的反射调用,为每个消息类生成WebApi接口。

Let me show you the code:

using MediatR;
using MediatRApplication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Reflection;
using System.Xml.Linq; namespace MediatRWebAPI
{
public static class MediatorWebAPIExtensions
{
/// <summary>
/// 扩展方法,为所有MediatR Contract 消息类创建WebAPI接口
/// </summary>
/// <param name="app"></param>
/// <param name="assemblies">Contract 消息类所在程序集</param>
/// <returns></returns>
public static IEndpointRouteBuilder MapMediatorWebAPIs(this IEndpointRouteBuilder app, params Assembly[] assemblies)
{
//为所有实现了IRequest<>的消息类创建WebAPI接口
Type genericRequestType = typeof(IRequest<>);
var sendMethodInfo = typeof(MediatorWebAPIExtensions).GetMethod("MapMediatorSendApi", BindingFlags.NonPublic | BindingFlags.Static);
foreach (var assembly in assemblies)
{
//获取该程序集中所有实现了IRequest<>的消息类类型
var requestTypes = assembly.GetTypes().Where(type => !type.IsInterface && type.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericRequestType));
foreach (var requestType in requestTypes)
{
//获取IRequest<>中尖括号中的泛型参数类型。
var responseType = requestType.GetInterfaces().First(t => t.IsGenericType && t.GetGenericTypeDefinition() == genericRequestType).GetGenericArguments().First();
//反射调用泛型映射WebApi方法
var genericMethod = sendMethodInfo.MakeGenericMethod(requestType, responseType);
genericMethod.Invoke(null, new object[] { app, requestType.Name });
} }
//为所有实现了INotification的消息类创建WebAAPI接口
Type genericNotificationType = typeof(INotification);
var publishMethodInfo = typeof(MediatorWebAPIExtensions).GetMethod("MapMediatorPublishApi", BindingFlags.NonPublic | BindingFlags.Static);
foreach (var assembly in assemblies)
{
//获取该程序集中所有实现了INotification的消息类类型
var requestTypes = assembly.GetTypes().Where(type => !type.IsInterface && genericNotificationType.IsAssignableFrom(type));
foreach (var requestType in requestTypes)
{
//反射调用泛型映射WebApi方法
var genericMethod = publishMethodInfo.MakeGenericMethod(requestType);
genericMethod.Invoke(null, new object[] { app, requestType.Name });
} } return app;
} /// <summary>
/// 为实现了IRequest<>的消息类为映射为WebAPI接口,根据消息类名称生成对应的CRUDD Http Method。
/// </summary>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="app"></param>
/// <param name="requestTypeName"></param>
internal static void MapMediatorSendApi<TRequest, TResponse>(IEndpointRouteBuilder app, string requestTypeName) where TRequest : IRequest<TResponse>
{
if (requestTypeName.StartsWith("Create")) //Http Post
{
var uri = new Uri(requestTypeName.Replace("Create", ""), UriKind.Relative);
app.MapPost(uri.ToString(), async ([FromServices] IMediator mediator, [FromBody] TRequest request) =>
{
TResponse response = await mediator.Send(request);
return Results.Created(uri, response);
}).WithName(requestTypeName).WithOpenApi();
}
else if (requestTypeName.StartsWith("Read")) //Http Get
{
var uri = new Uri(requestTypeName.Replace("Read", ""), UriKind.Relative);
app.MapGet(uri.ToString(), async ([FromServices] IMediator mediator, [FromBody] TRequest request) =>
{
TResponse response = await mediator.Send(request);
return Results.Ok(response);
}).WithName(requestTypeName).WithOpenApi();
}
else if (requestTypeName.StartsWith("Update")) //Http Put
{
var uri = new Uri(requestTypeName.Replace("Update", ""), UriKind.Relative);
app.MapPut(uri.ToString(), async ([FromServices] IMediator mediator, [FromBody] TRequest request) =>
{
TResponse response = await mediator.Send(request);
return Results.Ok(response);
}).WithName(requestTypeName).WithOpenApi();
}
else if (requestTypeName.StartsWith("Delete")) //Http Delete
{
var uri = new Uri(requestTypeName.Replace("Delete", ""), UriKind.Relative);
app.MapDelete(uri.ToString(), async ([FromServices] IMediator mediator, [FromBody] TRequest request) =>
{
TResponse response = await mediator.Send(request);
return Results.NoContent();
}).WithName(requestTypeName).WithOpenApi();
}
else //如不匹配则生成MediatR Send WebAPI接口
{
app.MapPost("/mediatr/send/" + requestTypeName, async ([FromServices] IMediator mediator, [FromBody] TRequest request) =>
{
TResponse response = await mediator.Send(request);
return Results.Ok(response);
}).WithName(requestTypeName).WithOpenApi();
}
} /// <summary>
/// 为实现了INotification的消息类映射WebAPI接口。
/// </summary>
/// <typeparam name="TNotification"></typeparam>
/// <param name="app"></param>
/// <param name="requestTypeName"></param>
internal static void MapMediatorPublishApi<TNotification>(IEndpointRouteBuilder app, string requestTypeName) where TNotification : INotification
{
app.MapPost("/mediatr/publish/" + requestTypeName, async ([FromServices] IMediator mediator, [FromBody] TNotification notification) =>
{
await mediator.Publish(notification);
return Results.Ok();
}).WithName(requestTypeName).WithOpenApi();
}
}
}

如上就是实现这个功能的所有代码,为了让大家看明白,我加了很多注释。如果哪位小伙伴还不明白就在下面留言。这些代码最难的地方就是对于泛型接口的处理。

我的示例项目如下,代码已经上传到了GitHub :iamxiaozhuang/MediatRWebAPI (github.com)  大家随便用。

根据MediatR的Contract Messages自动生成Minimal WebApi接口的更多相关文章

  1. iBatis——自动生成DAO层接口提供操作函数(详解)

    iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...

  2. Groovy元编程应用之自动生成订单搜索接口测试用例集

    背景 在 "Groovy元编程简明教程" 一文中,简明地介绍了 Groovy 元编程的特性. 那么,元编程可以应用哪些场合呢?元编程通常可以用来自动生成一些相似的模板代码. 在 & ...

  3. 【Golang】基于录制,自动生成go test接口自动化用例

    背景 之前写过一篇博客,介绍怎么用Python通过解析抓包数据,完成自动化用例的编写.最近这段时间在使用go test,所以就在想能不能也使用代码来生成自动化用例,快速提升测试用例覆盖率.说干就干. ...

  4. VS2017+WIN10自动生成类、接口的说明(修改类模板的方法)

    微软发布VS2017的时候,我第一时间离线一份专业版,安装到了自己的电脑上,开始体验,但是问题来了,在开发中建立类和接口的时候,说 明注释总要自己写一次,烦!~~于是还是像以前一样改IDE默认的类和接 ...

  5. 使用swagger实现在线api文档自动生成 在线测试api接口

    使用vs nuget包管理工具搜索Swashbuckle 然后安装便可 注释依赖于vs生成的xml注释文件

  6. 自动生成web api接口文档

    然后打开web程序,访问ip:port/Help. 为什么可以直接输入Help就能访问呢,因为这个插件本身已经配置了路径,如下. public class HelpPageAreaRegistrati ...

  7. spring和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    软件简介 Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛.http://projects.spring.io/spring-frame ...

  8. idea中自动生成实体类

    找到生成实体的路径,找到Database数据表 找到指定的路径即可自动生成entity实体 在创建好的实体类内如此修改 之后的步骤都在脑子里  写给自己看的东西 哪里不会就记录哪里 test类(以前都 ...

  9. swagger 自动生成接口测试用例

    ---整体更新一波--- 1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的: 2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加 ...

  10. springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同. 主要讲maven的pom.xml和一些配置变化,详细说明. 软件简介 Spring是一个流行 ...

随机推荐

  1. Centos下使用containerd管理容器:5分钟从docker转型到containerd

    目录 一.系统环境 二.前言 三.containerd 四.部署containerd 4.1 安装containerd 4.2 containerd配置文件 4.3 配置containerd阿里云镜像 ...

  2. GreatSQL vs MySQL性能测试来了,速围观~

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. GreatSQL是MySQL的国产分支版本,使用上与MySQL一致. 1.结论先行 无论ibp(innodb_buffer ...

  3. 数仓Hive和分布式计算引擎Spark多整合方式实战和调优方向

    @ 目录 概述 Spark on Hive Hive on Spark 概述 编译Spark源码 配置 调优思路 编程方向 分组聚合优化 join优化 数据倾斜 任务并行度 小文件合并 CBO 谓词下 ...

  4. Go语言学习的坑爹历程

    鄙人暑期实习,需要用Go语言进行编程 在go语言中,结构体的定义只支持变量的声明,成员函数是采用"接口方法"来实现的 留一个成员定义的模板在此 package main impor ...

  5. C语言001--hello world编译详解

    1.编写hello.c程序,并编译运行 book@100ask:~/linux/c01$ cat hello.c -n 1 #include <stdio.h> 2 3 int main( ...

  6. 使用Gitlab CI/CD功能在本地部署 Spring Boot 项目

    前提条件: 1.Docker安装Gitlab,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13814730.html 2.Docker安装Gitlab-runn ...

  7. HTTPS安全加固配置最佳实践指南

    转载自:https://www.bilibili.com/read/cv16067729?spm_id_from=333.999.0.0 0x02 HTTPS安全加固指南 描述: 当你的网站上了 HT ...

  8. Elasticsearch:使用_update_by_query更新文档

    转载自: https://blog.csdn.net/UbuntuTouch/article/details/105564270 在很多的情况下,我们我们想更新我们所有的文档: 添加一个新的field ...

  9. Node Exporter监控指标

    访问http://localhost:9100/metrics,可以看到当前node exporter获取到的当前主机的所有监控数据,如下所示: 每一个监控指标之前都会有一段类似于如下形式的信息: # ...

  10. CentOS系统磁盘目录空间调整

    前几天装了几台linux服务器,安装操作系统的时候,选择了默认磁盘分区,结果导致后面主目录分区空间不够用了,需要把其他分区的空间划分给主分区一点. 下面以CentOS6.5演示: 一.查看当前系统的磁 ...