当前位置:网站首页>如何更好地组织最小 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”留言讨论
边栏推荐
- Halcon knowledge: binocular_ Discrimination knowledge
- notepad++ 查找替换之分组替换保留
- 国家药品不良反应监测中心 ADR 电子传输EDI解决方案
- What are the characteristics of SRM supplier management system developed by manufacturing enterprises
- [acnoi2022] no way without guessing
- thinkPHP6解决跳转问题
- MySQL导入大文件(可以是百万级,也可以是百级)
- Laravel 通过服务提供者来自定义分页样式
- 接收传来得文件并下载(简单用法)a标签
- Static two position relay xjls-84/440/dc220v
猜你喜欢

DSP7 环境

Abnova actn4 purified rabbit polyclonal antibody instructions

Current relay hdl-a/1-110vdc-1
![Fundamentals of 3D mathematics [16] formulas for uniformly accelerated linear motion](/img/51/5b05694bbd0f4fd01dd26cf55b22c7.png)
Fundamentals of 3D mathematics [16] formulas for uniformly accelerated linear motion

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

Non return to zero code NRZ

Abnova LiquidCell-负富集细胞分离和回收系统

Abnova ACTN4纯化兔多克隆抗体说明书

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

Dpr-34v/v two position relay
随机推荐
PCB任意角度和距离放置元器件
AD9使用技巧拾遗
欢迎使用CSDN-markdown编辑器
Precautions for running high-frequency and high-speed signal lines near PCB board - basic principles for high-frequency and high-speed signal design
Abnova ACTN4纯化兔多克隆抗体说明书
Banner 标语 旗帜
composer按装laravel
PaddlePaddle模型服务化部署,重新启动pipeline后出现报错,trt报错
项目总结1(头文件,switch,&&,位变量)
Pta:6-33 student ranking table (destructor)
Altium designer 09 screen printing displays a green warning near the pad. How to prevent it from alarming?
Left and right values
LabVIEW displays both hexadecimal and normal characters in the same table
Transformers中的动态学习率
Pta:6-29 application of virtual base classes - people, teachers and students
Pads and flash symbols in cadence
Abnova 荧光染料 555-C3 马来酰亚胺方案
thinkphp6 无关联ID的连表查询(2张表)主要是select的应用
PCB----理论与现实的桥梁
Pta:7-63 calculate the number one in the college entrance examination