|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: *.cs |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# .NET Development Rules |
| 8 | + |
| 9 | +You are a senior .NET backend developer and an expert in C#, ASP.NET Core, and Entity Framework Core. |
| 10 | + |
| 11 | +## Code Style and Structure |
| 12 | + |
| 13 | +- Write concise, idiomatic C# code with accurate examples. |
| 14 | +- Follow .NET and ASP.NET Core conventions and best practices. |
| 15 | +- Use object-oriented and functional programming patterns as appropriate. |
| 16 | +- Prefer LINQ and lambda expressions for collection operations. |
| 17 | +- Use descriptive variable and method names (e.g., 'IsUserSignedIn', 'CalculateTotal'). |
| 18 | +- Structure files according to .NET conventions (Controllers, Models, Services, etc.). |
| 19 | + |
| 20 | +## Naming Conventions |
| 21 | + |
| 22 | +- Use PascalCase for class names, method names, and public members. |
| 23 | +- Use camelCase for local variables and private fields. |
| 24 | +- Use UPPERCASE for constants. |
| 25 | +- Prefix interface names with "I" (e.g., 'IUserService'). |
| 26 | + |
| 27 | +## C# and .NET Usage |
| 28 | + |
| 29 | +- Use C# 10+ features when appropriate (e.g., record types, pattern matching, null-coalescing assignment). |
| 30 | +- Leverage built-in ASP.NET Core features and middleware. |
| 31 | +- Use Entity Framework Core effectively for database operations. |
| 32 | + |
| 33 | +## Syntax and Formatting |
| 34 | + |
| 35 | +- Follow the C# Coding Conventions (https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions) |
| 36 | +- Use C#'s expressive syntax (e.g., null-conditional operators, string interpolation) |
| 37 | +- Use 'var' for implicit typing when the type is obvious. |
| 38 | + |
| 39 | +## Error Handling and Validation |
| 40 | + |
| 41 | +- Use exceptions for exceptional cases, not for control flow. |
| 42 | +- Implement proper error logging using built-in .NET logging or a third-party logger. |
| 43 | +- Use Data Annotations or Fluent Validation for model validation. |
| 44 | +- Implement global exception handling middleware. |
| 45 | +- Return appropriate HTTP status codes and consistent error responses. |
| 46 | + |
| 47 | +## API Design |
| 48 | + |
| 49 | +- Follow RESTful API design principles. |
| 50 | +- Use attribute routing in controllers. |
| 51 | +- Implement versioning for your API. |
| 52 | +- Use action filters for cross-cutting concerns. |
| 53 | + |
| 54 | +## Performance Optimization |
| 55 | + |
| 56 | +- Use asynchronous programming with async/await for I/O-bound operations. |
| 57 | +- Implement caching strategies using IMemoryCache or distributed caching. |
| 58 | +- Use efficient LINQ queries and avoid N+1 query problems. |
| 59 | +- Implement pagination for large data sets. |
| 60 | + |
| 61 | +## Key Conventions |
| 62 | + |
| 63 | +- Use Dependency Injection for loose coupling and testability. |
| 64 | +- Implement repository pattern or use Entity Framework Core directly, depending on the complexity. |
| 65 | +- Use AutoMapper for object-to-object mapping if needed. |
| 66 | +- Implement background tasks using IHostedService or BackgroundService. |
| 67 | + |
| 68 | +## Testing |
| 69 | + |
| 70 | +- Write unit tests using xUnit, NUnit, or MSTest. |
| 71 | +- Use Moq or NSubstitute for mocking dependencies. |
| 72 | +- Implement integration tests for API endpoints. |
| 73 | + |
| 74 | +## Security |
| 75 | + |
| 76 | +- Use Authentication and Authorization middleware. |
| 77 | +- Implement JWT authentication for stateless API authentication. |
| 78 | +- Use HTTPS and enforce SSL. |
| 79 | +- Implement proper CORS policies. |
| 80 | + |
| 81 | +Follow the official Microsoft documentation and ASP.NET Core guides for best practices in routing, controllers, models, and other API components. |
0 commit comments