I need to use nested transaction scopes to perform some actions:
1)Insert Address
2) After that insert ContactInfo
3) After that insert UserInfo
//one class
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required))
{
user.ContactInfo = BLContactInfo.Add(user.ContactInfo);
BEUser newUser = DLSecurity.CreateUser(user);
transaction.Complete();
//.......
}
//two class
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required))
{
contactInfo.Address = BLAddress.Add(contactInfo.Address);
BEContactInfo newContactInfo = DLContactInfo.Add(contactInfo);
transaction.Complete();
return newContactInfo;
}
I don’t know: Is this way good? Can I avoid using nested transaction?
Ah – nothing says why you need nested transactions. The transaction scope should NOT (!) be within the class. Create ONE transaction OUTSIDE on the top level, then make all calls.