当前位置:网站首页>How to better organize the minimum web api code structure
How to better organize the minimum web api code structure
2022-06-23 04:58:00 【Dotnet cross platform】
Preface
We are 《.NET 6 Try out new features 》 In the said , As project requirements and complexity increase , Minimum size of a single file WEB API Will become very bloated .
and ,Program.cs You should only put the startup and initialization code . It should not contain too much MapXXX Method .
that , How to better organize the smallest WEB API What about the code structure ?
1. Static help class
You can move them into separate classes .
for example , You can create one called MinimalApiHelper And add static methods to it :
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 Revised as follows :
......
app.UseHttpsRedirection();
MinimalApiHelper.RegisterWeatherForecastAPIs(app);
app.Run();2. Extension method
Further more , You can create these static methods as WebApplication Class extension methods :
public static void RegisterWeatherForecastAPIs(this WebApplication app)
{
......
}You can simplify the calling code like this :
......
app.RegisterWeatherForecastAPIs();
app.Run();3. Dependency injection
however , When there are a large number of services , You need to execute the registration code multiple times :
app.RegisterService1APIs();
app.RegisterService2APIs();
......We can use dependency injection to simplify registration code .
The implementation code is as follows :
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 Revised as follows :
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMinimalApiRegisters(typeof(Program));
var app = builder.Build();
app.UseMinimalApiRegisters();
app.Run(); Traverse all of the in the assembly IMinimalApiRegister Implementation class , Then iterate through the calling implementation class RegisterAPIs Method .
The example implementation classes are as follows :
public class Service1Register : IMinimalApiRegister
{
public void RegisterAPIs(WebApplication app)
{
app.MapGet("/", () => "Hello My IO");
}
}Conclusion
today , We introduced how to organize the smallest WEB API The code structure .
If you have a better plan , Welcome to my official account. “My IO” Message discussion
边栏推荐
- 开关磁阻电机悬浮驱动IR2128小结
- Dpr-34v/v two position relay
- STL tutorial 3- exception mechanism
- Abnova 荧光染料 510-M 链霉亲和素方案
- mysql json
- laravel 8.4 路由问题,结尾处是编辑器左侧对照表,小白可看懂
- 20000 words + 20 pictures | details of nine data types and application scenarios of redis
- 使用Live Chat促进业务销售的惊人技巧
- Dsp7 environment
- Thinkphp6 solving jump problems
猜你喜欢

开发一年不到,来面试居然敢开口要20K,面完连8K都不想给~

mysql json

2 万字 + 20张图|细说 Redis 九种数据类型和应用场景

Cve-2019-14287 (sudo right raising)

一款MVC5+EasyUI企业快速开发框架源码 BS框架源码

Cocos learning diary 1 - node

Dsp7 environment

Distance measure - cosine distance

cadence中的焊盘和flash symbol

The paddepaddle model is deployed in a service-oriented manner. After restarting the pipeline, an error is reported, and the TRT error is reported
随机推荐
ICER skill 01 regular matching
Openjudge noi 1.13 50: several
力扣今日题513. 找树左下角的值
智能语音时代到来,谁在定义新时代AI?
开关磁阻电机悬浮驱动IR2128小结
thinkPHP6解决跳转问题
Project summary 1 (header file, switch, &, bit variables)
如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
项目总结1(头文件,switch,&&,位变量)
Altium designer 09 screen printing displays a green warning near the pad. How to prevent it from alarming?
STL tutorial 3- exception mechanism
Static two position relay xjls-84/440/dc220v
2022 simulated examination question bank and answers for safety management personnel of metal and nonmetal mines (open pit mines)
Receive incoming files and download (simple usage) a tag
物体结构图,快速图解物体内部结构
Shadertoy basic teaching 02. Drawing smiling faces
Cve-2019-14287 (sudo right raising)
win10查看my.ini路径
CAN总线基础知识
Thinkphp6 template replacement