Transactions should be kept in the same layer as the database access, i.e. the Data Access layer (DAL)
I use a
TransactionScope
wrapper for all of my transactions:
public delegate void Transaction();
private const int Retries = 3;
public static void TransactionWrapper(Transaction transaction)
{
var options = new TransactionOptions
{
IsolationLevel = IsolationLevel.Snapshot,
Timeout = TimeSpan.FromSeconds(120)
};
for (int x = 0; x < Retries; x++)
{
try
{
using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
{
transaction();
scope.Complete();
}
x = Retries;
}
catch (Exception exception)
{
if (x == Retries - 1)
throw;
}
}
}
Which is used as follows:
object result;
TransactionWrapper(()=>{
result =
});
return result;
My code lends itself to creating all of the query cases on the DAL, including multiple transactions so I can add all of them in the wrapper at the same time.
I hope that helps ^_^
Andy