public void EnlistScope_throws_ArgumentNullException_when_scope_parameter_is_null()
 {
     using (var transaction = new TransactionScope())
     {
         var uowTx = new UnitOfWorkTransaction(MockRepository.GenerateStub<IUnitOfWork>(), transaction);
         Assert.Throws<ArgumentNullException>(() => uowTx.EnlistScope(null));
     }
 }
 public void disposing_transaction_raises_TransactionDisposing_event()
 {
     var disposed = false;
     using (var tx = new UnitOfWorkTransaction(MockRepository.GenerateStub<IUnitOfWork>(),new TransactionScope()))
     {
         tx.TransactionDisposing += (x) => disposed = true;
     }
     Assert.That(disposed);
 }
示例#3
0
        /// <summary>
        /// Handles a Dispose signal from a transaction.
        /// </summary>
        /// <param name="transaction"></param>
        void OnTransactionDisposing(UnitOfWorkTransaction transaction)
        {
            _logger.Info(x => x("UnitOfWorkTransaction {0} signalled a disposed. Unregistering transaction from TransactionManager {1}",
                                transaction.TransactionId, _transactionManagerId));

            transaction.TransactionDisposing -= OnTransactionDisposing;
            var node = _transactions.Find(transaction);

            if (node != null)
            {
                _transactions.Remove(node);
            }
        }
        public void when_scope_is_comitting_unit_of_work_and_transaction_is_comitted()
        {
            using (var tx = new TransactionScope())
            {
                Assert.That(Transaction.Current, Is.Not.Null);
                Assert.That(Transaction.Current.TransactionInformation.Status, Is.EqualTo(TransactionStatus.Active));

                var uow = MockRepository.GenerateMock<IUnitOfWork>();
                var uowScope = MockRepository.GenerateStub<IUnitOfWorkScope>();
                var uowTx = new UnitOfWorkTransaction(uow, tx);
                uowTx.EnlistScope(uowScope);
                uowScope.Raise(x => x.ScopeComitting += null, uowScope);

                uow.AssertWasCalled(x => x.Flush());
                Assert.That(Transaction.Current, Is.Null);
            }
        }
示例#5
0
        /// <summary>
        /// Enlists a <see cref="UnitOfWorkScope"/> instance with the transaction manager,
        /// with the specified transaction mode.
        /// </summary>
        /// <param name="scope">The <see cref="IUnitOfWorkScope"/> to register.</param>
        /// <param name="mode">A <see cref="TransactionMode"/> enum specifying the transaciton
        /// mode of the unit of work.</param>
        public void EnlistScope(IUnitOfWorkScope scope, TransactionMode mode)
        {
            _logger.Info(x => x("Enlisting scope {0} with transaction manager {1} with transaction mode {2}",
                                scope.ScopeId,
                                _transactionManagerId,
                                mode));

            var uowFactory = ServiceLocator.Current.GetInstance <IUnitOfWorkFactory>();

            if (_transactions.Count == 0 ||
                mode == TransactionMode.New ||
                mode == TransactionMode.Supress)
            {
                _logger.Debug(x => x("Enlisting scope {0} with mode {1} requires a new TransactionScope to be created.", scope.ScopeId, mode));
                var txScope     = TransactionScopeHelper.CreateScope(UnitOfWorkSettings.DefaultIsolation, mode);
                var unitOfWork  = uowFactory.Create();
                var transaction = new UnitOfWorkTransaction(unitOfWork, txScope);
                transaction.TransactionDisposing += OnTransactionDisposing;
                transaction.EnlistScope(scope);
                _transactions.AddFirst(transaction);
                return;
            }
            CurrentTransaction.EnlistScope(scope);
        }
        public void when_scope_is_rolledback_unit_of_work_and_transaction_is_disposed_even_when_other_scopes_are_attached()
        {
            using (var tx = new TransactionScope())
            {
                Assert.That(Transaction.Current, Is.Not.Null);
                Assert.That(Transaction.Current.TransactionInformation.Status, Is.EqualTo(TransactionStatus.Active));

                var uow = MockRepository.GenerateMock<IUnitOfWork>();
                var uowScope1 = MockRepository.GenerateStub<IUnitOfWorkScope>();
                var uowScope2 = MockRepository.GenerateStub<IUnitOfWorkScope>();
                var uowTx = new UnitOfWorkTransaction(uow, tx);
                uowTx.EnlistScope(uowScope1);
                uowTx.EnlistScope(uowScope2);
                uowScope1.Raise(x => x.ScopeRollingback += null, uowScope2);

                uow.AssertWasNotCalled(x => x.Flush());
                uow.AssertWasCalled(x => x.Dispose());
                Assert.That(Transaction.Current, Is.Null);
            }
        }
示例#7
0
        /// <summary>
        /// Enlists a <see cref="UnitOfWorkScope"/> instance with the transaction manager,
        /// with the specified transaction mode.
        /// </summary>
        /// <param name="scope">The <see cref="IUnitOfWorkScope"/> to register.</param>
        /// <param name="mode">A <see cref="TransactionMode"/> enum specifying the transaciton
        /// mode of the unit of work.</param>
        public void EnlistScope(IUnitOfWorkScope scope, TransactionMode mode)
        {
            _logger.Info(x => x("Enlisting scope {0} with transaction manager {1} with transaction mode {2}",
                                scope.ScopeId,
                                _transactionManagerId,
                                mode));

            var uowFactory = ServiceLocator.Current.GetInstance<IUnitOfWorkFactory>();
            if (_transactions.Count == 0 ||
                mode == TransactionMode.New ||
                mode == TransactionMode.Supress)
            {
                _logger.Debug(x => x("Enlisting scope {0} with mode {1} requires a new TransactionScope to be created.", scope.ScopeId, mode));
                var txScope = TransactionScopeHelper.CreateScope(UnitOfWorkSettings.DefaultIsolation, mode);
                var unitOfWork = uowFactory.Create();
                var transaction = new UnitOfWorkTransaction(unitOfWork, txScope);
                transaction.TransactionDisposing += OnTransactionDisposing;
                transaction.EnlistScope(scope);
                _transactions.AddFirst(transaction);
                return;
            }
            CurrentTransaction.EnlistScope(scope);
        }
示例#8
0
        /// <summary>
        /// Handles a Dispose signal from a transaction.
        /// </summary>
        /// <param name="transaction"></param>
        void OnTransactionDisposing(UnitOfWorkTransaction transaction)
        {
            _logger.Info(x => x("UnitOfWorkTransaction {0} signalled a disposed. Unregistering transaction from TransactionManager {1}",
                                    transaction.TransactionId, _transactionManagerId));

            transaction.TransactionDisposing -= OnTransactionDisposing;
            var node = _transactions.Find(transaction);
            if (node != null)
                _transactions.Remove(node);
        }