add-a-use-case
Skill1 starsChanged 7 days agoMITView on GitHub
How to add a command or query to the Application layer as a vertical slice (request, handler, validator, tests) using source-generated Mediator. Use when adding a new feature, a new query, or a new write operation.
In this repository40 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- api-conventionsSkill
- branches-and-pull-requestsSkill
- cancellationSkill
- ci-for-your-solutionSkill
- ci-pipelineSkill
- clean-architecture-layersSkill
- configuration-and-environmentsSkill
- coverageSkill
- csharp-house-styleSkill
- data-access-ef-coreSkill
- database-migrationsSkill
- docker-and-composeSkill
- docs-and-voiceSkill
- errors-and-exceptionsSkill
- expose-a-use-caseSkill
- family-syncSkill
- health-checksSkill
- integration-testsSkill
- json-serializationSkill
- mapping-with-mapperlySkill
- mcp-inspectorSkill
- mediatorSkill
- optional-token-authSkill
- packages-and-dependenciesSkill
- pagingSkill
- presentation-integration-testsSkill
- releasesSkill
- sample-dataSkill
- security-findingsSkill
- serilogSkill
- telemetrySkill
- template-authoringSkill
- test-designSkill
- time-providerSkill
- unit-testsSkill
- validationSkill
- writing-a-skillSkill
- xml-docs-and-openapiSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 2 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Every use case is one folder holding three files: the request, its handler and its validator.
- Very specific: line 24
Create the folder under Commands or Queries, named for the use case (UpdateReview, GetMovieById).
- Very specific: line 25
Write the request: a class with init properties implementing IRequest<TResponse>, with an XML summary on the class and each property …
- Very specific: line 26
Write the validator (validation).
- Very specific: line 27
Write the handler with a primary constructor that takes repository interfaces.
- Very specific: line 28
If the handler needs something new from storage, add it to the repository interface, then implement it in Infrastructure …
- Very specific: line 29
Add tests: tests/Application.Tests.Unit/<Feature>/<Commands|Queries>/<UseCase>/ with <UseCase>HandlerTests.cs and …
- Very specific: line 78
Use MediatR. It's commercially licensed now.
- Distinctive: line 79
Put two use cases in one handler, or share a request between a read and a write.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Add a use case
The rule
Every use case is one folder holding three files: the request, its handler and its validator. Reads are queries, writes are commands. Mediator (the source-generated one, not MediatR) finds handlers at compile time, and ValidationBehaviour runs the validator before the handler (mediator).
src/Application/<Feature>/
├── Commands/<Verb><Thing>/ <Verb><Thing>Command.cs, ...Handler.cs, ...Validator.cs
├── Queries/Get<Thing>/ Get<Thing>Query.cs, ...Handler.cs, ...Validator.cs
├── Entities/ records returned to callers
└── I<Feature>Repository.cs what the handlers need from storageSteps
- Create the folder under
CommandsorQueries, named for the use case (UpdateReview,GetMovieById). - Write the request: a class with
initproperties implementingIRequest<TResponse>, with an XML summary on the class and each property (xml-docs-and-openapi). - Write the validator (
validation). Every request gets one, even if it only checks an id. - Write the handler with a primary constructor that takes repository interfaces. Return
ValueTask<TResponse>. Pass theCancellationTokento every call (cancellation). - If the handler needs something new from storage, add it to the repository interface, then implement it in Infrastructure (
data-access-ef-core). - Add tests:
tests/Application.Tests.Unit/<Feature>/<Commands|Queries>/<UseCase>/with<UseCase>HandlerTests.csand<UseCase>ValidatorTests.cs, covering every success and failure (test-design,unit-tests). - Expose it in Presentation (
expose-a-use-case).
Example
// src/Application/Movies/Queries/GetMovies/GetMoviesQuery.cs
/// <summary>
/// Lists movies one page at a time, optionally only those whose title contains a search term.
/// </summary>
public class GetMoviesQuery : IRequest<Page<MovieSummary>>
{
/// <summary>The words to look for in the title, ignoring case. Leave empty to list every movie.</summary>
public string? Query { get; init; }
/// <summary>The number of results to skip, 0 for the first page.</summary>
public int Offset { get; init; }
/// <summary>The largest number of results to return, from 1 to 50.</summary>
public int Limit { get; init; } = Paging.DefaultLimit;
}// src/Application/Movies/Queries/GetMovies/GetMoviesHandler.cs
public class GetMoviesHandler(IMoviesRepository repository) : IRequestHandler<GetMoviesQuery, Page<MovieSummary>>
{
public async ValueTask<Page<MovieSummary>> Handle(GetMoviesQuery request, CancellationToken cancellationToken)
{
var (items, totalCount) = await repository.GetMovies(request.Query, request.Offset, request.Limit, cancellationToken);
return new Page<MovieSummary>(items, request.Offset, request.Limit, totalCount);
}
}A by-id query fetches and throws when nothing comes back:
// src/Application/Reviews/Queries/GetReviewById/GetReviewByIdHandler.cs
var result = await repository.GetReviewById(request.Id, cancellationToken);
NotFoundException.ThrowIfNull(result, EntityType.Review);
return result;Don't
- Use MediatR. It's commercially licensed now; we use
Mediator.AbstractionsandMediator.SourceGenerator. - Put two use cases in one handler, or share a request between a read and a write.
- Return database models or
IQueryablefrom a handler. - Validate inside the handler. Rules go in the validator; the handler only checks things that need storage (does the author exist?).
- Forget the validator. A request with no rules still gets one that checks its ids.
Check
dotnet build
dotnet test tests/Application.Tests.UnitDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

