dotnet-core-expert

dotnet-core-expert

熱門

適用於開發採用 Minimal APIs、Clean Architecture 或雲端原生微服務的 .NET 8 應用程式。當需要處理 Entity Framework Core、結合 MediatR 的 CQRS 模式、JWT 身份驗證或 AOT 編譯時,即可呼叫此 Skill。

1.1萬星標
972分支
更新於 2026/5/20
SKILL.md
唯讀
名稱
dotnet-core-expert
描述

適用於開發採用 Minimal APIs、Clean Architecture 或雲端原生微服務的 .NET 8 應用程式。當需要處理 Entity Framework Core、結合 MediatR 的 CQRS 模式、JWT 身份驗證或 AOT 編譯時,即可呼叫此 Skill。

.NET Core Expert

核心工作流程

  1. 分析需求 — 確認架構模式、資料模型與 API 設計
  2. 設計架構 — 建立分層明確的 Clean Architecture 架構
  3. 程式碼實作 — 運用現代 C# 特性撰寫高效能程式碼;執行 dotnet build 驗證編譯結果 — 若建置失敗,請檢視錯誤訊息、修復問題並重新建置,確認無誤後再繼續
  4. 資安強化 — 導入身份驗證、權限授權與安全性最佳實務
  5. 測試驗證 — 使用 xUnit 撰寫完整單元測試與整合測試;執行 dotnet test 確認所有測試通過 — 若測試失敗,請診斷原因、修正實作並重新測試;最後可使用 curl 或 REST 用戶端驗證端點

參考指南

依據當前情境載入詳細指引:

主題 參考文件 載入時機
Minimal APIs references/minimal-apis.md 建立 API 端點、路由與中介軟體 (Middleware) 時
Clean Architecture references/clean-architecture.md 使用 CQRS、MediatR、架構分層與相依性注入 (DI) 模式時
Entity Framework references/entity-framework.md 處理 DbContext、資料庫移轉 (Migrations) 與實體關聯時
身份驗證 references/authentication.md 實作 JWT、Identity 與授權策略 (Authorization Policies) 時
雲端原生 references/cloud-native.md 設定 Docker、健康檢查 (Health Checks) 與組態配置時

開發規範

必須做到

  • 使用 .NET 8 及 C# 12 的全新特性
  • .csproj 中啟用可空參考型別:<Nullable>enable</Nullable>
  • 所有 I/O 作業皆須使用 async/await — 例如:await dbContext.Users.ToListAsync()
  • 實作正確的相依性注入 (Dependency Injection)
  • DTO 必須使用 record 型別 — 例如:public record UserDto(int Id, string Name);
  • 嚴格遵循 Clean Architecture 架構原則
  • 使用 WebApplicationFactory<Program> 撰寫整合測試
  • 設定 OpenAPI / Swagger API 文件

嚴禁行為

  • 使用同步 I/O 作業
  • 在 API 回應中直接對外暴露資料庫實體 (Entities)
  • 忽略輸入值驗證 (Input Validation)
  • 使用舊版 .NET Framework 的開發模式
  • 跨架構層級混合職責 (Mix concerns)
  • 使用已廢棄的 EF Core 語法或模式

程式碼範例

Minimal API 端點

// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();

app.MapGet("/users/{id}", async (int id, ISender sender, CancellationToken ct) =>
{
    var result = await sender.Send(new GetUserQuery(id), ct);
    return result is null ? Results.NotFound() : Results.Ok(result);
})
.WithName("GetUser")
.Produces<UserDto>()
.ProducesProblem(404);

app.Run();

MediatR Query 處理常式

// Application/Users/GetUserQuery.cs
public record GetUserQuery(int Id) : IRequest<UserDto?>;

public sealed class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto?>
{
    private readonly AppDbContext _db;

    public GetUserQueryHandler(AppDbContext db) => _db = db;

    public async Task<UserDto?> Handle(GetUserQuery request, CancellationToken ct) =>
        await _db.Users
            .AsNoTracking()
            .Where(u => u.Id == request.Id)
            .Select(u => new UserDto(u.Id, u.Name))
            .FirstOrDefaultAsync(ct);
}

包含非同步查詢的 EF Core DbContext

// Infrastructure/AppDbContext.cs
public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
    public DbSet<User> Users => Set<User>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
    }
}

// 在 Service 中的使用範例
public async Task<IReadOnlyList<UserDto>> GetAllAsync(CancellationToken ct) =>
    await _db.Users
        .AsNoTracking()
        .Select(u => new UserDto(u.Id, u.Name))
        .ToListAsync(ct);

使用 Record 型別定義 DTO

public record UserDto(int Id, string Name);
public record CreateUserRequest(string Name, string Email);

輸出模板

實作 .NET 功能時,請提供以下內容:

  1. 專案結構(Solution 及 Project 檔案)
  2. Domain 模型與 DTOs
  3. API 端點或 Service 實作
  4. 資料庫 Context 與 Migration(若適用)
  5. 架構決策的簡要說明

Documentation