dotnet-best-practices
Ensure .NET/C# code meets best practices for the solution/project.
摘要
- 用途
- 協助 .NET/C# 程式碼遵循專案在文件、架構、相依性注入、非同步作業、測試、設定、本地化、記錄、安全性與可維護性方面的慣例。
- 使用時機
- 在實作或檢視涉及服務、測試、設定、非同步作業、錯誤處理或其他專案慣例的 .NET/C# 變更時,請使用此指引。
- 輸出結果
- 產出的程式碼會遵循適用於文件、設計、服務、測試、設定、非同步作業、記錄、安全性與資源管理的專案慣例。
上游變更(3 筆,最新收錄 2026-02-19)
caab1f6 Convert all prompts to skills and update plugin manifests 新增 .NET/C# 最佳實務技能,涵蓋文件、架構、相依性注入、本地化、非同步處理、測試、組態、Semantic Kernel、錯誤處理、安全性與程式碼品質。 2f9c96d Fix deprecated 'mode' attribute in prompt files by renaming to 'agent' (#441) 將提示詞前置中繼資料欄位從 `mode` 改名為 `agent`,並保留值 `agent`。 e1c7c7a Add 18 new prompts files (#56) 新增 .NET/C# 最佳實務提示詞,涵蓋文件、架構、相依性注入、本地化、非同步處理、測試、組態、Semantic Kernel、錯誤處理、安全性與程式碼品質。 npx skills add "lettucebo/Skills#v2.2.0@dotnet-best-practices" --full-depth ⚠ v2.2.0 發布後即可使用
.NET/C# Best Practices
Your task is to ensure .NET/C# code in ${selection} meets the best practices specific to this solution/project. This includes:
Documentation & Structure
- Create comprehensive XML documentation comments for all public classes, interfaces, methods, and properties
- Include parameter descriptions and return value descriptions in XML comments
- Follow the established namespace structure: {Core|Console|App|Service}.{Feature}
Design Patterns & Architecture
- Use primary constructor syntax for dependency injection (e.g.,
public class MyClass(IDependency dependency)) - Implement the Command Handler pattern with generic base classes (e.g.,
CommandHandler<TOptions>) - Use interface segregation with clear naming conventions (prefix interfaces with 'I')
- Follow the Factory pattern for complex object creation.
Dependency Injection & Services
- Use constructor dependency injection with null checks via ArgumentNullException
- Register services with appropriate lifetimes (Singleton, Scoped, Transient)
- Use Microsoft.Extensions.DependencyInjection patterns
- Implement service interfaces for testability
Resource Management & Localization
- Use ResourceManager for localized messages and error strings
- Separate LogMessages and ErrorMessages resource files
- Access resources via
_resourceManager.GetString("MessageKey")
Async/Await Patterns
- Use async/await for all I/O operations and long-running tasks
- Return Task or Task from async methods
- Use ConfigureAwait(false) where appropriate
- Handle async exceptions properly
Testing Standards
- Use MSTest framework with FluentAssertions for assertions
- Follow AAA pattern (Arrange, Act, Assert)
- Use Moq for mocking dependencies
- Test both success and failure scenarios
- Include null parameter validation tests
Configuration & Settings
- Use strongly-typed configuration classes with data annotations
- Implement validation attributes (Required, NotEmptyOrWhitespace)
- Use IConfiguration binding for settings
- Support appsettings.json configuration files
Semantic Kernel & AI Integration
- Use Microsoft.SemanticKernel for AI operations
- Implement proper kernel configuration and service registration
- Handle AI model settings (ChatCompletion, Embedding, etc.)
- Use structured output patterns for reliable AI responses
Error Handling & Logging
- Use structured logging with Microsoft.Extensions.Logging
- Include scoped logging with meaningful context
- Throw specific exceptions with descriptive messages
- Use try-catch blocks for expected failure scenarios
Performance & Security
- Use C# 12+ features and .NET 8 optimizations where applicable
- Implement proper input validation and sanitization
- Use parameterized queries for database operations
- Follow secure coding practices for AI/ML operations
Code Quality
- Ensure SOLID principles compliance
- Avoid code duplication through base classes and utilities
- Use meaningful names that reflect domain concepts
- Keep methods focused and cohesive
- Implement proper disposal patterns for resources