❔ Using Mediator with seperate API Contracts project

NNathan11/30/2022
Sup guys, I've finally decided to use Mediator, and now I have the following problem:
1. I have a API contracts project, which should not have any Nuget packages installed (it's just the API request / response models)
2. I want to, for example, send the request object via Mediator. But since the API contracts project does not have Mediator installed, I can't have them implement IRequest.
3. So my solution is as follows. Is this correct?
public record CreateAutomationCommand : IRequest<int>
{
    public CreateAutomationRequest Request { get; set; }
}
DDuke11/30/2022
Keep the commands/responses close to the handlers
DDuke11/30/2022
I put all of the stuff for a single request into a single static class
DDuke11/30/2022
which contains the command, response, handler class and validator
DDuke11/30/2022
For example:
public static class CreateRegistrationFlow {
    public record Command : IRequest<Response>
    {
        public string Email { get; init; } = null!;
    }

    public record Response : IRegistrationFlowResponse
    {
        public Guid FlowId { get; set; }
        public string NextStep { get; set; } = null!;
        public Instant ExpiresAt { get; set; }
    }

    public class Handler : IRequestHandler<Command, Response>
    {
        private readonly AppDbContext _db;
        private readonly IClock _clock;
    
        public Handler(AppDbContext db, IClock clock)
        {
            _db = db;
            _clock = clock;
        }
    
        public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
        {
            var flow = new RegistrationFlow()
            {
                Email = request.Email,
                NextStep = RegistrationFlowSteps.SetPersonalInformation,
                ExpiresAt = _clock.GetCurrentInstant() + Duration.FromMinutes(5)
            };
            
            _db.RegistrationFlows.Add(flow);
            await _db.SaveChangesAsync(cancellationToken);
            
            return new Response()
            {
                FlowId = flow.Id,
                NextStep = flow.NextStep,
                ExpiresAt = flow.ExpiresAt
            };
        }
    }
    
    public class CommandValidator : AbstractValidator<Command>
    {
        public CommandValidator()
        {
            RuleFor(x => x.Email).EmailAddress().NotEmpty();
        }
    }
}
NNathan11/30/2022
Yeah that's a preference, but that wasn't my question
DDuke11/30/2022
To answer your question: No that doesn't look correct
DDuke11/30/2022
https://www.nuget.org/packages/Mediator.Abstractions/ maybe this contains the IRequest interface?
AAccord12/1/2022
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.