Example #1
0
        public void Commit()
        {
            if (_state == UnitOfWorkState.Committed)
            {
                throw new UnitOfWorkException("Can't commit unit of work that was already committed.");
            }

            if (_unitOfWorkScope.State == UnitOfWorkState.RolledBack)
            {
                throw new UnitOfWorkException("Can't commit unit of work that was already rolled back.");
            }

            foreach (var action in _preCommitActions)
            {
                action.Invoke();
            }

            if (IsScopeWrapper)
            {
                _unitOfWorkScope.Commit();
            }

            _transactionScope.Complete();
            _state = UnitOfWorkState.Committed;
        }
 protected override void Dispose(bool isDisposing)
 {
     if (State == UnitOfWorkState.Active)
         State = UnitOfWorkState.Canceled;
     if (Manager.IsRoot(this))
         Manager.Clear();
     base.Dispose(isDisposing);
 }
Example #3
0
 public void Enter()
 {
     if (State == UnitOfWorkState.Nothing)
     {
         _transaction = _context.Database.BeginTransaction();
         State        = UnitOfWorkState.Entered;
     }
 }
Example #4
0
 public void Rollback()
 {
     if (State == UnitOfWorkState.Entered)
     {
         _transaction?.Rollback();
         State = UnitOfWorkState.Rollback;
     }
 }
Example #5
0
 public void Commit()
 {
     if (State == UnitOfWorkState.Entered)
     {
         _transaction?.Commit();
         State = UnitOfWorkState.Committed;
     }
 }
Example #6
0
 public void Begin(
     IsolationLevel transactionIsolationLevel =
     IsolationLevel.ReadCommitted)
 {
     ThrowIfInvalidState(UnitOfWorkState.Ready);
     Log.Debug(BEGIN_INFO_MESSAGE);
     BeginTransaction(transactionIsolationLevel);
     State = UnitOfWorkState.Begun;
 }
Example #7
0
 private void ThrowIfInvalidState(UnitOfWorkState expectedState)
 {
     if (expectedState != State)
     {
         throw new InvalidOperationException(
                   $"The unit of work is required " +
                   $"to be in {expectedState} state for this action. " +
                   $"Current state is {State}"
                   );
     }
 }
Example #8
0
        internal void Commit()
        {
            if (State == UnitOfWorkState.RolledBack)
            {
                throw new UnitOfWorkException("Can't commit unit of work that was already rolled back.");
            }

            _repository.Transaction.Commit();

            if (Transaction.Current != null && Transaction.Current.TransactionInformation.Status != TransactionStatus.Aborted)
            {
                Transaction.Current.EnlistVolatile(new TransactionCommitHandler(_postCommitActions), EnlistmentOptions.None);
            }

            State = UnitOfWorkState.Committed;
        }
Example #9
0
        public virtual void Rollback()
        {
            ThrowIfInvalidState(UnitOfWorkState.Begun);

            try {
                RollbackTransaction();
                State = UnitOfWorkState.Rollbacked;
            }
            catch (Exception exception) {
                Log.Error(
                    exception: exception,
                    messageTemplate: ROLLBACK_ERROR_MESSAGE
                    );
                throw;
            }
        }
 public void Complete()
 {
     if (!Manager.CanComplete(this))
         throw new UnitOfWorkWasNotCompletedException();
     if (Manager.IsRoot(this) && DataContext.IsDirty)
     {
         try
         {
             DataContext.SaveChanges();
         }
         catch (UserException ex)
         {
             throw new UnitOfWorkWasNotCompletedException(ex.Message, ex);
         }
     }
     State = UnitOfWorkState.Completed;
 }
Example #11
0
        public virtual void Commit()
        {
            ThrowIfInvalidState(UnitOfWorkState.Begun);

            try {
                CommitTransaction();
                State = UnitOfWorkState.Committed;
            }
            catch (Exception exception) {
                Log.Error(
                    exception: exception,
                    messageTemplate: COMMIT_ERROR_MESSAGE
                    );
                RollbackTransaction();
                throw;
            }
        }
Example #12
0
        public void Rollback()
        {
            if (_unitOfWorkScope.State == UnitOfWorkState.RolledBack)
            {
                return;
            }

            if (_unitOfWorkScope.State == UnitOfWorkState.Committed)
            {
                throw new UnitOfWorkException("Can't rollback unit of work that was already committed.");
            }

            _unitOfWorkScope.Rollback();
            _state = UnitOfWorkState.RolledBack;

            if (!IsScopeWrapper)
            {
                throw new UnitOfWorkException("Rolling back inner unit of work will break outer unit of work.");
            }
        }
Example #13
0
        public void Rollback()
        {
            if (State == UnitOfWorkState.RolledBack)
            {
                return;
            }

            if (State == UnitOfWorkState.Committed)
            {
                throw new UnitOfWorkException("Can't rollback unit of work that was already committed.");
            }

            _repository.Transaction.Rollback();

            if (Transaction.Current != null)
            {
                Transaction.Current.EnlistVolatile(new TransactionRollbackHandler(_postRollbackActions), EnlistmentOptions.None);
                Transaction.Current.Rollback();
            }

            State = UnitOfWorkState.RolledBack;
        }
Example #14
0
        public void Commit()
        {
            if (_state == UnitOfWorkState.Committed)
            {
                throw new UnitOfWorkException("Can't commit unit of work that was already committed.");
            }

            if (_unitOfWorkScope.State == UnitOfWorkState.RolledBack)
            {
                throw new UnitOfWorkException("Can't commit unit of work that was already rolled back.");
            }

            foreach (var action in _preCommitActions)
            {
                action.Invoke();
            }

            if (IsScopeWrapper)
            {
                _unitOfWorkScope.Commit();
            }

            _transactionScope.Complete();
            _state = UnitOfWorkState.Committed;
        }
Example #15
0
        public void Rollback()
        {
            if (_unitOfWorkScope.State == UnitOfWorkState.RolledBack) return;

            if (_unitOfWorkScope.State == UnitOfWorkState.Committed)
            {
                throw new UnitOfWorkException("Can't rollback unit of work that was already committed.");
            }

            _unitOfWorkScope.Rollback();
            _state = UnitOfWorkState.RolledBack;

            if (!IsScopeWrapper)
            {
                throw new UnitOfWorkException("Rolling back inner unit of work will break outer unit of work.");
            }
        }
 public InvalidStateForActionException(UnitOfWorkState currentState, string action)
     : base($"The state [{currentState}] of UnitOfWork is not valid for action [{action}]")
 {
 }