Home > Mobile >  When should I use IPipelineBehavior over IRequestPreProcessor?
When should I use IPipelineBehavior over IRequestPreProcessor?

Time:03-23

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.

CodePudding user response:

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.

  • Related