You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Nick Chapsas的Clean Minimal API扩展多Feature的结构咨询

针对扩展Clean Minimal API多Feature的问题解答

1. 实现多Feature的正确结构原则

核心遵循垂直功能划分,每个Feature(如Games、Consoles、Users)作为独立的模块化单元,和原模板中Customers的结构保持一致。每个Feature只负责自身领域的业务逻辑、端点暴露和数据处理,避免跨Feature的直接依赖,保持模块间的低耦合。

2. Feature文件夹内应存放的内容

参考原模板的Customers结构,每个Feature文件夹下应包含该功能专属的所有相关代码,示例如下:

  • 端点定义文件:如GamesEndpoints.cs,负责定义该Feature的所有API路由和请求处理逻辑
  • DTO子文件夹:存放该Feature的请求/响应数据传输对象,如CreateGameRequest.cs、GameResponse.cs
  • 业务逻辑层:如GameService.cs(或使用MediatR的Request/Handler),封装该Feature的核心业务规则
  • 验证规则:如CreateGameRequestValidator.cs,对输入请求做参数校验
  • 可选:如果每个Feature有独立的仓储需求,可包含仓储接口(如IGameRepository.cs),但建议将仓储实现统一放在基础设施层

3. 多Feature的合理项目结构

推荐采用分层+垂直Feature的结构,既保证代码的整洁性,又符合Clean Architecture的设计思想,示例结构如下:

GameStoreApi/
├── GameStoreApi.Web/          # Minimal API宿主项目
│   ├── Features/
│   │   ├── Customers/
│   │   │   ├── CustomersEndpoints.cs
│   │   │   ├── DTOs/
│   │   │   │   ├── CreateCustomerRequest.cs
│   │   │   │   └── CustomerResponse.cs
│   │   │   ├── Services/
│   │   │   │   └── CustomerService.cs
│   │   │   └── Validators/
│   │   │       └── CreateCustomerRequestValidator.cs
│   │   ├── Games/
│   │   │   ├── GamesEndpoints.cs
│   │   │   ├── DTOs/
│   │   │   ├── Services/
│   │   │   └── Validators/
│   │   ├── Consoles/
│   │   │   └── ...(同上述结构)
│   │   └── Users/
│   │       └── ...(同上述结构)
│   ├── Program.cs
│   └── appsettings.json
├── GameStoreApi.Core/         # 核心领域层(可选,用于共享领域模型和抽象接口)
│   ├── Entities/
│   │   ├── Customer.cs
│   │   ├── Game.cs
│   │   └── Console.cs
│   └── Interfaces/
│       ├── ICustomerRepository.cs
│       ├── IGameRepository.cs
│       └── IConsoleRepository.cs
├── GameStoreApi.Infrastructure/ # 基础设施层(处理数据访问、外部服务依赖)
│   ├── Persistence/
│   │   ├── AppDbContext.cs
│   │   └── Repositories/
│   │       ├── CustomerRepository.cs
│   │       └── GameRepository.cs
│   └── ExternalServices/
│       └── ...(如支付服务、第三方认证等)

该结构的优势:

  • Web层仅负责暴露API端点,不包含核心业务逻辑,职责单一
  • Core层封装领域模型和抽象接口,保证业务逻辑的独立性
  • Infrastructure层处理具体的外部依赖,便于替换和测试
  • 每个Feature在Web层内独立,便于团队并行开发和维护

.NET7 API优质模板推荐

  • 官方Minimal API模板:通过dotnet new minimalapi创建,基础灵活,适合基于此扩展多Feature结构
  • Ardalis Clean Architecture模板:支持Minimal API选项,严格遵循Clean Architecture设计,内置完整的Feature划分示例,适合中大型项目
  • Clean Architecture Template(Steve Smith):经典的Clean架构模板,可适配Minimal API,强调分层和领域驱动设计
  • Minimal API Clean Architecture Template:专门针对Minimal API的Clean架构模板,直接按Feature组织代码,开箱即用

关于你提供的结构截图可行性评估

如果截图中的结构是每个Feature作为独立文件夹,内部包含自身的端点、DTO、业务逻辑等专属代码,那么这个结构是完全可行的,符合垂直功能划分的原则,能保证代码的整洁性和可维护性。

如果截图是按代码类型(如所有Endpoints放一个文件夹、所有DTOs放另一个)来组织,长期维护会导致代码耦合度升高,建议调整为按Feature垂直划分的结构。

内容的提问来源于stack exchange,提问作者user13474036

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 22:15:28