|
| 1 | +--- |
| 2 | +applyTo: '**/*.razor, **/*.razor.cs, **/*.razor.css' |
| 3 | + |
| 4 | +description: 'This file contains instructions for Blazor development. |
| 5 | + It includes guidelines for component development, performance optimization, and following Blazor coding standards. |
| 6 | + Ensure to follow the practices outlined in this file to maintain code quality and consistency.' |
| 7 | +--- |
| 8 | + |
| 9 | +# Blazor Development Instructions |
| 10 | + |
| 11 | +## General |
| 12 | + |
| 13 | +* MUST write idiomatic and efficient Blazor and C# code. |
| 14 | +* MUST follow .NET and Blazor conventions and best practices. |
| 15 | +* MUST use Razor Components appropriately for component-based UI development. |
| 16 | +* MUST use the latest version C#, currently C# 13 features like record types, pattern matching, and global usings. |
| 17 | +* MUST use async/await where applicable to ensure non-blocking UI operations. |
| 18 | + |
| 19 | +## Code Style and Structure |
| 20 | + |
| 21 | +* MUST prefer inline functions for smaller components but separate complex logic into code-behind or service classes. |
| 22 | +* MUST structure Blazor components and services following Separation of Concerns principles. |
| 23 | +* MUST follow PascalCase for component names, method names, and public members. |
| 24 | +* MUST use camelCase for private fields and local variables. |
| 25 | +* MUST prefix interface names with "I" (e.g., `IUserService`). |
| 26 | + |
| 27 | +## Component Lifecycle and Data Binding |
| 28 | + |
| 29 | +* MUST utilize Blazor's built-in features for component lifecycle (e.g., `OnInitializedAsync`, `OnParametersSetAsync`). |
| 30 | +* MUST use data binding effectively with `@bind` directive. |
| 31 | +* MUST leverage Dependency Injection for services in Blazor components. |
| 32 | +* MUST use `EventCallbacks` for handling user interactions efficiently, passing only minimal data when triggering events. |
| 33 | + |
| 34 | +## Performance Optimization |
| 35 | + |
| 36 | +* MUST optimize Razor components by reducing unnecessary renders and using `StateHasChanged()` efficiently. |
| 37 | +* MUST minimize the component render tree by avoiding re-renders unless necessary, using `ShouldRender()` where appropriate. |
| 38 | +* MUST use asynchronous methods (`async`/`await`) for API calls or UI actions that could block the main thread. |
| 39 | +* MUST utilize Blazor server-side or WebAssembly optimally based on the project requirements. |
| 40 | + |
| 41 | +## Error Handling and Validation |
| 42 | + |
| 43 | +* MUST implement proper error handling for Blazor pages and API calls using try-catch blocks. |
| 44 | +* MUST use logging for error tracking in the backend and consider capturing UI-level errors in Blazor with tools like `ErrorBoundary`. |
| 45 | +* MUST implement validation using FluentValidation or DataAnnotations in forms. |
| 46 | +* MUST provide proper user feedback in the UI for error conditions. |
| 47 | + |
| 48 | +## Caching Strategies |
| 49 | + |
| 50 | +* MUST implement in-memory caching for frequently used data, especially for Blazor Server apps using `IMemoryCache`. |
| 51 | +* MUST utilize `localStorage` or `sessionStorage` to cache application state between user sessions for Blazor WebAssembly. |
| 52 | +* MUST consider Distributed Cache strategies (like Redis or SQL Server Cache) for larger applications that need shared state. |
| 53 | +* MUST cache API calls by storing responses to avoid redundant calls when data is unlikely to change. |
| 54 | + |
| 55 | +## State Management |
| 56 | + |
| 57 | +* MUST use Blazor's built-in Cascading Parameters and EventCallbacks for basic state sharing across components. |
| 58 | +* MUST implement advanced state management solutions using libraries like Fluxor or BlazorState when the application grows in complexity. |
| 59 | +* MUST use Blazored.LocalStorage or Blazored.SessionStorage for client-side state persistence in Blazor WebAssembly. |
| 60 | +* MUST use Scoped Services and the StateContainer pattern for server-side Blazor to manage state within user sessions. |
| 61 | + |
| 62 | +## API Integration |
| 63 | + |
| 64 | +* MUST use `HttpClient` or other appropriate services to communicate with external APIs or backend services. |
| 65 | +* MUST implement comprehensive error handling for API calls and provide meaningful user feedback. |
| 66 | +* MUST use HTTPS for all web communication and ensure proper CORS policies are implemented. |
| 67 | + |
| 68 | +## Security and Authentication |
| 69 | + |
| 70 | +* MUST implement Authentication and Authorization in Blazor applications where necessary using ASP.NET Identity or JWT tokens. |
| 71 | +* MUST ensure proper security measures are in place for API authentication and data protection. |
| 72 | +* MUST validate all user inputs and implement proper authorization checks. |
| 73 | + |
| 74 | +## Testing and Debugging |
| 75 | + |
| 76 | +* MUST perform all unit testing and integration testing using Visual Studio Enterprise. |
| 77 | +* MUST test Blazor components and services using TUnit (preferred) or xUnit/NUnit/MSTest. |
| 78 | +* MUST use appropriate mocking frameworks for testing dependencies. |
| 79 | +* MUST debug Blazor UI issues using browser developer tools and Visual Studio's debugging tools. |
| 80 | +* MUST use Visual Studio's diagnostics tools for performance profiling and optimization. |
| 81 | + |
| 82 | +## Documentation |
| 83 | + |
| 84 | +* MUST use Swagger/OpenAPI for API documentation for backend API services. |
| 85 | +* MUST ensure XML documentation for models and API methods to enhance Swagger documentation. |
| 86 | +* MUST document complex component logic and business rules for maintainability. |
0 commit comments