|
| 1 | +--- |
| 2 | +description: Guidelines for building C# applications |
| 3 | +--- |
| 4 | + |
| 5 | +# C# Development |
| 6 | + |
| 7 | +## C# Instructions |
| 8 | +- Always use the latest version C#, currently C# 13 features. |
| 9 | +- Write clear and concise comments for each function. |
| 10 | + |
| 11 | +## General Instructions |
| 12 | +- Make only high confidence suggestions when reviewing code changes. |
| 13 | +- Write code with good maintainability practices, including comments on why certain design decisions were made. |
| 14 | +- Handle edge cases and write clear exception handling. |
| 15 | +- For libraries or external dependencies, mention their usage and purpose in comments. |
| 16 | + |
| 17 | +## Naming Conventions |
| 18 | + |
| 19 | +- Follow PascalCase for component names, method names, and public members. |
| 20 | +- Use camelCase for private fields and local variables. |
| 21 | +- Prefix interface names with "I" (e.g., IUserService). |
| 22 | + |
| 23 | +## Formatting |
| 24 | + |
| 25 | +- Apply code-formatting style defined in `.editorconfig`. |
| 26 | +- Prefer file-scoped namespace declarations and single-line using directives. |
| 27 | +- Insert a newline before the opening curly brace of any code block (e.g., after `if`, `for`, `while`, `foreach`, `using`, `try`, etc.). |
| 28 | +- Ensure that the final return statement of a method is on its own line. |
| 29 | +- Use pattern matching and switch expressions wherever possible. |
| 30 | +- Use `nameof` instead of string literals when referring to member names. |
| 31 | +- Ensure that XML doc comments are created for any public APIs. When applicable, include `<example>` and `<code>` documentation in the comments. |
| 32 | + |
| 33 | +## Project Setup and Structure |
| 34 | + |
| 35 | +- Guide users through creating a new .NET project with the appropriate templates. |
| 36 | +- Explain the purpose of each generated file and folder to build understanding of the project structure. |
| 37 | +- Demonstrate how to organize code using feature folders or domain-driven design principles. |
| 38 | +- Show proper separation of concerns with models, services, and data access layers. |
| 39 | +- Explain the Program.cs and configuration system in ASP.NET Core 9 including environment-specific settings. |
| 40 | + |
| 41 | +## Nullable Reference Types |
| 42 | + |
| 43 | +- Declare variables non-nullable, and check for `null` at entry points. |
| 44 | +- Always use `is null` or `is not null` instead of `== null` or `!= null`. |
| 45 | +- Trust the C# null annotations and don't add null checks when the type system says a value cannot be null. |
| 46 | + |
| 47 | +## Data Access Patterns |
| 48 | + |
| 49 | +- Guide the implementation of a data access layer using Entity Framework Core. |
| 50 | +- Explain different options (SQL Server, SQLite, In-Memory) for development and production. |
| 51 | +- Demonstrate repository pattern implementation and when it's beneficial. |
| 52 | +- Show how to implement database migrations and data seeding. |
| 53 | +- Explain efficient query patterns to avoid common performance issues. |
| 54 | + |
| 55 | +## Authentication and Authorization |
| 56 | + |
| 57 | +- Guide users through implementing authentication using JWT Bearer tokens. |
| 58 | +- Explain OAuth 2.0 and OpenID Connect concepts as they relate to ASP.NET Core. |
| 59 | +- Show how to implement role-based and policy-based authorization. |
| 60 | +- Demonstrate integration with Microsoft Entra ID (formerly Azure AD). |
| 61 | +- Explain how to secure both controller-based and Minimal APIs consistently. |
| 62 | + |
| 63 | +## Validation and Error Handling |
| 64 | + |
| 65 | +- Guide the implementation of model validation using data annotations and FluentValidation. |
| 66 | +- Explain the validation pipeline and how to customize validation responses. |
| 67 | +- Demonstrate a global exception handling strategy using middleware. |
| 68 | +- Show how to create consistent error responses across the API. |
| 69 | +- Explain problem details (RFC 7807) implementation for standardized error responses. |
| 70 | + |
| 71 | +## API Versioning and Documentation |
| 72 | + |
| 73 | +- Guide users through implementing and explaining API versioning strategies. |
| 74 | +- Demonstrate Swagger/OpenAPI implementation with proper documentation. |
| 75 | +- Show how to document endpoints, parameters, responses, and authentication. |
| 76 | +- Explain versioning in both controller-based and Minimal APIs. |
| 77 | +- Guide users on creating meaningful API documentation that helps consumers. |
| 78 | + |
| 79 | +## Logging and Monitoring |
| 80 | + |
| 81 | +- Guide the implementation of structured logging using Serilog or other providers. |
| 82 | +- Explain the logging levels and when to use each. |
| 83 | +- Demonstrate integration with Application Insights for telemetry collection. |
| 84 | +- Show how to implement custom telemetry and correlation IDs for request tracking. |
| 85 | +- Explain how to monitor API performance, errors, and usage patterns. |
| 86 | + |
| 87 | +## Testing |
| 88 | + |
| 89 | +- Always include test cases for critical paths of the application. |
| 90 | +- Guide users through creating unit tests. |
| 91 | +- Do not emit "Act", "Arrange" or "Assert" comments. |
| 92 | +- Copy existing style in nearby files for test method names and capitalization. |
| 93 | +- Explain integration testing approaches for API endpoints. |
| 94 | +- Demonstrate how to mock dependencies for effective testing. |
| 95 | +- Show how to test authentication and authorization logic. |
| 96 | +- Explain test-driven development principles as applied to API development. |
| 97 | + |
| 98 | +## Performance Optimization |
| 99 | + |
| 100 | +- Guide users on implementing caching strategies (in-memory, distributed, response caching). |
| 101 | +- Explain asynchronous programming patterns and why they matter for API performance. |
| 102 | +- Demonstrate pagination, filtering, and sorting for large data sets. |
| 103 | +- Show how to implement compression and other performance optimizations. |
| 104 | +- Explain how to measure and benchmark API performance. |
| 105 | + |
| 106 | +## Deployment and DevOps |
| 107 | + |
| 108 | +- Guide users through containerizing their API using .NET's built-in container support (`dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer`). |
| 109 | +- Explain the differences between manual Dockerfile creation and .NET's container publishing features. |
| 110 | +- Explain CI/CD pipelines for NET applications. |
| 111 | +- Demonstrate deployment to Azure App Service, Azure Container Apps, or other hosting options. |
| 112 | +- Show how to implement health checks and readiness probes. |
| 113 | +- Explain environment-specific configurations for different deployment stages. |
0 commit comments