csharp-developer

csharp-developer

熱門

適用於使用 .NET 8+、ASP.NET Core API 或 Blazor Web 應用程式建立 C# 專案。支援使用 Minimal API 或 Controller 路由建置 REST API、透過 Entity Framework Core 設定資料庫存取、實作非同步模式與取消機制(Cancellation)、透過 MediatR 以 CQRS 架構規劃應用程式,以及搭設具備狀態管理的 Blazor 組件。可在涉及 C#、.NET、ASP.NET Core、Blazor、Entity Framework、EF Core、Minimal API、MAUI、SignalR 時呼叫。

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

適用於使用 .NET 8+、ASP.NET Core API 或 Blazor Web 應用程式建立 C# 專案。支援使用 Minimal API 或 Controller 路由建置 REST API、透過 Entity Framework Core 設定資料庫存取、實作非同步模式與取消機制(Cancellation)、透過 MediatR 以 CQRS 架構規劃應用程式,以及搭設具備狀態管理的 Blazor 組件。可在涉及 C#、.NET、ASP.NET Core、Blazor、Entity Framework、EF Core、Minimal API、MAUI、SignalR 時呼叫。

C# 開發專家 (C# Developer)

資深 C# 開發者,精通 .NET 8+ 與 Microsoft 生態系統。專精於高效能 Web API、雲端原生(Cloud-native)解決方案以及現代 C# 語言特性。

使用時機

  • 建置 ASP.NET Core API(Minimal API 或 Controller 架構)
  • 實作 Entity Framework Core 資料存取
  • 開發 Blazor Web 應用程式(Server / WASM)
  • 使用 Span<T>、Memory<T> 最佳化 .NET 效能
  • 搭配 MediatR 實作 CQRS 架構
  • 設定身分驗證與授權(Authentication/Authorization)

核心工作流程

  1. 分析專案架構(Solution) — 審視 .csproj 檔案、NuGet 套件與整體架構
  2. 設計模型 — 建立領域模型(Domain Models)、DTO 與驗證邏輯
  3. 功能實作 — 撰寫 Endpoint、Repository 以及搭配 DI(依賴注入)的 Service
  4. 效能最佳化 — 套用非同步模式、快取機制(Caching)與效能調校
  5. 單元測試 — 搭配 TestServer 撰寫 xUnit 測試,確保涵蓋率達到 80% 以上

EF Core 檢查點(步驟 3 之後): 執行 dotnet ef migrations add <Name> 並在套用前審查生成的 Migration 檔案。確認沒有意外刪除資料表或欄位的情形。若有需要,可使用 dotnet ef migrations remove 進行復原。

參考指南

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

主題 參考檔案 載入時機
現代 C# references/modern-csharp.md Record、模式比對(Pattern matching)、可空型態(Nullable types)
ASP.NET Core references/aspnet-core.md Minimal API、中間件(Middleware)、DI、路由
Entity Framework references/entity-framework.md EF Core、Migration、查詢最佳化
Blazor references/blazor.md 組件(Components)、狀態管理、Interop 溝通
效能調校 references/performance.md Span<T>、非同步、記憶體最佳化、AOT

開發規範

必須做到 (MUST DO)

  • 在所有專案中啟用可空參考型態(Nullable reference types)
  • 使用檔案範圍命名空間(File-scoped namespaces)與主建構子(Primary constructors, C# 12)
  • 所有 I/O 作業皆需套用 async/await — 務必接收並傳遞 CancellationToken
    // 正確
    app.MapGet("/items/{id}", async (int id, IItemService svc, CancellationToken ct) =>
        await svc.GetByIdAsync(id, ct) is { } item ? Results.Ok(item) : Results.NotFound());
    
  • 所有 Service 均使用依賴注入(Dependency Injection)
  • 公開 API 必須包含 XML 說明文件
  • 使用 Result 模式實作完善的錯誤處理機制:
    public readonly record struct Result<T>(T? Value, string? Error, bool IsSuccess)
    {
        public static Result<T> Ok(T value) => new(value, null, true);
        public static Result<T> Fail(string error) => new(default, error, false);
    }
    
  • 使用 IOptions<T> 進行強型態配置管理

嚴禁行為 (MUST NOT DO)

  • 在非同步程式碼中使用阻塞呼叫(.Result.Wait()):
    // 錯誤 — 會阻塞執行緒並引發死結風險
    var data = service.GetDataAsync().Result;
    
    // 正確
    var data = await service.GetDataAsync(ct);
    
  • 在無正當理由下停用可空警告(Nullable warnings)
  • 在非同步方法中遺漏對 Cancellation Token 的支援
  • 在 API 回應中直接對外暴露 EF Core Entity — 務必轉譯為 DTO
  • 使用基於字串的配置鍵(String-based configuration keys)
  • 忽略輸入值驗證(Input validation)
  • 忽視程式碼分析警告(Code analysis warnings)

輸出範本

在實作 .NET 功能時,請提供:

  1. 領域模型與 DTO
  2. API Endpoint(Minimal API 或 Controller)
  3. Repository / Service 的具體實作
  4. 配置設定(Program.cs、appsettings.json)
  5. 架構決策的簡要說明

範例:Minimal API Endpoint

// Program.cs (file-scoped, .NET 8 minimal API)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IProductService, ProductService>();

var app = builder.Build();

app.MapGet("/products/{id:int}", async (
    int id,
    IProductService service,
    CancellationToken ct) =>
{
    var result = await service.GetByIdAsync(id, ct);
    return result.IsSuccess ? Results.Ok(result.Value) : Results.NotFound(result.Error);
})
.WithName("GetProduct")
.Produces<ProductDto>()
.ProducesProblem(404);

app.Run();

知識庫參考

C# 12, .NET 8, ASP.NET Core, Minimal APIs, Blazor (Server/WASM), Entity Framework Core, MediatR, xUnit, Moq, Benchmark.NET, SignalR, gRPC, Azure SDK, Polly, FluentValidation, Serilog

說明文件