Skip to content
bitzorcas
EN

Guide

Add a feature

How to add a new feature to an existing module — command, query, handler, validation rule, and endpoint.

Last updated

This guide shows how to add a new feature (e.g., “archive a note”) to an existing module.

Feature components

Application/Sandbox/Notes/
├── ArchiveNoteCommand.cs → Command DTO
├── ArchiveNoteCommandHandler.cs → Handler
└── ArchiveNoteCommandRule.cs → Validation rule
Api/Endpoints/
├── NoteEndpoints.cs → Add archive endpoint

Step 1: Define the command

public record ArchiveNoteCommand(Guid NoteId) : IRequest<Result<Unit>>;

Step 2: Add validation rule

public class ArchiveNoteCommandRule : IRequestRule<ArchiveNoteCommand>
{
public IReadOnlyList<Error> Validate(ArchiveNoteCommand request)
{
if (request.NoteId == Guid.Empty)
return [Error.Validation("ArchiveNote.Id", "Note ID is required")];
return [];
}
}

Step 3: Implement the handler

public class ArchiveNoteCommandHandler : IRequestHandler<ArchiveNoteCommand, Result<Unit>>
{
public async ValueTask<Result<Unit>> Handle(
ArchiveNoteCommand request, CancellationToken ct)
{
// Business logic: validate ownership, set IsArchived, commit
return Result<Unit>.Success(Unit.Value);
}
}

Step 4: Add the endpoint

group.MapPost("/{id:guid}/archive", async (
Guid id, IMediator mediator) =>
{
var result = await mediator.Send(new ArchiveNoteCommand(id));
return result.ToProblemDetails();
});

Step 5: Register the rule

services.AddScoped<IRequestRule<ArchiveNoteCommand>, ArchiveNoteCommandRule>();

See also