I have a class with many methods with different inserts and updates to a database that uses Dapper for its operations.
I call some of them in one method and want to have one transaction for all of them.
Something like this :
public bool OrderItemChange(int OrderItem)
{
using (var con = await Connection.GetOpenConnection())
{
using (var trans = con.BeginTransaction())
{
try
{
// It's better if I don't send 'con' and 'trans' to all these methods
Find(OrderItem);
Remove(OrderItem);
Save(OrderItem);
trans.commit();
}
catch(exception ex)
{
trans.Rollback();
}
}
}
}
I've seen some answers like this one.
but I want to know if there is a better and simpler way than passing SqlConnection
and SqlTransaction
arguments to each method.