Build and extend
Add a feature using a complete vertical slice
Use Catalog brand creation as a working model for contracts, validation, permissions and tests.
Choose the owning module and a reference operation
Start with an existing capability before creating a new module. This walkthrough uses Catalog's completed CreateBrand slice so every step can be compared with working source. For a new operation, choose its owning module, input, response, authorization and observable success before editing files.
A feature is complete when its API behavior and tests agree. A handler that compiles but is never mapped, or a visible button whose request is unauthorized, is unfinished.
Define the contract and input rules
The existing contract is in Modules.Catalog.Contracts/v1/Brands/CreateBrandCommand.cs:
public sealed record CreateBrandCommand(
string Name,
string? Description = null,
string? LogoUrl = null) : ICommand<Guid>;
Its runtime validator lives with the feature and derives from AbstractValidator<CreateBrandCommand>. It requires a nonempty name of at most 128 characters, description up to 1024, and logo URL up to 512. For your operation, define field limits and invalid combinations explicitly rather than passing arbitrary request data to persistence.
Keep contracts free of database-context or HTTP implementation details. The API already registers ValidationBehavior; module discovery registers validators from its supplied assemblies. A validator in an unregistered assembly will not help the request.
Implement business behavior in the handler
CreateBrandCommandHandler implements ICommandHandler<CreateBrandCommand, Guid>. It creates Brand, checks for an existing slug, throws a localized conflict for a duplicate, adds the entity and saves through CatalogDbContext.
For a new mutation, use the module's domain methods and context. Establish organizational access when the resource is scoped, and preserve tenant filtering. Add an explicit transaction when several writes must commit together. A domain event, outbox event or background job should be selected for a stated requirement, not added automatically to every CRUD operation.
Map the endpoint and permission
The existing route is mapped inside Catalog's versioned, authorized group:
return endpoints.MapPost("/brands",
async (CreateBrandCommand command, IMediator mediator, CancellationToken ct) =>
Results.Ok(await mediator.Send(command, ct)))
.WithName("CreateBrand")
.WithSummary("Create a brand")
.RequirePermission(CatalogPermissions.Brands.Create)
.WithIdempotency();
This excerpt is the body of the existing endpoint-extension method. Add the corresponding group.Map...Endpoint() call to your module entry class; creating the file does not map it. Register any new permission in the owning permission catalog. Read adding a permission before assigning it to roles.
If you add a new contract or runtime assembly, update Mediator composition. A feature added to an already registered assembly normally uses the existing registration. Do not manually instantiate the handler to bypass its pipeline.
Prove persistence, rejection and isolation
BrandsEndpointTests demonstrates creating a brand through HTTP, reading it back, checking its values and exercising duplicate, validation, deletion and restore behavior. Reuse the existing integration fixture and unique-data helpers so tests do not depend on order.
Run targeted checks while developing:
dotnet test src/Tests/Catalog.Tests -c Release
dotnet test src/Tests/Integration.Tests -c Release --filter FullyQualifiedName~BrandsEndpointTests
dotnet test src/Tests/Architecture.Tests -c Release
Integration tests need the documented Docker and browser prerequisites. These commands are instructions, not a claim that your new code has passed.
For your operation, cover permitted execution, invalid input, missing permission, another tenant and expired organizational scope where applicable. Check the stored state after failure as well as the HTTP response. Then add the client operation and localized UI through adding a page.