I have a problem with Asp.net and MediatR

Viewed 34

I'm learning CQRS so I'm using MediatR I've watched videos for demos but I don't understand this line

  services.AddMediatR(typeof(Startup));

Why did they add to the services container the type of "Startup"? What is exactly the type that should be added? Request Types or RequestHandler types or Startup?

1 Answers

This method registers handlers and mediator types from the assemblies that contain the specified types - Startup in this case.

However, you can use any from the current assembly - services.AddMediatR(typeof(Program)); or even any of your controllers. Common practice is to use Startup because almost every ASP.NET Core application has it.

This will help you avoid registering all the handlers yourself by scanning assemblies and adding handlers, preprocessors, and postprocessors implementations to the container.

See MediatR extensions for Microsoft.Extensions.DependencyInjection

Related