Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use IPipelineBehavior over IRequestPreProcessor?

Tags:

.net

mediatr

I have a bunch of custom IPipelineBehavior's implemented, such as AuthenticationBehavior, AuthorizationBehavior, ValidationBehavior, MemoryCachedBehavior and a few more. They all get executed before the handler which is what I want them to do anyway.

So my question is when would I want to use IRequestPreProcessor instead? For example if I changed my AuthenticationBehavior from this:

public class AuthenticationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IAuthenticatedRequest

to this:

public class AuthenticationPreProcessor<TRequest> : IRequestPreProcessor<TRequest> where TRequest : IAuthenticatedRequest

what would be the practical difference?

I do see that the preprocessor doesn't return a response, but besides that, is there anything else that would be significantly different? Are there any benefits which I'm not seeing other than just cognitively knowing that this is supposed to process the request only and that it can't short circuit and return before even entering a handler? When should one approach be favored over the other?

Thanks.

like image 537
tkit Avatar asked Mar 13 '26 02:03

tkit


1 Answers

I got an answer by Jimmy Bogard over on MediatR GitHub page.

It's not significantly different - it's just to be explicit that "this runs before the handler" and "this runs after the handler", so you don't have to worry about the delegate to execute the continuation. Sometimes that delegate confuses people.

like image 131
tkit Avatar answered Mar 15 '26 00:03

tkit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!