本指南将介绍如何按照 BitzOrcas 的五件套模块套件模式创建新的平台模块。
模块结构
一个 BitzOrcas 模块由以下部分组成:
Platform.Application/└── {ModuleName}/ ├── {Module}Service.cs → 业务逻辑 ├── I{Module}Repository.cs → 仓储端口 ├── I{Module}EventPublisher.cs → 事件发布端口 ├── I{Module}AuditSink.cs → 审计端口 ├── InMemory{Module}Repository.cs → 测试/Shell 默认实现 └── Null{Module}EventPublisher.cs → 空操作默认实现
SaaS.Contracts/└── {ModuleName}/ → 集成事件 DTO
Infrastructure.SqlSugar/└── {ModuleName}/ ├── {Module}PlatformDependencyInjection.cs └── SqlSugar{Module}Repository.cs
Infrastructure.Persistence.Models/└── {ModuleName}/ └── {Module}Entity.cs → 持久化模型
Api/├── Endpoints/│ └── {Module}Endpoints.cs → Minimal API 端点│ └── {Request}Dto.cs → 请求 DTO逐步操作
1. 定义实体
// Infrastructure.Persistence.Models/{ModuleName}/public class TodoEntity : TenantSoftDeleteEntityBase{ [SugarColumn(Length = 200)] public string Title { get; set; } public bool IsCompleted { get; set; }}2. 在 PersistenceModelRegistry 中注册
将实体添加到 PersistenceModelRegistry,以便 SqlSugar 自动创建表。
3. 创建服务端口
// Platform.Application/{ModuleName}/public interface ITodoRepository{ Task<IReadOnlyList<Todo>> GetAllAsync(TenantId tenantId, CancellationToken ct); Task<Todo?> GetByIdAsync(Guid id, CancellationToken ct); Task<Todo> CreateAsync(TenantId tenantId, string title, CancellationToken ct);}4. 实现服务
public class TodoService{ public async Task<Todo> CreateAsync(TenantId tenantId, string title, CancellationToken ct) { // 业务逻辑 }}5. 创建 Minimal API 端点
public static class TodoEndpoints{ public static void MapTodoEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/api/todos") .RequireAuthorization();
group.MapPost("/", async (...) => ...); group.MapGet("/", async (...) => ...); }}6. 注册到 DI
添加到 PersistenceRegistration 和 EndpointMap。