- Use
var
only when the type is obvious; otherwise, use explicit types. - Keep line length under 120 characters.
- Use consistent indentation and always include braces (
{}
) even for single-line statements. - Group
using
directives withSystem.*
first, then others in alphabetical order.
- Use
PascalCase
for component names, classes, methods, and properties. - Use
camelCase
for parameters and local variables. - Prefix private fields with
_
(e.g.,_userService
). - Blazor component files must match the component class name (e.g.,
MyComponent.razor
must containMyComponent
).
- Split large components into smaller, reusable child components.
- Use
@code { }
instead of@functions { }
. - Keep UI markup and C# logic separate when complexity grows (e.g., use partial classes).
- Avoid directly mutating bound parameters (
[Parameter]
) in child components. - Use
EventCallback<T>
instead ofAction
or custom delegates for parameter events. - Use
CascadingParameter
for passing data like authentication state, theme, or culture. - Prefer
OnInitializedAsync()
overOnInitialized()
when usingawait
.
- Organize components by domain/feature in folders (e.g.,
Pages/
,Components/
,Shared/
). - Follow the MVU or MVVM pattern when the state becomes complex.
- Use
@inject
for dependency injection rather than service locators. - Prefer
RenderFragment
overMarkupString
unless you need raw HTML rendering.
- Minimize re-rendering by using
ShouldRender()
or conditional UI logic. - Use
@key
in@foreach
loops to help Blazor track DOM elements. - Avoid using
async void
; useasync Task
instead. - Dispose components that use resources by implementing
IDisposable
.
- Never trust client-side validation—always validate on the server.
- Avoid exposing sensitive logic or secrets in
.razor
files. - Use
Microsoft.AspNetCore.Components.Authorization
for secure user authentication and role checking. - Use proper encoding when injecting raw HTML or third-party content.
- Prefer
RenderFragment
parameters to allow child content injection (similar to slot in other frameworks). - Isolate reusable logic in services or base classes.
- Use feature-based folders to group pages, components, and services.
- Use
bUnit
for unit testing Blazor components. - Mock services using
Moq
,FakeItEasy
, orNSubstitute
in test projects. - Use
IJSRuntime
abstraction for JavaScript interop, and mock it in tests. - Validate components for accessibility (ARIA, keyboard navigation).
- Use
@ref
cautiously to avoid tight coupling. - Enable detailed error messages in development mode.
- Use browser dev tools and Blazor’s built-in error boundaries.
- Follow the Arrange-Act-Assert pattern in unit tests.
- Ensure all services are injected through interfaces.
- Avoid static classes unless stateless and pure utility.
- Keep logic out of the UI layer when possible for easier testing.