Commands · Queries · Notifications
Cortex.Mediator separates concerns cleanly: commands change state, queries read data, and notifications broadcast events—each with its own handler and optional pipeline behaviors.
Commands
Mutate system state. Each command implements ICommand<TResult> and is handled by a dedicated ICommandHandler.
Queries
Read data without side-effects. Implement IQuery<TResult> and a matching IQueryHandler.
Notifications
Fan-out events to multiple handlers. Implement INotification and one or more INotificationHandlers.
How Requests Flow Through the Mediator
Every command or query passes through configurable pipeline behaviors before reaching its handler—giving you hooks for validation, logging, caching, and transactions.
Behaviors
Validation
Run FluentValidation rules on any command or query before it reaches the handler. Invalid requests are rejected early.
Logging
Automatic structured logging for every request—duration, success/failure, and exception details without boilerplate code.
Transactions
Wrap command handlers in a transaction scope to guarantee atomicity. Rollback happens automatically on failure.
Caching
Cache query results with a pipeline behavior. Subsequent identical queries return instantly from cache.
Exception Handling
Centralize exception handling across your application. Map exceptions to user-friendly error responses consistently.
Request Processors
Pre- and post-process requests for cross-cutting concerns like auditing, enrichment, or metrics collection.
Up & Running in Minutes
Register the mediator in your DI container, define a command + handler, and send it. That's it—no XML, no configuration files, no ceremony.
-
1
Install —
dotnet add package Cortex.Mediator -
2
Register — Add
services.AddCortexMediator()in Startup. -
3
Define — Create a command implementing
ICommand<T>. -
4
Handle — Write an
ICommandHandler<TCommand, TResult>. -
5
Send — Call
mediator.SendCommandAsync(…).
Build an E-Commerce Backend
See how Cortex.Mediator powers a complete order processing flow—from placing an order, to validation, persistence, and notification broadcasting.
Place Order
Client sends a PlaceOrderCommand via the mediator.
Validate
FluentValidation behavior checks stock, pricing, and customer eligibility.
Persist
Handler saves the order inside a transactional pipeline behavior.
Notify
An OrderPlaced notification fans out to email, analytics, and inventory handlers.
Simplify Your Architecture Today
Stop scattering cross-cutting concerns across controllers and services. Let Cortex.Mediator centralize your command, query, and notification logic.