I use CQRS for most of my applications. My request objects contain the data needed by the Handler to perform the Request.
However, I've started to realize that some data points we have are required on every single request and are more contextual to the request. For example, the ID of the specific Division the request is for is added to the header of every http request.
Originally, this contextual data was added to the request before it was sent to the CQRS dispatcher. Something like this (This is in ASP.NET with Mediatr) -
[FromHeader]
public int DivisonId { get; set; }
public async IActionRequest CreateOrder([FromBody] CreateOrderRequest request)
{
request.DivisionId = this.DivisionId;
await _mediator.Send(request);
}
However, I'm considering moving to using a Scoped Service that can provide this contextual data within the Handler itself.
public class CreateOrderHandler
{
private readonly ICurrentDivisionService _currentDivision;
public CreateOrderHandler(ICurrentDivisionService currentDivision)
{...}
public async Task Handle(CreateOrderRequest request)
{
// Code uses _currentDivision.DivisionId instead of request.DivisionId
}
}
My question is this - Should all data be on the request? Or does providing contextual data that is needed for every request through a service make sense?