简体   繁体   中英

Have a single Request/RequestHandler for all incoming requests

I've been using MediatR with great degree of success in my current project. The idea of using a mediator pipeline to tackle cross-cutting concerns is really good.

One thing that I'm not getting (and maybe the reason is my lack of exposure to c# covariance/contravariance ) is how I can handle all incoming requests in a single handler? I'd like to perform an action before a request is executed regardless of their type:

IRequest

or

IRequest<Foo>

I get what Jimmy says about the PreRequestHandler however I have not been able to implement a single one for all request as they are always typed (I could implement an IPreRequest for every single IRequest I have but that doesn't sound right) ....

Any ideas please?

Here's how I do it with Castle Windsor and Mediatr (let's say we want to log and authorize calls to every handlers). See the gist I made: Decorator unit test

Note that when registering the handlers with the container (Castle Windsor in my case), you need to register them in the right order (in Windsor at least, YMMV).

Also, note that I make exclusions when registering component (for decorators, register all concrete types excluding other decorator handlers.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM