当前位置:网站首页>如何更好地组织最小 WEB API 代码结构
如何更好地组织最小 WEB API 代码结构
2022-06-23 03:51:00 【dotNET跨平台】
前言
我们在《.NET 6新特性试用》中讲过,随着项目需求和复杂性的增加,单个文件的最小 WEB API 会变得非常臃肿。
而且,Program.cs 应该只放启动和初始化代码。不应该包含太多 MapXXX 方法。
那么,如何以更好的方式组织最小 WEB API 代码结构呢?
1. 静态帮助类
可以将它们移到单独的类中。
例如,你可以创建一个名为 MinimalApiHelper 的静态类并向其添加静态方法:
public static class MinimalApiHelper
{
public static void RegisterWeatherForecastAPIs(WebApplication app)
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
......
})
.WithName("GetWeatherForecast");
}
}Program.cs 修改如下:
......
app.UseHttpsRedirection();
MinimalApiHelper.RegisterWeatherForecastAPIs(app);
app.Run();2. 扩展方法
更进一步,可以将这些静态方法创建为 WebApplication 类的扩展方法:
public static void RegisterWeatherForecastAPIs(this WebApplication app)
{
......
}可以像这样简化调用代码:
......
app.RegisterWeatherForecastAPIs();
app.Run();3. 依赖注入
但是,存在大量服务时,需要多次执行注册代码:
app.RegisterService1APIs();
app.RegisterService2APIs();
......我们可以使用依赖注入简化注册代码。
实现代码如下:
public static class MinimalApiExtentions
{
public static void AddMinimalApiRegisters(this IServiceCollection services, params Type[] types)
{
var registers = new List<IMinimalApiRegister>();
foreach (var type in types)
{
registers.AddRange(type.Assembly.GetTypes()
.Where(x => typeof(IMinimalApiRegister).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
.Select(Activator.CreateInstance).Cast<IMinimalApiRegister>());
}
services.AddSingleton<IEnumerable<IMinimalApiRegister>>(registers);
}
public static void UseMinimalApiRegisters(this WebApplication app)
{
var registers = app.Services.GetRequiredService<IEnumerable<IMinimalApiRegister>>();
foreach (var register in registers)
{
register.RegisterAPIs(app);
}
}
}Program.cs 修改如下:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMinimalApiRegisters(typeof(Program));
var app = builder.Build();
app.UseMinimalApiRegisters();
app.Run();遍历程序集中的所有 IMinimalApiRegister 实现类,然后遍历调用实现类的 RegisterAPIs 方法。
示例实现类如下:
public class Service1Register : IMinimalApiRegister
{
public void RegisterAPIs(WebApplication app)
{
app.MapGet("/", () => "Hello My IO");
}
}结论
今天,我们介绍了如何组织最小 WEB API 代码结构。
如果你有更好的方案,欢迎到我的公众号“My IO”留言讨论
边栏推荐
- Alkylation process test questions and simulation test in 2022
- FreeModBus解析1
- Cocos学习日记2——脚本和属性
- 8位全加器原理
- 不归零编码NRZ
- Examples of corpus data processing cases (part of speech encoding, part of speech restoration)
- What are the characteristics of SRM supplier management system developed by manufacturing enterprises
- 静态双位置继电器 XJLS-84/440/DC220V
- 如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
- 关于php里tcp通讯用swoole框架出现的小问题
猜你喜欢

Pads and flash symbols in cadence

Dpr-34v/v two position relay

What are the characteristics of SRM supplier management system developed by manufacturing enterprises

Abnova酸性磷酸酶(小麦胚芽)说明书

laravel 8.4 路由问题,结尾处是编辑器左侧对照表,小白可看懂

Abnova actn4 purified rabbit polyclonal antibody instructions

The paddepaddle model is deployed in a service-oriented manner. After restarting the pipeline, an error is reported, and the TRT error is reported

Cocos learning diary 2 - scripts and attributes

Notes on writing questions in C language -- free falling ball

Qt 及QT VS Tools插件官方下载及安装
随机推荐
OGNL Object-Graph Navigation Language
欢迎使用CSDN-markdown编辑器
Cocos学习日记1——节点
Qt 及QT VS Tools插件官方下载及安装
Notes on writing questions in C language -- free falling ball
const理解之一
Abnova acid phosphatase (wheat germ) instructions
Static two position relay gls-3004k/dc220v
CVE-2019-14287(sudo提权)
TS advanced infer
独立站聊天机器人有哪些类型?如何快速创建属于自己的免费聊天机器人?只需3秒钟就能搞定!
Non return to zero code NRZ
Can bus Basics
openwrt目录结构
cadence中的焊盘和flash symbol
#18生成器函数的参数传递
An understanding of free() (an error in C Primer Plus)
Summary of switched reluctance motor suspension drive ir2128
STL教程3-异常机制
ADR electronic transmission EDI solution of national adverse drug reaction monitoring center