1

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?

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
b.g
  • 31
  • 3

1 Answers1

0

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

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • Thanks I think I got it but to make sure I have another question. I register the handler and it works but when I try `services.AddMediatR(typeof(Program));` it doesn't work. Program isn't in assymbly of Handlers so is it the reason? – b.g Sep 21 '22 at 22:40
  • is Startup in the same assembly with handlers? – Roman Marusyk Sep 21 '22 at 22:44
  • I don't have Startup. Services container is in Program. – b.g Sep 21 '22 at 22:59
  • then you can choose any other class from the assembly with handlers or use smth like `services.AddMediatR(Assembly.GetExecutingAssembly());` - it depends on your project structure – Roman Marusyk Sep 21 '22 at 23:31