Mediatr: IRequestPostProcessor - using with IRequestHandler with no return type

Created on 5 Feb 2018  路  2Comments  路  Source: jbogard/MediatR

Hi Jimmy,

Making use of the RequestPostProcessor in a project for the first time and have a question regarding usage.

IRequestHandler

I have a request handler with no return type as with many of my commands, how do I setup a RequestPostProcessor to run afterward?

c# public interface IRequestPostProcessor<in TRequest, in TResponse> { Task Process(TRequest request, TResponse response); }

IRequestPostProcessor appears to only have an interface supporting those handlers with a return type.

Kind regards and thanks for a fantastic free tool.

Steve

Most helpful comment

You can substitute "Unit" as the return type. C# doesn't allow void to be a return type so I had to borrow the void equivalent from the functional world.

All 2 comments

You can substitute "Unit" as the return type. C# doesn't allow void to be a return type so I had to borrow the void equivalent from the functional world.

Thank's Jimmy. Works perfectly.

As a note to others having the same question 'Unit' is a MediatR type and I've implemented it with the following code.

    public class SectionValidatorRequestPostProcessor : IRequestPostProcessor<AnswersCommand, Unit>,
        IRequireDapperConnection
    {
        private readonly IMediator mediator;
        public IDapperConnection DapperConnection { get; set; }

        public SectionValidatorRequestPostProcessor(IMediator mediator)
        {
            this.mediator = mediator;
        }

        public async Task Process(AnswersCommand command, Unit response)
        {
            var evidence = await mediator.Send(new GetEvidenceForSectionQuery(command.SectionId));

            var isValid = await mediator.Send(new GetSectionValidityQuery(command.SectionId, evidence.Items));

            if (isValid)
            {
                isValid = await mediator.Send(new GetAnswersValidityQuery(command.Answers));
            }

            await mediator.Send(new UpdateSectionStateCommand(command.SectionId, isValid));
        }
    }
```

public class CreateUpdateAnswerCommandHandler : IRequestHandler, IRequireDapperConnection
{
public IDapperConnection DapperConnection { get; set; }

    public async Task Handle(AnswersCommand command, CancellationToken cancellationToken)
    {
        foreach (var answer in command.Answers)
        {
            await DapperConnection.ExecuteAsync("usp_CreateUpdateAnswer", new { answer.QuestionId, command.SectionId, answer.Value, command.AccountId }, CommandType.StoredProcedure);
        }
    }
}

```

Was this page helpful?
0 / 5 - 0 ratings