0Pricing
C# Academy · Lesson

Commands and Handlers with MediatR

Dispatch requests to dedicated handlers.

Registering MediatR

MediatR scans your assemblies to discover handlers. Register it once at startup, pointing it at an assembly that contains your handlers.

builder.Services.AddMediatR(cfg =>
    cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

IRequest

A message that expects a response implements IRequest<TResponse>. The type parameter is what the handler returns.

public record CreateOrderCommand(int CustomerId, string[] Items)
    : IRequest<int>;   // returns the new order id

All lessons in this course

  1. CQRS Concepts
  2. Commands and Handlers with MediatR
  3. Pipeline Behaviors
  4. Notifications and Events
← Back to C# Academy