0

I use Service - Repository pattern.

I keep all basic CRUD in all repositories and won't touch/change. Service is empty and it's only for functions that are customized. So when I want to execute few actions, I create and customize like this in service class.

HomeService.cs

public ReceiptModel Save(UserModel user, InvoiceModel invoice)
{
   BeginTransaction();
   User.Save(user);
   var invoiceId = Invoice.Save(invoice);
   Receipt.Save(...);
   Commit();
   var result = Receipt.Get(x => x.InvoiceId == invoiceId);
   return result;
}

Now in CQRS + MediatR,

  1. do I need to call _mediator.Send() 4 times?

HomeController.cs

[HttpPost]
public async Task<IResult> InsertInvoice([FromBody]SaveInvoiceCommand invoice)
{
   await _mediator.Send(new SaveUserCommand(user));
   var invoiceId = await _mediator.Send(invoice);
   await _mediator.Send(new SaveReceiptCommand(invoiceId));
   var result = _mediator.Send(new GetReceiptQuery(invoiceId));
   return Results.Ok(result);
}
  1. or create new handler to combine these 4 actions?

If no.2, doesn't it bloat abit more when I have alot of customized functions comparing with service repository pattern?

Asherguru
  • 1,687
  • 1
  • 5
  • 10
  • 2 Commands and 1 Query, to use one mediator.Send or 3? If you want it to work like a SQL Transaction then you'd batch them so they can't execute async out of order. If they were all Queries I'd use 3 separate commands so its as fast as the slowest query. A combination of Commands and Queries it depends but you should take into consideration the execution order. – Jeremy Thompson Jan 11 '23 at 01:46

0 Answers0