ワンクリックで
handler
Handler 業務邏輯層實作技能,協助開發者實作符合專案規範的 Handler,包含業務邏輯處理、流程協調、Result Pattern 錯誤處理與跨 Repository 操作。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Handler 業務邏輯層實作技能,協助開發者實作符合專案規範的 Handler,包含業務邏輯處理、流程協調、Result Pattern 錯誤處理與跨 Repository 操作。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
API 開發流程引導技能,協助開發者選擇合適的開發流程(API First 或 Code First),並提供 OpenAPI 規格管理、程式碼產生等自動化支援。
Cucumber/Gherkin BDD 最佳實踐指導技能,提供 Gherkin 撰寫規範、情境設計原則、Discovery Workshop 引導與常見反模式識別,協助團隊撰寫高品質的行為驅動開發規格。
BDD 測試實作技能,協助開發者使用 Reqnroll 撰寫行為驅動開發測試,包含 Gherkin 語法、測試步驟實作與 Docker 測試環境設定。
EF Core 操作與最佳化技能,協助開發者正確使用 Entity Framework Core,包含 DbContextFactory 模式、查詢最佳化、Migration 管理與反向工程。
錯誤處理與 Result Pattern 技能,協助開發者實作統一的錯誤處理機制,包含 Result Pattern 應用、Failure 物件建立與分層錯誤處理策略。
中介軟體實作技能,協助開發者實作符合專案規範的中介軟體,包含 TraceContext 管理、Exception Handling、Request Logging 與管線配置。
| name | handler |
| description | Handler 業務邏輯層實作技能,協助開發者實作符合專案規範的 Handler,包含業務邏輯處理、流程協調、Result Pattern 錯誤處理與跨 Repository 操作。 |
Handler 業務邏輯層實作技能,協助開發者實作符合專案規範的 Handler,包含業務邏輯處理、流程協調、Result Pattern 錯誤處理等。
Result<TSuccess, TFailure> 作為回傳類型@workspace 我需要實作 Handler 業務邏輯
使用 handler 實作業務邏輯
graph LR
A[Controller] --> B[Handler]
B --> C[Repository 1]
B --> D[Repository 2]
B --> E[External Service]
C --> F[(Database)]
D --> F
業務邏輯
錯誤處理
交易管理
HTTP 相關邏輯
直接資料存取
拋出業務例外
完整範本請參考:assets/handler-template.cs
public async Task<Result<MemberResponse, Failure>> CreateMemberAsync(
CreateMemberRequest request,
CancellationToken cancellationToken = default)
{
try
{
// 業務邏輯
var member = new Member
{
Email = request.Email,
Name = request.Name
};
var createResult = await repository.CreateAsync(member, cancellationToken);
return createResult.Match(
success => Result.Success<MemberResponse, Failure>(MapToResponse(success)),
failure => Result.Failure<MemberResponse, Failure>(failure)
);
}
catch (Exception ex)
{
return Result.Failure<MemberResponse, Failure>(new Failure
{
Code = nameof(FailureCode.InternalServerError),
Message = ex.Message,
TraceId = traceContext.TraceId,
Exception = ex // 保存原始例外
});
}
}
public async Task<Result<OrderDetail, Failure>> CreateOrderAsync(
CreateOrderRequest request,
CancellationToken cancellationToken = default)
{
await using var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
try
{
// 1. 建立訂單
var orderResult = await orderRepository.CreateAsync(...);
if (orderResult.IsFailure) return orderResult;
// 2. 建立訂單明細
var itemsResult = await orderItemRepository.CreateBatchAsync(...);
if (itemsResult.IsFailure)
{
await transaction.RollbackAsync(cancellationToken);
return Result.Failure<OrderDetail, Failure>(itemsResult.Error);
}
// 3. 更新庫存
var stockResult = await inventoryRepository.ReduceStockAsync(...);
if (stockResult.IsFailure)
{
await transaction.RollbackAsync(cancellationToken);
return Result.Failure<OrderDetail, Failure>(stockResult.Error);
}
await transaction.CommitAsync(cancellationToken);
return Result.Success<OrderDetail, Failure>(...);
}
catch (Exception ex)
{
await transaction.RollbackAsync(cancellationToken);
return Result.Failure<OrderDetail, Failure>(new Failure { ... });
}
}
// ❌ 錯誤
await repository.CreateAsync(member);
// ✅ 正確
await repository.CreateAsync(member, cancellationToken);
// ❌ 錯誤
if (await repository.EmailExistsAsync(email))
throw new BusinessException("Email 已存在");
// ✅ 正確
if (await repository.EmailExistsAsync(email, cancellationToken))
return Result.Failure<MemberResponse, Failure>(new Failure
{
Code = nameof(FailureCode.DuplicateEmail),
Message = "Email 已被註冊",
TraceId = traceContext.TraceId
});
// ❌ 錯誤
catch (Exception ex)
{
return Result.Failure<T, Failure>(new Failure
{
Code = nameof(FailureCode.InternalServerError),
Message = ex.Message
// 缺少 Exception = ex
});
}
// ✅ 正確
catch (Exception ex)
{
return Result.Failure<T, Failure>(new Failure
{
Code = nameof(FailureCode.InternalServerError),
Message = ex.Message,
TraceId = traceContext.TraceId,
Exception = ex // 保存原始例外
});
}
repository-design - Repository 設計與實作error-handling - Result Pattern 與錯誤處理api-development - Controller 整合feature-development-agent - 使用本 skill 的完整功能開發流程