Dotnet Core Expert

作者 jeffallan1be15d8064f8MIT11K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 天前更新

Use when building .NET 8 applications with minimal APIs, clean architecture, or cloud-native microservices. Invoke for Entity Framework Core, CQRS with MediatR, JWT authentication, AOT compilation.

AI 產生的概覽

指導使用最小 API、整潔架構、EF Core、CQRS 與 JWT 驗證建置 .NET 8 應用程式。

功能
提供實作 .NET 8 後端應用程式的工作流程與參考指引,涵蓋最小 API、整潔架構分層、Entity Framework Core、驗證與雲原生相關主題。它提供端點、MediatR 查詢處理常式、DbContext 用法與 record DTO 的程式碼範例,並列出必須做與禁止做的限制。產出為實作程式碼以及對架構決策的簡要說明。
適用情境
適用於建置或擴充 .NET 8 或 ASP.NET Core 服務,尤其是使用最小 API、MediatR 的 CQRS、Entity Framework Core 或 JWT 驗證的情境。也適合 Docker、健康檢查與設定等雲原生微服務工作。
執行需求
需要 .NET 8 SDK 與 C# 12 工具鏈,包括 dotnet build 與 dotnet test,以及用於整合測試的 xUnit 與 WebApplicationFactory。參考檔案由模型讀取;不隨附指令碼。

.NET Core Expert

Core Workflow

  1. Analyze requirements — Identify architecture pattern, data models, API design
  2. Design solution — Create clean architecture layers with proper separation
  3. Implement — Write high-performance code with modern C# features; run dotnet build to verify compilation — if build fails, review errors, fix issues, and rebuild before proceeding
  4. Secure — Add authentication, authorization, and security best practices
  5. Test — Write comprehensive tests with xUnit and integration testing; run dotnet test to confirm all tests pass — if tests fail, diagnose failures, fix the implementation, and re-run before continuing; verify endpoints with curl or a REST client

Reference Guide

Load detailed guidance based on context:

TopicReferenceLoad When
Minimal APIsreferences/minimal-apis.mdCreating endpoints, routing, middleware
Clean Architecturereferences/clean-architecture.mdCQRS, MediatR, layers, DI patterns
Entity Frameworkreferences/entity-framework.mdDbContext, migrations, relationships
Authenticationreferences/authentication.mdJWT, Identity, authorization policies
Cloud-Nativereferences/cloud-native.mdDocker, health checks, configuration

Constraints

MUST DO

  • Use .NET 8 and C# 12 features
  • Enable nullable reference types: <Nullable>enable</Nullable> in the .csproj
  • Use async/await for all I/O operations — e.g., await dbContext.Users.ToListAsync()
  • Implement proper dependency injection
  • Use record types for DTOs — e.g., public record UserDto(int Id, string Name);
  • Follow clean architecture principles
  • Write integration tests with WebApplicationFactory<Program>
  • Configure OpenAPI/Swagger documentation

MUST NOT DO

  • Use synchronous I/O operations
  • Expose entities directly in API responses
  • Skip input validation
  • Use legacy .NET Framework patterns
  • Mix concerns across architectural layers
  • Use deprecated EF Core patterns

Code Examples

Minimal API Endpoint

csharp
// Program.csvar 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 Handler

csharp
// Application/Users/GetUserQuery.cspublic 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 with Async Query

csharp
// Infrastructure/AppDbContext.cspublic 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);    }}
// Usage in a servicepublic async Task<IReadOnlyList<UserDto>> GetAllAsync(CancellationToken ct) =>    await _db.Users        .AsNoTracking()        .Select(u => new UserDto(u.Id, u.Name))        .ToListAsync(ct);

DTO with Record Type

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

Output Templates

When implementing .NET features, provide:

  1. Project structure (solution/project files)
  2. Domain models and DTOs
  3. API endpoints or service implementations
  4. Database context and migrations if applicable
  5. Brief explanation of architectural decisions

Maintained by @jeffallan, Principal Consultant at Synergetic Solutions

Documentation

來源與署名

來源:jeffallan/claude-skills位於skills/dotnet-core-expert提交1be15d8

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架