.NET 开发常用技巧

依赖注入

1
2
3
4
5
6
7
8
9
10
11
12
// 注册服务
services.AddScoped<IMyService, MyService>();

// 构造函数注入
public class MyController : Controller
{
private readonly IMyService _service;
public MyController(IMyService service)
{
_service = service;
}
}

SqlSugar 常用操作

1
2
3
4
5
6
7
8
9
// 查询
var list = await db.Queryable<Order>()
.Where(x => x.Status == 1)
.OrderByDescending(x => x.CreateTime)
.ToListAsync();

// 分页
var page = await db.Queryable<Order>()
.ToPageIndexListAsync(pageIndex, pageSize);

异步编程

1
2
3
4
5
6
// 避免 async void
public async Task DoSomethingAsync()
{
await Task.Delay(1000);
Console.WriteLine("Done");
}

配置管理

1
2
3
4
5
6
// 读取配置
var value = configuration["Section:Key"];

// 强类型绑定
services.Configure<AppSettings>(
configuration.GetSection("AppSettings"));

.NET 开发常用技巧
https://dominator.abrdns.com/2026/06/28/dotnet-tips/
发布于
2026年6月28日
许可协议
CC BY-NC-SA 4.0