0

I've seen the combination of MediatR and CQRS is used mostly in Web projects (Especially in MVC projects). We can call the Send method of MediatR inside a controller action without an issue.

var result = await _mediator.Send(command);

Is it suitable to use this combination in a Windows Service application? I couln't find any article that uses MediatR and CQRS in a Windows Service application.

Aki T
  • 612
  • 2
  • 7
  • 17
  • `We can call the Send method of MediatR inside a controller action without an issue` - what's the issue calling it inside a Windows Service? – Alex Jun 05 '18 at 11:59
  • @Alex I thought the Send method is meant to use in a Controller action. – Aki T Jun 05 '18 at 12:20
  • nothing stopping you using it in your windows service. It's not tied to web, or asp.net in any way whatsoever – Alex Jun 05 '18 at 13:43

2 Answers2

0

MediatR is just internal messaging. You can use it in any type of app you like.

Brad Irby
  • 2,397
  • 1
  • 16
  • 26
  • @Alex I'm having an architectural issue. In the Windows Service app, from where (which layer and class) should I call the '_mediator.Send(command)' method? – Aki T Jun 06 '18 at 11:50
  • I'm not sure I follow you. Part of the beauty of the messaging pattern is that components on any level can send messages to other levels without breaking the barriers. MediatR would be setup at system start and injected into lower level classes via Dependency Injection, then any layer can send or receive messages to any other. Does that answer your question? – Brad Irby Jun 06 '18 at 13:33
0

I think I understand where the OP is coming from.

When using MediatR in web apps, it's very easy to see the individual pieces that make up the application. There would be one request / handler / optional for each controller action, each cleanly separated from each other.

In a Windows Service, there's no concept of individual actions. There's only a single entry point, so it can be confusing to know where to use MediatR.

I believe a good way to go about it is to use a single request / handler / response that corresponds to the single entry point. That would mean putting all the windows service logic in the handler.

If that results in a very long handler, then continuously refactor as new abstractions and opportunities for eliminating code duplication are discovered. This would be exactly the same approach as you would do in a MediatR-powered web application.

OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30