backend-feature
Scaffolds backend features including new domain entities, new properties on existing entities, and new business operations. Use when creating a new domain entity, extending an existing entity with new
backend
feature
SKILL.md.claude/skills/backend-feature
GitHubBackend Feature
Four tasks:
- A new entity: run
scripts/new-feature.cs, then write the domain rules. - A new property on an entity that exists.
- A new business operation on an entity that exists.
- Files on an entity that exists: an attachment entity with its upload rules.
The files that the script writes are the reference for each pattern of a CRUD feature.
<!-- example:start -->The template also has a reference feature in FullstackTemplate.Server/Examples/Workshops and Examples/Venues. It shows each core pattern: a decision shape, a run again, a version, allowed actions, and the three entrypoints. CancelWorkshop and ScheduleWorkshop show each step of a new operation.
Read the rule doc of each area in docs/backend/ before you change it.
1. New Entity
Run the script
Ask the user for the properties and for the entrypoints of the entity: HTTP, gRPC, MCP, or none.
Run the script from the root of the repo:
dotnet run scripts/new-feature.cs -- SupplyItem --entrypoints http,mcp \ --fields "Name:text, Notes:text?, Code:text:public, Quantity:int, IsActive:bool, ReceivedOn:date, CountedAt:instant" dotnet run scripts/new-feature.cs -- Delivery --entrypoints http,mcp \ --fields "Title:text, SupplyItem:ref, DueOn:zoned-date"Run it with no arguments to see the options. The first property must be required text: the list sorts by it. A text, date, zoned date, or instant value is
[Sensitive]. Add:publicwhen the value cannot hold personal data. Protobuf has no date type, so the script refusesdateandzoned-datewithgrpc.zoned-dateis a day at the place of the entity, such as a due date. The script adds the propertyTimeZone, an IANA ID that the add sets. To get the current day at the place, useZonedDate.TodayAt(TimeZone, now), not the date of the server.SupplyItem:refis the propertySupplyItemId: the ID of aSupplyItemof the same tenant. Add the parent with the script first. The add loads the parent and refuses an ID that the caller cannot see. An update does not change the reference. An entity has at most oneref.
The script writes a tenant-owned root with add, get, list, update, and delete. It adds:
- The entity, the domain events, the models, the DTOs, the mapper, and the EF configuration.
- The five features and their entrypoints. There is no MCP delete tool, because a tool returns the DTO.
- The permission
manage_<plural>, and a migration that gives it to each admin. - The entity graph blueprint, the rules of the fakes, and the unit, integration, and functional tests.
- Lines in the shared files, at each
// new-feature:<slot>marker: theDbSet, the permission, the gRPC map,PermissionTests,StringColumnLengthTests,TenantDeleteOwnedRowsTests, andEndpointAccessSweepTests.
A second run with the same input changes nothing. When a file that the script writes exists with other content, the script stops before it writes. CI runs the script for two samples on each pull request, and then builds and tests them (the new-feature job). To run the same steps before you push a change to scripts/new-feature.cs or to a marker, run scripts/ci/new-feature.cs: dotnet run scripts/ci/new-feature.cs. It works on a throwaway copy of the checkout, so it does not change your files. It needs Docker and buf.
With examples in the template, a change to a feature can also fail the without-examples job. Run its steps with scripts/ci/examples/without-examples.cs and the new-feature job with --examples remove. See Run the CI jobs locally.
Write the domain rules
The script writes generic rules. Make them the rules of the domain:
- In the entity, set each max length, minimum, and maximum. The script writes each message from the names of the entity and the property, for example
Please give the supply item a value for "Received on".Rewrite a message when a user needs other words to understand it (docs/messages.md). Update the messages in the unit tests of the entity to match. - Check each
[Sensitive]and[NotSensitive]mark (docs/backend/audit-log.md). - Make each value that a person names into a value object, such as
EmailAddress(docs/backend/domain-entities.md). - Add the guards and the business methods. For a rule that two callers share, or a rule that reads more than one aggregate, use the decision shape (
docs/backend/features-and-cqrs.md#one-rule-set-for-two-callers). - Make the rules in
SharedTestHelpers/Fakes/<Plural>/<Entity>Rules.csgive realistic values. - For a
ref, decide what the delete of the parent does with the entities that refer to it: refuse, delete them too, or clear the reference. Run its load and save inRootLock.Tenant.RunExclusiveAsync(docs/backend/domain-events.md,docs/backend/concurrency.md). - If a unique value, a second foreign key, or a child entity is necessary, change the configuration and add a migration (
docs/backend/entity-configurations.md). - Regenerate and commit the API contract with the
api-contractskill. - Run
dotnet build FullstackTemplate.slnxanddotnet test --solution FullstackTemplate.slnx.
The script writes only a root that a tenant owns. For a child entity, an entity of all tenants, or a read model of a view, write the files by hand. Use the files of a generated feature as the pattern, and the checklist of docs/backend/audit-log.md.
2. New Property
Change each of these files:
| File | Change |
|---|---|
<Entity>.cs |
The property with a private setter, its max length or range constants, and its check in Validate<Entity> |
Models/<Entity>ForCreation.cs, <Entity>ForUpdate.cs |
The property, if it is set at creation or if it can change |
Dtos/<Entity>Dto.cs, <Entity>ForCreationDto.cs, <Entity>ForUpdateDto.cs |
The property. A text value has [SchemaMaxLength] |
Mappings/<Entity>Mapper.cs |
A map for a value object |
Databases/EntityConfigurations/<Entity>Configuration.cs |
HasMaxLength, or ComplexProperty for a value object |
| Each MCP tool and gRPC method that makes a creation or an update DTO | A parameter or a field for the property |
SharedTestHelpers/EntityGraphs/<Entity>Input.cs, <Entity>Blueprints.cs, and <Entity>Rules |
The input property, the factory value, and a Derive for a required value |
StringColumnLengthTests |
Each new text column, in CallerColumns, FixedOptionColumns, or ServerWrittenColumns |
Rules:
- Each text, date, or time value has
[Sensitive]or[NotSensitive]. A flag, an enum, aGuid, an integer, or a foreign key needs no mark. - Check a required text value with
ThrowWhenNullOrWhitespacefirst. Check each text value withThrowWhenLongerThanandThrowWhenHasCharacterPostgresCannotStore. Put the checks of a nullable value in anis not nullblock. - A new required parameter of an MCP tool that exists needs a new tool name. To keep the name, add an optional parameter with the default
nullafterCancellationToken("MCP versions" indocs/backend/endpoints.md). - A gRPC field that can have no value is
optional. ReadHas<Field>, and set the response field only when the value is not null. - Give each new text value a rule in
<Entity>Rulesthat fits the max length. OtherwiseAutoFakermakes text that is too long, and unrelated tests fail at random. - Add a migration:
dotnet tool exec dotnet-ef@<version> --yes -- migrations add Add<Property>To<Entity> --context AppDbContext --project FullstackTemplate.Server --startup-project FullstackTemplate.Server --output-dir Databases/Migrations. The version is theMicrosoft.EntityFrameworkCore.Designversion inDirectory.Packages.props. - Regenerate and commit the API contract with the
api-contractskill.
3. New Operation
- Choose where the rule lives. Use the decision shape when a second caller needs the same rules, or when a rule reads more than one aggregate. Otherwise, use a guard on the entity.
- Add the method to the entity. Queue a domain event with a name in the past tense. The event carries IDs and values, never an entity (
docs/backend/domain-events.md). - Pick the concurrency tool from
docs/backend/concurrency.md: a version, a run again (IRunsAgainWhenRaced), or a lock. - Write the command in
Features/<Action><Entity>/<Action><Entity>.cs. Make the checks in the order ofdocs/backend/features-and-cqrs.md#the-order-of-the-checks. The first statement is the permission check withIPermissionGuard. - If the SPA shows a button for the action, add the action to the action value object of the entity, and call
GuardAllowsafter the load (docs/backend/domain-entities.md#allowed-actions). - Add one adapter file for each entrypoint that the callers need (
docs/backend/endpoints.md). An MCP tool has exactly one[ToolAccess], which matches the check of the handler. - Add the telemetry with the
feature-telemetryskill. - Add the tests with the
writing-testsskill. A command that runs again or takes a lock has a race test.EndpointAccessSweepTestsmust pass for each new endpoint, tool, and gRPC method. - Regenerate and commit the API contract with the
api-contractskill.
A new permission needs a Permission.Names constant, a PermissionEnum entry, a factory method, its name in PermissionTests, and a migration that gives it to the admins. The script writes these lines for a new entity. Use them as the pattern.
4. Attachments
Follow "Add attachments to an entity" in attachments.md, on the Server and then in the SPA. The steps build on the parts in Domain/Attachments/: the entity declares only its rules, its key, and its owner, and the owner gets a Manage<Noun>s action that the add and the delete guard. Done when the functional tests of an upload, a refused type, a file whose bytes do not match its name, and a file over the largest size pass, and the list gives the upload rules.
Build rules
The build fails on these rules:
- Each entity has
public const string IdPrefixwith 1 to 17 lower-case ASCII letters. The prefix is unique. Each constructor callsbase(IdPrefix)with the IdPrefix of its own type (FSTID001-005). - An entity derives from
BaseEntity, or from an abstract base class with no IdPrefix. The constructor of the abstract class takes the parameteridPrefixand gives it toBaseEntity. - The text of a user message obeys
docs/messages.md(the user message analyzer). - A set-based write, raw SQL, or a bulk import uses the paths of
docs/backend/audit-log.md(RS0030). - Each query on a
DbSetofAppDbContexthasTagWithOperationCallSite(FSTTEL001 and FSTTEL003). - A query lambda in a handler compares a text or a list of texts of the request only after
EntityId.IsValidorPostgresText.CanStorechecks it, or checks each item of the list (FSTQRY001). - A request handler that calls
SaveChangesAsyncorSaveChangeshas a_logger.Here().Informationlog and anActivity.Current?.SetTag("app.*_id", ...)tag (FSTTEL002).