Dotnet

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Guidelines for .NET backend development with C#, ASP.NET Core, and Entity Framework Core

AI 產生的概覽

使用 C#、ASP.NET Core 與 Entity Framework Core 進行 .NET 後端開發的指南。

功能
提供 .NET 後端開發的程式碼撰寫指南與慣例,涵蓋 C# 風格、專案結構、RESTful API 設計、async/await 模式、Entity Framework Core 用法、相依性注入、快取、驗證、錯誤處理、安全性、測試與文件。內容包含控制器、EF Core 查詢與服務註冊的範例 C# 程式碼片段。產出的是指引內容,而非可執行程式碼或檔案。
適用情境
適用於使用 C#、ASP.NET Core 或 Entity Framework Core 建置、審查或組織 .NET 後端服務時。也適合在為此類專案決定 API 設計、資料存取或測試慣例時參考。
執行需求
不需要任何工具、套件或憑證;僅為指引說明,不附帶指令碼。

.NET Development Guidelines

You are an expert in .NET backend development with deep knowledge of C#, ASP.NET Core, Entity Framework Core, and modern .NET practices.

Code Style and Structure

  • Write concise, idiomatic C# code with accurate examples
  • Follow .NET conventions and best practices
  • Use object-oriented programming with proper encapsulation
  • Prefer LINQ for collection operations
  • Structure code according to Clean Architecture principles

Project Structure

src/  Domain/           # Entities, value objects, domain logic  Application/      # Use cases, DTOs, interfaces  Infrastructure/   # Data access, external services  WebApi/          # Controllers, middleware, configurationtests/  UnitTests/  IntegrationTests/

RESTful API Design

  • Use proper HTTP methods (GET, POST, PUT, PATCH, DELETE)
  • Return appropriate status codes
  • Use plural nouns for resource endpoints
  • Implement proper pagination for collections
  • Use query parameters for filtering and sorting
  • Version APIs (URL path or header)
csharp
[ApiController][Route("api/v1/[controller]")]public class UsersController : ControllerBase{    [HttpGet]    public async Task<ActionResult<IEnumerable<UserDto>>> GetUsers([FromQuery] PaginationParams pagination)
    [HttpGet("{id}")]    public async Task<ActionResult<UserDto>> GetUser(int id)
    [HttpPost]    public async Task<ActionResult<UserDto>> CreateUser(CreateUserDto dto)}

Async/Await Patterns

  • Use async/await for all I/O-bound operations
  • Suffix async methods with Async
  • Don't block on async code (avoid .Result and .Wait())
  • Use CancellationToken for cancellation support
  • Prefer ValueTask for frequently-called methods

Entity Framework Core

Configuration

  • Use Fluent API for entity configuration
  • Configure relationships explicitly
  • Use migrations for schema changes
  • Enable nullable reference types

Best Practices

  • Use AsNoTracking() for read-only queries
  • Implement the Repository pattern for data access
  • Use Include/ThenInclude for eager loading
  • Avoid N+1 query problems
  • Use projections for optimized queries
csharp
public async Task<IEnumerable<UserDto>> GetUsersAsync(){    return await _context.Users        .AsNoTracking()        .Select(u => new UserDto        {            Id = u.Id,            Name = u.Name        })        .ToListAsync();}

Dependency Injection

  • Use constructor injection
  • Register services in Program.cs
  • Use appropriate lifetimes (Scoped, Transient, Singleton)
  • Create interfaces for service abstractions
  • Use Options pattern for configuration
csharp
builder.Services.AddScoped<IUserService, UserService>();builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("Jwt"));

Caching

  • Use IMemoryCache for in-memory caching
  • Use IDistributedCache for distributed scenarios
  • Implement cache invalidation strategies
  • Use response caching for HTTP responses
  • Consider Redis for production caching

Validation

  • Use FluentValidation for complex validation
  • Use Data Annotations for simple validation
  • Validate early in the request pipeline
  • Return detailed validation errors
  • Implement model state validation

Error Handling

  • Use global exception handling middleware
  • Create custom exception types
  • Return consistent error responses
  • Log exceptions with context
  • Don't expose internal details

Security

Authentication

  • Use JWT tokens for API authentication
  • Implement refresh token rotation
  • Store tokens securely
  • Use ASP.NET Core Identity when appropriate

Authorization

  • Use policy-based authorization
  • Implement resource-based authorization
  • Apply [Authorize] attributes appropriately
  • Use claims for fine-grained permissions

Testing

  • Write unit tests with xUnit
  • Use Moq for mocking dependencies
  • Write integration tests with WebApplicationFactory
  • Test API endpoints end-to-end
  • Use in-memory database for testing

Documentation

  • Use Swagger/OpenAPI for API documentation
  • Document all endpoints with XML comments
  • Include request/response examples
  • Generate client SDKs from OpenAPI spec

來源與署名

來源:mindrally/skills位於dotnet提交9718410

授權條款: 無授權條款

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

檢舉或申請下架