cqrs-mediatr-guidelines

Enforces CQRS and MediatR conventions for commands, queries, handlers, and validators in Explore.Application.

7|3|Updated Sep 23, 2025
One-click install
npx skills add https://github.com/islamu-ngo/Event --skill cqrs-mediatr-guidelines-islamu-ngo
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: cqrs-mediatr-guidelines
Source: https://github.com/islamu-ngo/Event/tree/main/.agents/skills/cqrs-mediatr-guidelines
Command: npx skills add https://github.com/islamu-ngo/Event --skill cqrs-mediatr-guidelines-islamu-ngo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? It keeps .NET application code consistent when implementing CQRS with MediatR by defining rules for immutable request/result contracts, handler boundaries, validation, caching, and specification-driven reads, preventing drift across features. ## Core Features & Use Cases - Contract Rules: Defines sealed record requests, immutable BaseCommandResponse<TKey> results, named factories, and RFC 7807 failure mapping. - Handler & Validation Patterns: Prescribes repository-only handlers, manually instantiated validators, DTO mapping, cancellation flow, and HybridCache read-through for queries. - Use Case: When adding a new CreateEvent command, follow the workflow to lock request construction with tests, implement the handler and validator per the patterns, and verify with the architecture and unit test suites. ## Quick Start Load the cqrs-mediatr-guidelines skill before writing a new MediatR command or query handler in Explore.Application.

Frequently Asked Questions about cqrs-mediatr-guidelines

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a MediatR command and handler in a CQRS .NET application?▼

Define the command as a sealed record implementing IRequest<BaseCommandResponse<Guid>>, manually instantiate its FluentValidation validator inside the handler, map DTOs to entities, and save through a repository. Handlers never consume DbContext or set HTTP status codes.

How should query handlers use caching with MediatR?▼

Query handlers may use HybridCache read-through via GetOrCreateAsync with a key built from query parameters. Commands invalidate affected cache keys only after the owning write succeeds.

Should validators be injected via dependency injection in MediatR handlers?▼

No. This codebase requires validators to be manually instantiated inside handlers rather than resolved through DI. Validators may still receive repositories through their constructors when needed.

Can handlers use DbContext or IQueryable directly in CQRS?▼

No. Handlers must use repositories that return entities, and handlers perform DTO mapping themselves. Consuming ExploreDbContext or a repository-returned IQueryable violates the pattern.

When should I not apply these CQRS guidelines?▼

The guidelines do not apply to controller-only or repository-only edits that do not touch MediatR requests, handlers, validators, or result contracts in Explore.Application.