$npx -y skills add github/awesome-copilot --skill dotnet-best-practicesdotnet-best-practices is an agent skill published from github/awesome-copilot, installable through the skills CLI.
| 1 | # .NET/C# Best Practices |
| 2 | |
| 3 | Your task is to ensure .NET/C# code in ${selection} meets the best practices specific to this solution/project. This includes: |
| 4 | |
| 5 | ## Documentation & Structure |
| 6 | |
| 7 | - Create comprehensive XML documentation comments for all public classes, interfaces, methods, and properties |
| 8 | - Include parameter descriptions and return value descriptions in XML comments |
| 9 | - Follow the established namespace structure: {Core|Console|App|Service}.{Feature} |
| 10 | |
| 11 | ## Design Patterns & Architecture |
| 12 | |
| 13 | - Use primary constructor syntax for dependency injection (e.g., `public class MyClass(IDependency dependency)`) |
| 14 | - Implement the Command Handler pattern with generic base classes (e.g., `CommandHandler<TOptions>`) |
| 15 | - Use interface segregation with clear naming conventions (prefix interfaces with 'I') |
| 16 | - Follow the Factory pattern for complex object creation. |
| 17 | |
| 18 | ## Dependency Injection & Services |
| 19 | |
| 20 | - Use constructor dependency injection with null checks via ArgumentNullException |
| 21 | - Register services with appropriate lifetimes (Singleton, Scoped, Transient) |
| 22 | - Use Microsoft.Extensions.DependencyInjection patterns |
| 23 | - Implement service interfaces for testability |
| 24 | |
| 25 | ## Resource Management & Localization |
| 26 | |
| 27 | - Use ResourceManager for localized messages and error strings |
| 28 | - Separate LogMessages and ErrorMessages resource files |
| 29 | - Access resources via `_resourceManager.GetString("MessageKey")` |
| 30 | |
| 31 | ## Async/Await Patterns |
| 32 | |
| 33 | - Use async/await for all I/O operations and long-running tasks |
| 34 | - Return Task or Task<T> from async methods |
| 35 | - Use ConfigureAwait(false) where appropriate |
| 36 | - Handle async exceptions properly |
| 37 | |
| 38 | ## Testing Standards |
| 39 | |
| 40 | - Use MSTest framework with FluentAssertions for assertions |
| 41 | - Follow AAA pattern (Arrange, Act, Assert) |
| 42 | - Use Moq for mocking dependencies |
| 43 | - Test both success and failure scenarios |
| 44 | - Include null parameter validation tests |
| 45 | |
| 46 | ## Configuration & Settings |
| 47 | |
| 48 | - Use strongly-typed configuration classes with data annotations |
| 49 | - Implement validation attributes (Required, NotEmptyOrWhitespace) |
| 50 | - Use IConfiguration binding for settings |
| 51 | - Support appsettings.json configuration files |
| 52 | |
| 53 | ## Semantic Kernel & AI Integration |
| 54 | |
| 55 | - Use Microsoft.SemanticKernel for AI operations |
| 56 | - Implement proper kernel configuration and service registration |
| 57 | - Handle AI model settings (ChatCompletion, Embedding, etc.) |
| 58 | - Use structured output patterns for reliable AI responses |
| 59 | |
| 60 | ## Error Handling & Logging |
| 61 | |
| 62 | - Use structured logging with Microsoft.Extensions.Logging |
| 63 | - Include scoped logging with meaningful context |
| 64 | - Throw specific exceptions with descriptive messages |
| 65 | - Use try-catch blocks for expected failure scenarios |
| 66 | |
| 67 | ## Performance & Security |
| 68 | |
| 69 | - Use C# 12+ features and .NET 8 optimizations where applicable |
| 70 | - Implement proper input validation and sanitization |
| 71 | - Use parameterized queries for database operations |
| 72 | - Follow secure coding practices for AI/ML operations |
| 73 | |
| 74 | ## Code Quality |
| 75 | |
| 76 | - Ensure SOLID principles compliance |
| 77 | - Avoid code duplication through base classes and utilities |
| 78 | - Use meaningful names that reflect domain concepts |
| 79 | - Keep methods focused and cohesive |
| 80 | - Implement proper disposal patterns for resources |