AgentSkillsCN

aspnet-patterns

ASP.NET Core 设计模式——控制器、服务、中间件

SKILL.md
--- frontmatter
name: aspnet-patterns
description: ASP.NET Core patterns - Controllers, Services, Middleware

ASP.NET Core Patterns

Clean architecture for ASP.NET Core applications.


Architecture

code
Controller → Service → Repository → Database
LayerResponsibility
ControllerHTTP handling, validation
ServiceBusiness logic
RepositoryData access

Controller Pattern

csharp
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    private readonly IUserService _userService;

    public UsersController(IUserService userService)
    {
        _userService = userService;
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<UserDto>> GetById(int id)
    {
        var user = await _userService.GetByIdAsync(id);
        return user is null ? NotFound() : Ok(user);
    }

    [HttpPost]
    public async Task<ActionResult<UserDto>> Create(CreateUserDto dto)
    {
        var user = await _userService.CreateAsync(dto);
        return CreatedAtAction(nameof(GetById), new { id = user.Id }, user);
    }
}

Service Pattern

csharp
public class UserService : IUserService
{
    private readonly IUserRepository _repository;

    public UserService(IUserRepository repository)
    {
        _repository = repository;
    }

    public async Task<UserDto?> GetByIdAsync(int id)
    {
        var user = await _repository.GetByIdAsync(id);
        return user?.ToDto();
    }
}

Dependency Injection

csharp
// Program.cs
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();

Response Format

csharp
public class ApiResponse<T>
{
    public bool Success { get; set; }
    public T? Data { get; set; }
    public string? Error { get; set; }
}

HTTP Status Codes

MethodSuccessError
GET200 OK404 Not Found
POST201 Created400 Bad Request
PUT200 OK404 Not Found
DELETE204 No Content404 Not Found

DO / DON'T

✅ Do❌ Don't
Async/awaitBlocking calls
DI everywherenew in controllers
Proper status codes200 for everything
Constants/Enums for stringsMagic strings
Environment variables/ConfigHardcoded settings