dotnet-best-practices
Scannednpx machina-cli add skill github/awesome-copilot/dotnet-best-practices --openclaw.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<T> 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
Source
git clone https://github.com/github/awesome-copilot/blob/main/plugins/csharp-dotnet-development/skills/dotnet-best-practices/SKILL.mdView on GitHub Overview
Provides concrete, project-specific guidelines to bring .NET/C# code in line with established standards. It covers documentation, architecture, DI, localization, async patterns, testing, configuration, AI integration, security, and code quality to improve maintainability and reliability.
How This Skill Works
It codifies actionable rules from the skill: XML documentation for public APIs, primary constructor DI, a Command Handler pattern with generic bases, and clear interface segregation. It also prescribes ResourceManager-based localization, async/await usage with ConfigureAwait(false), structured logging, and DI wiring via Microsoft.Extensions.DependencyInjection.
When to Use It
- When bootstrapping a new .NET project and establishing standard architecture
- When auditing an existing codebase to improve DI, testing, and SOLID compliance
- When localizing UI or error messages using ResourceManager with separated log/error resources
- When implementing AI features with Semantic Kernel and structured output patterns
- When enforcing secure coding, input validation, and parameterized database access
Quick Start
- Step 1: Define namespaces as {Core|Console|App|Service}.{Feature} and enable XML documentation across public APIs
- Step 2: Introduce constructor DI with null checks and register services with appropriate lifetimes in the DI container
- Step 3: Add ResourceManager-based localization, apply async/await patterns, and enforce structured logging and error handling
Best Practices
- Create XML documentation comments for all public classes, interfaces, methods, and properties, including parameter and return value descriptions, following the {Core|Console|App|Service}.{Feature} namespace structure
- Use primary constructor syntax for dependency injection and apply null checks in constructors (ArgumentNullException); register services with correct lifetimes (Singleton, Scoped, Transient) via Microsoft.Extensions.DependencyInjection
- Implement the Command Handler pattern with generic base classes (e.g., CommandHandler<TOptions>) and follow interface segregation with names prefixed by 'I'
- Use ResourceManager for localization with separate LogMessages and ErrorMessages resources; access via _resourceManager.GetString("MessageKey")
- Adopt async/await for I/O operations, return Task/Task<T>, use ConfigureAwait(false) where appropriate, and handle exceptions with proper try-catch and meaningful messages
Example Use Cases
- XML-docs added to public API surface with parameter and return descriptions and a consistent {Core|Console|App|Service}.{Feature} namespace
- Refactored dependencies to use the primary constructor syntax and wired via DI with ArgumentNullException checks
- Implemented a Command Handler pattern using a generic CommandHandler<TOptions> base class for command processing
- Localization implemented with ResourceManager; separate LogMessages and ErrorMessages resources accessed via GetString
- Async IO methods converted to return Task/Task<T> with ConfigureAwait(false) and structured exception handling