mediatr-handlers

Implement MediatR request, notification, and handler patterns for CQRS in .NET applications.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill mediatr-handlers
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mediatr-handlers
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/cqrs/mediatr-handlers
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill mediatr-handlers

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill codifies patterns and best practices for implementing MediatR request handlers, notification handlers, and dispatching so handlers are consistent, testable, and aligned with CQRS principles.

Core Features & Use Cases

  • Handler patterns: guidance for commands, queries, void commands, and notifications with recommended class shapes and naming conventions.
  • Integration practices: how to use repositories, UnitOfWork, DbContext for read/write separation, and how to register handlers with AddMediatR.
  • Use case: implement a CreateOrderCommand that persists an order, returns the new Id, logs activity, and publishes notifications for side effects.

Quick Start

Implement a CreateOrderCommand and its IRequestHandler that uses IOrderRepository and IUnitOfWork to persist a new order and return the created order Id.

Frequently Asked Questions about mediatr-handlers

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

FAQPage Schema
How do I implement MediatR command and query handlers in a .NET CQRS project?

MediatR command and query handlers are implemented as internal sealed classes implementing IRequestHandler, using repositories and UnitOfWork for state changes while returning read-only DTOs for queries.

What is the best way to dispatch MediatR notifications for side effects across services?

MediatR notifications dispatch side-effect handlers across services by publishing events after state changes, allowing multiple independent notification handlers to execute subsequent actions without coupling to the original command.

How do I register MediatR handlers in a .NET application?

Register MediatR handlers in .NET using the AddMediatR method or RegisterServicesFromAssembly to scan assemblies and automatically discover command, query, and notification handlers for dispatch.

Should MediatR handlers return entities or DTOs in a CQRS architecture?

MediatR handlers should prefer returning DTOs over entities to maintain clean separation in CQRS, ensuring queries retrieve read-only data contracts while commands change state and return minimal identifiers.

Do I need to propagate CancellationToken in MediatR request handlers?

Propagating CancellationToken in MediatR request handlers is required to allow cooperative cancellation of database queries and side-effect operations, ensuring long-running command and notification dispatch can be safely aborted.

When should I use MediatR void commands instead of queries in .NET?

Use MediatR void commands when an action changes state without needing to return data, such as logging activity or persisting records, whereas queries are reserved for retrieving read-only DTOs.