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