In the separation of concerns, I have a Controller, Handler, and Repository. If I want the database IO to be done asynchronously, where should I put the async method?
For instance, I currently have the following:
In the Controller
public async Task<ActionResult> CreateOrder(OrderCreateModel ocm)
{
var order = await Using<CreateOffer>().Exeucte(ocm, userID);
...
}
In the Handler
public async Task<Order> Execute(OrderCreateModel ocm, Guid userID)
{
...
return await _ordersRepository.CreateOrderAsync(new Order(ConvertToDataObject(ocm, userID));
}
And finally, in the Repository
public async Task<Order> CreateOrderAsync(Order newOrder)
{
context.orders.Add(newOrder);
await context.SaveChangesAsync();
return newOrder;
}
It seems like this is too many Async methods. Would I get the same results if I:
- just make the repository method async (and all the above methods synchronous)?
- Or if I just make the Controller method Async (and make all the underlying calls synchronous)?
What would be the best practices traversing the different layers to make the data operation asynchronous?