Clear Code for Minimal API

十念  金牌会员 | 2024-5-21 00:05:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 876|帖子 876|积分 2628

我在写MinimalAPI的时候,发现不能最清晰的看到每个API,缘故原由就是:WebAPI中不停增长逻辑处理过程
于是我在想如何简化API至一行,在一点一点想办法中,发现了简化DotNET Minimal API的方式。特此记录下来这个思路给需要资助的人。我的灵感泉源于 C# 11 功能 - 接口中的静态假造成员,通过静态假造成员清晰整个API。
这是我思路的终极结果:在 Program.cs 中我们能通过一行代码,清晰的看到代码情况。
而无需指定平常不是很关心的处理过程和哀求方式。
  1. app.MapGroup("Connect", o =>
  2. {
  3.     o.MapMethods<Authorize>("Authorize");
  4.     o.MapMethods<Authorize.Callback>("Authorize/Callback");
  5.     o.MapMethods<Token>("Token");
  6.     o.MapMethods<UserInfo>("UserInfo").RequireAuthorization(new AuthorizeAttribute()
  7.     {
  8.         AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme
  9.     });
  10.     o.MapMethods<Endsession>("Endsession");
  11. });
  12. app.MapGroup("Account", o =>
  13. {
  14.     o.MapMethods<Login>("Login");
  15.     o.MapMethods<Externallogin>("Externallogin");
  16.     o.MapMethods<Externallogin.Callback>("Externallogin/Callback");
  17.     o.MapMethods<ConfirmationLogin>("ConfirmationLogin");
  18.     o.MapMethods<ForgotPassword>("ForgotPassword");
  19.     o.MapMethods<ResetPassword>("ResetPassword");
  20. });
复制代码
我们只需要简单的三步就可以做到这个事情,并且可以不消反射和其他的复杂过程。
第一步,我们需要创建(附带静态抽象函数的接口)IEndpointBase。
  1. public interface IEndpointBase
  2. {
  3.     public static abstract IEnumerable<string> HTTPMethods();
  4.     public static abstract Delegate Handler();
  5. }
复制代码
第二步,需要实现IEndpointBase。
  1. public class Login : IEndpointBase
  2. {
  3.     public record AccountLoginRequest
  4.     {
  5.         [JsonPropertyName("u"), Required]
  6.         public string UserName { get; set; } = default!;
  7.         [JsonPropertyName("p"), Required]
  8.         public string Password { get; set; } = default!;
  9.         [JsonPropertyName("r"), Required]
  10.         public string ReturnUrl { get; set; } = default!;
  11.         [FromQuery]
  12.         public bool UseCookies { get; set; }
  13.     }
  14.     public static Delegate Handler()
  15.     {
  16.         var callback = async ([FromBody] AccountLoginRequest request, [FromServices] SignInManager signInManager) =>
  17.         {
  18.             // Todo: returnUrl validate is success
  19.             var result = await signInManager.PasswordSignInAsync(request.UserName, request.Password, request.UseCookies, lockoutOnFailure: true);
  20.             return Results.Text(result.ToString());
  21.         };
  22.         return callback;
  23.     }
  24.     public static IEnumerable<string> HTTPMethods() => [HttpMethods.Post];
  25. }
复制代码
第三步:处理静态IEndpointBase,此时我们已经完成了这件事情。
  1. public static RouteHandlerBuilder MapMethods<T>(this IEndpointRouteBuilder app, [StringSyntax("Route")] string pattern) where T : IEndpointBase
  2. {
  3.     return app.MapMethods(pattern, T.HTTPMethods(), T.Handler());
  4. }
复制代码
单纯的利用扩展好的 MapMethods 已经足够清晰了。
但如果有对API进行分组的要求,利用原生的照旧不会清晰,缘故原由是:

  • 要对 MapGroup 的值赋予变量名,比如说 var accountGroup = app.MapGroup("Account"),每个组都要想个名字。accountGroup
  • 不能清楚自己的边界,比如说 写代码时,有可能出现插队的情况,本来 accountGroup下面都是属于它的端点,结果不小心插进来一个别的。
于是简单的对MapGroup进行了扩展,终极结果在本文最上面。
  1. public static void MapGroup(this IEndpointRouteBuilder endpoints, [StringSyntax("Route")] string prefix, Action<IEndpointRouteBuilder> action)
  2. {
  3.     var group = endpoints.MapGroup(prefix);
  4.     action(group);
  5. }
复制代码
总结:通过这种方式,代码结构已经清晰多了。若是有议,可以在评论区联系我。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

十念

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表