MediatR

MediatR

🌍 Idioma: English | Español

Mensajería en proceso para Delphi — comandos, consultas y notificaciones despachados a través de una única interfaz IMediator. Inspirado en la biblioteca .NET MediatR.

Delphi 12+ Licencia


Conceptos

Tipo Dirección Devuelve Caso de uso
IRequest 1 → 1 handler Comando (fire-and-forget)
IRequest<TResponse> 1 → 1 handler TResponse Consulta
INotification 1 → N handlers Evento de dominio

Inicio rápido — Comando

// 1. Definir comando
type
  TCreateOrderCommand = class(TRequest)
    OrderId: Integer;
  end;

// 2. Implementar handler
type
  TCreateOrderHandler = class(TRequestHandler<TCreateOrderCommand>)
    procedure Handle(Request: TCreateOrderCommand); override;
  end;

// 3. Registrar handler
Services.AddTransient<IRequestHandler<TCreateOrderCommand>, TCreateOrderHandler>;

// 4. Despachar
Mediator.Send<TCreateOrderCommand>(Cmd);

Inicio rápido — Consulta

type
  TGetOrderQuery    = class(TRequest<TOrderDTO>);
  TGetOrderHandler  = class(TResponseHandler<TOrderDTO, TGetOrderQuery>)
    function Handle(Request: TGetOrderQuery): TOrderDTO; override;
  end;

var DTO := Mediator.Send<TOrderDTO, TGetOrderQuery>(Query);

Inicio rápido — Notificación

type
  TOrderCreatedEvent    = class(TNotification);
  TEmailNotifyHandler   = class(TNotificacionHandler<TOrderCreatedEvent>)
    procedure Handle(Notification: TOrderCreatedEvent); override;
  end;

// Registrar tantos handlers como sea necesario
Services.AddTransient<INotificationHandler<TOrderCreatedEvent>, TEmailNotifyHandler>;
Services.AddTransient<INotificationHandler<TOrderCreatedEvent>, TAuditLogHandler>;

// Publicar — se llama a TODOS los handlers
Mediator.Publish<TOrderCreatedEvent>(Evt);

Registro en DI

uses Daf.MediatR.DependencyInjection;

Services.AddMediatR;   // registra el singleton IMediator + escanea handlers

O manualmente por handler:

Services.AddTransient<IRequestHandler<TMyCommand>, TMyCommandHandler>;

Documentación

  • 📖 Guía de uso — IMediatorHelper, respuestas ARC, pipeline, [MediatorAbstract], escaneo de DI

Abstracciones

MediatR.Abstractions

🌍 Idioma: English | Español

Interfaces y clases base fundamentales para la mensajería en proceso MediatR de DAFce. Referencia estas desde el código de aplicación y las implementaciones de handlers sin depender del mediador concreto.


Interfaces

IRequest

Comando fire-and-forget — despachado a exactamente un handler, sin valor de retorno.

IRequest = interface
end;

IRequest<TResponse>

Consulta — despachada a exactamente un handler, devuelve TResponse.

IRequest<TResponse> = interface(IRequest)
end;

INotification

Evento — publicado a todos los handlers registrados.

INotification = interface
end;

IMediator

IMediator = interface
  procedure Send<TRequest: IRequest>(Request: TRequest); overload;
  function  Send<TResponse; TRequest: IRequest<TResponse>>(
              Request: TRequest): TResponse; overload;
  procedure Publish<TNotification: INotification>(
              Notification: TNotification);
end;

IMediatorHelper

Extiende IMediator con SendARC — devuelve la respuesta envuelta en ARC<TResponse> para una propiedad con conteo de referencias segura.

IMediatorHelper = interface(IMediator)
  function SendARC<TResponse: IInterface; TRequest: IRequest<TResponse>>(
             Request: TRequest): ARC<TResponse>;
end;

IRequestHandler<TRequest>

IRequestHandler<TRequest: IRequest> = interface
  procedure Handle(Request: TRequest);
end;

IResponseHandler<TResponse, TRequest>

IResponseHandler<TResponse; TRequest: IRequest<TResponse>> = interface
  function Handle(Request: TRequest): TResponse;
end;

INotificationHandler<TNotification>

INotificationHandler<TNotification: INotification> = interface
  procedure Handle(Notification: TNotification);
end;

Clases base

Clase Uso
TRequest Hereda para definir un comando
TRequest<TResponse> Hereda para definir una consulta
TNotification Hereda para definir un evento
TRequestHandler<TReq> Hereda para manejar un comando — sobreescribe Handle
TResponseHandler<TRes,TReq> Hereda para manejar una consulta — sobreescribe Handle
TNotificacionHandler<TNot> Hereda para manejar un evento — sobreescribe Handle

Atributos

[MediatorAbstract]

Aplicar a clases base abstractas de handlers para evitar que el mediador las resuelva directamente:

[MediatorAbstract]
TBaseHandler<T: TRequest> = class(TRequestHandler<T>);

Relacionado