agentleFS
joaoguimaraesprojoaoguimaraespro/personal-finance/.agents/skills/dotnet-create-command-query/SKILL.md

dotnet-create-command-query

Skill1 starsChanged todayMITView on GitHub

Create a CQRS command (with handler and validator) or query (with handler and response) in a .NET module, returning Result, named <Verb><Entity>Command/Query, plus the endpoint that sends it. Use when adding a use case, an endpoint or a read model to a module's Application layer.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Test first: the handler test (success and each Error) and, for a new endpoint, an HTTP test through WebApplicationFactory.

    line 13
  2. Very specific:

    Update: load by id (tenant-filtered), NotFound error when missing, call entity methods, save.

    line 65

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Create a command or a query

Inputs: command or query, verb (Create, Update, Archive, Get, List, Search...), entity, module. First read one existing command and one query in the same module and copy their shape: mediator interfaces (ICommand<T>/IQuery<T>/ISender, or MediatR's), Result type, how handlers reach data (repository + unit of work, or the module's DbContext for reads), how endpoints map a Result to HTTP.

Test first: the handler test (success and each Error) and, for a new endpoint, an HTTP test through WebApplicationFactory.

Layout

src/Modules/<Module>/<Module>.Application/<Entity>/<Verb>/
  <Verb><Entity>Command.cs           or  <Verb><Entity>Query.cs
  <Verb><Entity>CommandHandler.cs        <Verb><Entity>QueryHandler.cs
  <Verb><Entity>CommandValidator.cs      <Entity>Response.cs (sealed record), if not shared

Command

namespace <Module>.Application.<Entity>.Create;

public sealed record Create<Entity>Command(string Name) : ICommand<Guid>;   // ICommand (no value) for Update/Delete

internal sealed class Create<Entity>CommandHandler(
    I<Entity>Repository repository,
    IUnitOfWork unitOfWork) : ICommandHandler<Create<Entity>Command, Guid>
{
    public async Task<Result<Guid>> Handle(Create<Entity>Command command, CancellationToken cancellationToken)
    {
        bool exists = await repository.ExistsByNameAsync(command.Name, cancellationToken);
        if (exists)
        {
            return Result.Failure<Guid>(<Entity>Errors.DuplicateName(command.Name));
        }

        Result<<Entity>> created = <Entity>.Create(command.Name);
        if (created.IsFailure)
        {
            return Result.Failure<Guid>(created.Error);
        }

        repository.Add(created.Value);
        await unitOfWork.SaveChangesAsync(cancellationToken);
        return created.Value.Id;   // the repo's public id if it has one
    }
}

internal sealed class Create<Entity>CommandValidator : AbstractValidator<Create<Entity>Command>
{
    public Create<Entity>CommandValidator()
    {
        RuleFor(c => c.Name).NotEmpty().MaximumLength(<Entity>.MaxNameLength);
    }
}

Update: load by id (tenant-filtered), NotFound error when missing, call entity methods, save. Delete or archive: the same, with the entity's method. Validators check shape; the domain checks business rules.

Query

namespace <Module>.Application.<Entity>.List;

public sealed record List<Entity>Query(int Page = 1, int PageSize = 20) : IQuery<PagedResult<<Entity>Response>>;

internal sealed class List<Entity>QueryHandler(<Module>DbContext db)
    : IQueryHandler<List<Entity>Query, PagedResult<<Entity>Response>>
{
    public async Task<Result<PagedResult<<Entity>Response>>> Handle(List<Entity>Query query, CancellationToken cancellationToken)
    {
        IQueryable<<Entity>> source = db.<Entity>s.AsNoTracking();   // tenant filter is global
        int total = await source.CountAsync(cancellationToken);
        List<<Entity>Response> items = await source
            .OrderBy(e => e.Name)
            .Skip((query.Page - 1) * query.PageSize)
            .Take(query.PageSize)
            .Select(e => new <Entity>Response(e.Id, e.Name, e.Status.ToString()))
            .ToListAsync(cancellationToken);
        return new PagedResult<<Entity>Response>(items, total, query.Page, query.PageSize);
    }
}

public sealed record <Entity>Response(Guid Id, string Name, string Status);

Queries: AsNoTracking, projection, pagination, sort fields from an allow-list, NotFound for a missing single item. A query never changes state.

Endpoint

[HttpPost]
public async Task<IActionResult> Create(Create<Entity>Command command, CancellationToken cancellationToken)
{
    Result<Guid> result = await sender.Send(command, cancellationToken);
    return result.Match(Ok, CustomResults.Problem);   // the repo's Result-to-HTTP mapping
}

Inject ISender. Prefer the repo's existing Match/Problem mapping; avoid CreatedAtAction link generation if the repo's versioned routes break it (return Ok with the id instead).

Checklist

  • Names <Verb><Entity>Command|Query and ...Handler/...Validator
  • Returns Result/Result<T>; failures are typed Errors
  • Handler uses only its own module's data access
  • Tests first; gate passes

Download now (ZIP)

Does 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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.