I'm creating ASP.NET Core application using Dapper as ORM.
What is the proper flow to saves objects into multiple tables?
In my app architecture I got standard web api controllers that invoke command/query handlers that calculate/invoke other services/repositories etc.
My entities/db tables are User, Order, Product. One of my CommandHandler create user, then calculate products, orders etc. I just wonder how to save these objects into database. I see 2 solutions:
1.) I create some kind of DTO for all the calculated stuff during command handler:
public class TestDto
{
public User User;
public IList<Orders> Orders;
}
calculate all the stuff, fill the DTO one by one, and then at the end of command handler invoke all repositories:
...using (var ts = new Transaction)
{
_userRepository.Save(dto.User);
_ordersRepository.Save(dto.Orders);
ts.Complete
}
etc..
2.) Create transaction per whole command handler, and save user immediately after calculating him in memory, then calculate orders and save them immediately as well, and the same with orders.