Example #1
0
 private void CreateNewStackIfNoneExist()
 {
     if (ScopeStack == null)
     {
         ScopedUnitOfWorkConfiguration.LoggingAction("[ScopeManager] creating new stack since none exists");
         ScopeStack = new UoWScopeStack <TContext>(ServiceLocator.GetInstance <TContext>());
     }
 }
Example #2
0
        public void Remove(IUnitOfWork unitOfWork)
        {
            // simply ignore if not in the stack at all
            if (_scopeStack == null || !_scopeStack.Stack.Contains(unitOfWork))
            {
                return;
            }

            ScopedUnitOfWorkConfiguration.LoggingAction("[ScopeManager] removing " + unitOfWork);

            ThrowIfNotLastScoped(unitOfWork);

            _scopeStack.Stack.Pop();

            if (unitOfWork.ScopeType == ScopeType.Transactional)
            {
                // we check if not already commited / rolled-back
                if (!unitOfWork.IsFinished && !_scopeStack.IsRolledBack())
                {
                    ScopedUnitOfWorkConfiguration.LoggingAction("[ScopeManager] rolling back transaction for " + unitOfWork);
                    _scopeStack.RollBack();
                }

                // if very last transaction, we can remove the transactional portion of the stack
                if (!_scopeStack.AnyTransactionalUnitsOfWork())
                {
                    ScopedUnitOfWorkConfiguration.LoggingAction("[ScopeManager] removing transaction since it is the last one");
                    _scopeStack.CleanTransaction();
                }
            }

            // if last, remove the stack / context
            if (!_scopeStack.Stack.Any())
            {
                ScopedUnitOfWorkConfiguration.LoggingAction("[ScopeManager] disposing the scope stack after last unit of work removed");
                _scopeStack.Dispose();
                _scopeStack = null;
            }
        }