How Requests and Notifications work in SimpleMediator
mainSimpleMediator uses two primary messaging patterns to decouple components:
Requests (
IRequest<TResponse>): Represents a command or query that expects a single response. You useIMediator.Send()to execute a request, which is handled by exactly oneIRequestHandler<TRequest, TResponse>.Notifications (
INotification): Represents an event broadcast to multiple handlers. You useIMediator.Publish()to broadcast a notification, which can be handled by zero or moreINotificationHandler<TNotification>implementations.
This separation allows you to distinguish between actions that change state and return a result (Commands/Queries) and actions that simply communicate that something has occurred (Events).
// A Request (Command/Query) that returns a string
public class CreateCustomerCommand : IRequest<string>
{
public string Name { get; set; }
}
// A Notification (Event)
public class CustomerCreatedEvent : INotification
{
public Guid CustomerId { get; }
public CustomerCreatedEvent(Guid customerId) => CustomerId = customerId;
}