Created
February 25, 2020 12:36
-
-
Save mesuttalebi/75dce878e0871ced0803e385b6eb8d93 to your computer and use it in GitHub Desktop.
Transaction Business service able to handle nested transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TransactionService : ITransactionService | |
{ | |
private IDbContextTransaction _transaction; | |
private readonly IUnitOfWork _unitOfWork; | |
private Queue _transactions; | |
public TransactionService(IUnitOfWork unitOfWork) | |
{ | |
_unitOfWork = unitOfWork; | |
_transactions = new Queue(); | |
} | |
public void BeginTransaction() | |
{ | |
_transactions.Enqueue(1); | |
if(_unitOfWork.Context.Database.CurrentTransaction == null) | |
_transaction = _unitOfWork.Context.Database.BeginTransaction(); | |
} | |
public void Commit() | |
{ | |
if(_transactions.Count > 0) | |
_transactions.Dequeue(); | |
if(_transactions.Count == 0) | |
_transaction.Commit(); | |
} | |
public void Rollback() | |
{ | |
_transaction.Rollback(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment