Prompt file imported from RayLiu1999/Finance_Manager (
.github/prompts/add-go-handler.prompt.md). Copyright stays with the author.
新增 CQRS Command(C# Clean Architecture)
請依據以下資訊,建立完整的 Command 垂直切片:
任務描述:$PLACEHOLDER_DESCRIPTION
你需要依序完成以下步驟
1. 確認 Domain Aggregate
確認 src/PFM.Domain/Aggregates/ 下對應的 Aggregate,了解現有的工廠方法/行為方法:
- 此 Command 觸發的業務邏輯應在哪個 Aggregate?
- 是否需要新增工廠方法或行為方法?
2. 在 Domain 層新增/修改方法
// src/PFM.Domain/Aggregates/{Resource}/{Resource}.cs
public static Result<{Resource}> {Action}({params})
{
// 業務規則驗證
// 建立 Aggregate
// RaiseDomainEvent(...)
return Result.Success(entity);
}
3. 定義 Command record
src/PFM.Application/{Resource}/Commands/{Action}{Resource}/
{Action}{Resource}Command.cs
{Action}{Resource}CommandHandler.cs
{Action}{Resource}CommandValidator.cs
Command 必須繼承 ICommand<TResponse>。
4. 撰寫 FluentValidation Validator
public class {Action}{Resource}CommandValidator : AbstractValidator<{Action}{Resource}Command>
{
public {Action}{Resource}CommandValidator()
{
// 輸入格式驗證(不是業務規則,業務規則在 Domain)
RuleFor(x => x.Symbol).NotEmpty().MaximumLength(20);
}
}
5. 撰寫 Handler
public sealed class {Action}{Resource}CommandHandler
: IRequestHandler<{Action}{Resource}Command, Result<{Resource}Dto>>
{
// 注入 IRepository + IUnitOfWork
// 呼叫 Domain 工廠/行為方法
// repo.Add() + uow.SaveChangesAsync()
// return Result.Success(Dto.From(entity))
}
6. 新增 Minimal API Endpoint
在 src/PFM.API/Endpoints/{Resource}Endpoints.cs 的 MapGroup 中新增:
g.MapPost("/", async (Command cmd, ISender sender, CancellationToken ct) =>
{
var result = await sender.Send(cmd, ct);
return result.IsSuccess
? Results.Created($"/api/v1/{resource}/{result.Value!.Id}", new { data = result.Value })
: result.Error.ToHttpResult();
}).WithName("{Action}{Resource}");
7. 若需要 EF Core Migration
dotnet ef migrations add {MigrationName} \
--project src/PFM.Infrastructure \
--startup-project src/PFM.API
dotnet ef database update \
--project src/PFM.Infrastructure \
--startup-project src/PFM.API
強制規範
- Domain 層零 NuGet 依賴
- Command record 使用不可變 record(非 class)
- Validator 只驗證輸入格式,業務規則在 Domain
- Handler 通過 Repository interface,不直接用 DbContext
- Result Pattern:禁止 throw Exception 控制業務流程
請依據以下資訊,建立完整的 C# Clean Architecture CQRS Command:
任務描述:$PLACEHOLDER_DESCRIPTION
你需要依序完成以下步驟
1. 確認現有結構
先確認以下檔案是否存在,了解目前的程式碼風格:
src/PFM.Domain/Aggregates/— 確認現有 Aggregate 寫法src/PFM.Application/{Resource}/Commands/— 確認現有 Command/Handler 寫法src/PFM.Infrastructure/Persistence/Configurations/— 確認現有 EF Core 設定
2. 設計 Domain Aggregate 或行為方法
在 src/PFM.Domain/Aggregates/{Resource}/{Resource}.cs 新增工廠方法或行為方法:
public static Result<{Resource}> Create(...) { ... }
3. 定義 Command + FluentValidation Validator
src/PFM.Application/{Resource}/Commands/{Action}/- Command 用
record不可變 - Validator 繼承
AbstractValidator<TCommand>
4. 實作 Handler
- 呼叫 Domain 工廠/行為方法
- 透過
IRepository + IUnitOfWork,不直接用 DbContext - 回傳
Result.Success(Dto.From(entity))
5. 新增 Minimal API Endpoint
在 src/PFM.API/Endpoints/{Resource}Endpoints.cs 的 MapGroup 中新增
6. 若需要新 table 或欄位,產生 EF Core Migration
dotnet ef migrations add {Name} --project src/PFM.Infrastructure --startup-project src/PFM.API
強制規範
- Domain 層零 NuGet 依賴
- 錯誤傳遞用
Result<T>,禁止 throw/catch 控制業務流程 - 日誌用
_logger.LogInformation("{Key} {Value}", ...)structured logging - Response 格式:
{ "data": result }或{ "data": items, "meta": {...} } - 業務錯誤碼格式:
RESOURCE_ACTION(例如TRADE_NOT_FOUND)