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,
- 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);
}
- 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?