Skip to main content

IRequest

Namespace: RA.Utilities.Feature.Abstractions

IRequest and its generic counterpart IRequest<TResponse> are marker interfaces used to define the "messages" that your application sends. These messages can be either Commands (requests to change state) or Queries (requests to read data).

The purpose of inheriting from IRequest is to "mark" a class as a message that can be dispatched through the custom mediator pipeline to a corresponding handler.

There are two primary versions:

  1. IRequest: A marker interface for a feature (command or query). This is used for features that do not return a value. It's used for "fire-and-forget" operations.
  2. IRequest<TResponse>: This is the most common one. It marks a class as a message that, when processed by its handler, will return a value of type TResponse.

🛠️ How It's Used​

// The command containing the data for the new product
public record CreateProductCommand(string Name, decimal Price) : IRequest<int>;

Let's break this down:

  • CreateProductCommand: This is a C# record that holds the data needed to create a product.
  • : IRequest<int>: By inheriting from IRequest<int>, this record is marked as a mediator message. This declaration tells the system two things:
    1. This is a request that can be sent via the custom mediator (IMediator.Send).
    2. When it is successfully handled, the expected response will be an int (for example, the new product's ID).

🧠 Summary

In summary, IRequest is the foundational interface that enables the CQRS pattern within your architecture. It defines the messages that trigger your business logic, allowing you to create clean, decoupled, and highly maintainable vertical slices for each feature.