Skip to content

project rulesFrontendcsharpdotnet

.NET Blazor Rules

Project rules for Blazor apps: small components, scoped state, injected services, EditForm validation and bUnit component tests.

npx workspacetemplates add dotnet-blazor-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file19 lines · 1.3 KBFile
# Project rules: Blazor

## Components
- One component per `.razor` file with a code-behind `.razor.cs` partial class once logic passes a few lines. Keep markup free of business rules.
- Use `[Parameter]` for inputs and `EventCallback<T>` for outputs. Mark required inputs with `[EditorRequired]`. Never mutate a parameter from inside the child.
- Override `OnInitializedAsync` for loading; call `StateHasChanged` only after work done outside the render cycle.

## Services and state
- Inject services with `@inject` or `[Inject]`; never use `new` for a service. Keep HTTP and data access in typed services behind interfaces.
- Share state with a scoped service that raises a change event, and unsubscribe by implementing `IDisposable`. Do not use static fields for user data.
- Pass a `CancellationToken` through async calls.

## Forms
- Use `EditForm` with `DataAnnotationsValidator` or FluentValidation, and show `ValidationMessage`. Disable submit while a request runs.

## Quality
- Enable nullable reference types. Use `@key` on list items. Prefer `<Virtualize>` for long lists.
- Write bUnit tests for each component: render, interact, assert markup and callbacks. Mock services with interfaces.
- Run `dotnet build`, `dotnet test` and `dotnet format --verify-no-changes` before calling work done.

What it does

These rules guide an agent building Blazor Server or WebAssembly interfaces. They keep components small, move logic into injected services and make forms and state behave the same way across the app.

They also cover the test side, so every new component arrives with a bUnit test that renders it and checks the markup and events.

Example

you › Add an editable customer profile page

agent › The agent adds a ProfileForm component with EditForm validation, a typed ProfileService, and a bUnit test that submits invalid and valid input.

Questions

Does it work for Server and WebAssembly?
Yes. The component and testing rules are the same; only service registration differs.
Why scoped state services?
They give each user session its own state and make components easy to test with a fake.
  • Angular Rules

    Project rulesangular

    Free
  • Astro Rules

    Project rulesastro

    Free
  • Electron Rules

    Project ruleselectron

    Free
  • Next.js App Router Rules

    Project rulesnextjs

    Free