我什么时候应该使用 IPipelineBehavior 而不是 IRequestPreProcessor?

When should I use IPipelineBehavior over IRequestPreProcessor?

我实现了一堆自定义 IPipelineBehavior,例如 AuthenticationBehaviorAuthorizationBehaviorValidationBehaviorMemoryCachedBehavior 等等。它们都在处理程序之前执行,这正是我希望它们执行的操作。

所以我的问题是我什么时候想改用 IRequestPreProcessor?例如,如果我将我的 AuthenticationBehavior 更改为:

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

对此:

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

实际区别是什么?

我确实看到预处理器没有 return 响应,但除此之外,还有其他明显不同的地方吗?除了认知上知道这应该只处理请求并且在进入处理程序之前它不能短路和 return 之外,还有什么我没有看到的好处吗?什么时候应该采用一种方法而不是另一种方法?

谢谢。

我在 MediatR GitHub 页面上得到了 Jimmy Bogard 的回答。

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.