Generic MediatR Handlers
Odd as it might be, I couldn’t find much information about it myself, so I thought I’ll share my findings instead. Perhaps it’ll be of help to those seeking answers in the future.
Creating a generic controller? Nothing could be easier, simply create a method with a signature like
private async Task<ActionResult> BlockContent<T>(PostData data) where T : BaseModel, IBlockableContent
and use it like you would any other generic method:
But how about using it with MediatR and the CQRS pattern..?
As it turns out, it’s fairly simple as well:
public sealed record Command<T>(long ObjectId, string Reason) : IRequest<ActionResult> where T : BaseModel, IBlockableContent;
public async Task<ActionResult> Handle(Command<T> request, CancellationToken cancellationToken)
and… that’s it. The IDE will complain about the type parameter in
unused, but such is life, the IDE doesn’t know that we use it down the road, in the
Handle() method. It serves as a bit of a marker, more than a proper type parameter.
And using this generic command and handler? As simple as using the generic action, actually:
Just send the appropriate generic command, and voila!
This post might come off as a bit unnecessary, seeing how short and to the point it is, but I assure you, you will find no better explanation elsewhere. Everything I managed to find, at least, was either waaaay overcomplicated, or boiled down to “no can do”