Ejemplo n.º 1
0
        public void BeginTransaction_Throws_InvalidOperationException_When_Transaction_Already_Running()
        {
            var mockSession = MockRepository.GenerateStub<ISession>();
            mockSession.Stub(x => x.BeginTransaction(IsolationLevel.Unspecified))
                .IgnoreArguments().Return(MockRepository.GenerateStub<global::NHibernate.ITransaction>());

            var unitOfWork = new NHUnitOfWork(mockSession);
            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            Assert.Throws<InvalidOperationException>(() => unitOfWork.BeginTransaction());
        }
Ejemplo n.º 2
0
        public void BeginTransaction_Throws_InvalidOperationException_When_Transaction_Already_Running()
        {
            var mockSession = MockRepository.GenerateStub <ISession>();

            mockSession.Stub(x => x.BeginTransaction(IsolationLevel.Unspecified))
            .IgnoreArguments().Return(MockRepository.GenerateStub <global::NHibernate.ITransaction>());

            var unitOfWork = new NHUnitOfWork(mockSession);

            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            Assert.Throws <InvalidOperationException>(() => unitOfWork.BeginTransaction());
        }
Ejemplo n.º 3
0
        private static void CreateAccount(int number, Instrument instrument)
        {
            using var uow = new NHUnitOfWork();
            uow.OpenSession(_currentReadTenantIds);
            uow.BeginTransaction();

            Console.WriteLine(
                $"Creating Account#: {number} under Instrument#: {instrument.Number} for Tenant ID: {_currentWriteTenantId}");

            var account = new Account
            {
                AccountNumber = number,
                Instrument    = instrument
            };

            try
            {
                instrument.AddAccount(account);
                uow.Session.SaveOrUpdate(account);

                uow.Commit();
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine($"Unable to create Account: {ex.Message}");
                uow.Rollback();
            }
        }
Ejemplo n.º 4
0
        public void Begin_Transaction_Should_Start_A_New_Transaction_With_Default_IsolationLevel()
        {
            var mockSession = MockRepository.GenerateMock<ISession>();
            var mockTransaction = MockRepository.GenerateStub<global::NHibernate.ITransaction>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
                        .Return(mockTransaction);

            var unitOfWork = new NHUnitOfWork(mockSession);
            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            mockSession.VerifyAllExpectations();
        }
Ejemplo n.º 5
0
        public void Rollback_Transaction_Releases_Transaction_From_UnitOfWork()
        {
            var mockSession = MockRepository.GenerateMock <ISession>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
            .Return(MockRepository.GenerateStub <global::NHibernate.ITransaction>());

            var unitOfWork  = new NHUnitOfWork(mockSession);
            var transaction = unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            transaction.Rollback();
            Assert.That(!unitOfWork.IsInTransaction);
            mockSession.VerifyAllExpectations();
        }
Ejemplo n.º 6
0
        private static void CreateReferenceDataItem(string value)
        {
            using var uow = new NHUnitOfWork();
            uow.OpenSession(_currentReadTenantIds);
            uow.BeginTransaction();

            Console.WriteLine($"Creating ReferenceDataItem with Value: {value} using Tenant ID: {_currentWriteTenantId}");

            var referenceDataItem = new ReferenceDataItem
            {
                Value = value
            };

            uow.Session.SaveOrUpdate(referenceDataItem);
            uow.Commit();
        }
Ejemplo n.º 7
0
        public void BeginTransaction_Should_Start_A_New_Transaction_With_Specified_IsolatinLevel()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateStub <global::NHibernate.ITransaction>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.Snapshot))
            .Return(mockTransaction);

            var unitOfWork = new NHUnitOfWork(mockSession);

            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction(IsolationLevel.Snapshot);

            Assert.That(unitOfWork.IsInTransaction);
            mockSession.VerifyAllExpectations();
        }
Ejemplo n.º 8
0
        public void Dispose_UnitOfWork_Disposed_Underlying_Transaction_And_Session()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateMock <global::NHibernate.ITransaction>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
            .Return(mockTransaction);
            mockTransaction.Expect(x => x.Dispose());
            mockSession.Expect(x => x.Dispose());

            using (var unitOfWork = new NHUnitOfWork(mockSession))
            {
                unitOfWork.BeginTransaction();
            }
            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Ejemplo n.º 9
0
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateMock <global::NHibernate.ITransaction>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
            .Return(mockTransaction)
            .Repeat.Once();            //Expect BeginTransaction to be called only once.

            var unitOfWork = new NHUnitOfWork(mockSession);

            unitOfWork.BeginTransaction();
            unitOfWork.TransactionalFlush();

            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Ejemplo n.º 10
0
        private static Instrument CreateInstrument(int number)
        {
            using var uow = new NHUnitOfWork();
            uow.OpenSession(_currentReadTenantIds);
            uow.BeginTransaction();

            Console.WriteLine($"Creating Instrument#: {number} for Tenant ID: {_currentWriteTenantId}");

            var instrument = new Instrument
            {
                Number = number
            };

            uow.Session.SaveOrUpdate(instrument);
            uow.Commit();

            return(instrument);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine($"Tenant One ID: {TenantOne}");
            Console.WriteLine($"Tenant Two ID: {TenantTwo}");

            Console.WriteLine(Environment.NewLine);

            _currentReadTenantIds = new Guid[] { TenantOne, TenantTwo };

            CreateTestData();

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(Environment.NewLine);

            _currentReadTenantIds = new Guid[] { TenantOne, TenantTwo };
            using (var uow = new NHUnitOfWork())
            {
                uow.OpenSession(_currentReadTenantIds);
                uow.BeginTransaction();

                var allInstruments        = GetInstruments(uow);
                var allAccounts           = GetAccounts(uow);
                var allReferenceDataItems = GetReferenceDataItems(uow);

                Console.WriteLine("All Instruments:");
                foreach (var instrument in allInstruments)
                {
                    Console.WriteLine($"{instrument.TenantId} - Instrument#: {instrument.Number}");
                }

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("All Accounts:");
                foreach (var account in allAccounts)
                {
                    Console.WriteLine(
                        $"{account.TenantId} - Instrument#: {account.Instrument.Number} Account#: {account.AccountNumber}");
                }

                Console.WriteLine(Environment.NewLine);


                Console.WriteLine("All Reference Data Items:");
                foreach (var refDataItem in allReferenceDataItems)
                {
                    Console.WriteLine($"{refDataItem.Value}");
                }

                Console.WriteLine(Environment.NewLine);
            }


            _currentReadTenantIds = new Guid[] { TenantOne };
            using (var uow = new NHUnitOfWork())
            {
                uow.OpenSession(_currentReadTenantIds);
                uow.BeginTransaction();

                var tenantOneInstruments             = GetInstruments(uow);
                var tenantOneAccountsFromInstruments = tenantOneInstruments.SelectMany(i => i.Accounts);
                var tenantOneAccounts = GetAccounts(uow);

                Console.WriteLine("Tenant One Instruments:");
                foreach (var instrument in tenantOneInstruments)
                {
                    Console.WriteLine($"{instrument.TenantId} - Instrument#: {instrument.Number}");
                }

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Tenant One Accounts:");
                foreach (var account in tenantOneAccounts)
                {
                    Console.WriteLine(
                        $"{account.TenantId} - Instrument#: {account.Instrument.Number} Account#: {account.AccountNumber}");
                }

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Tenant One Accounts From Instruments:");
                foreach (var account in tenantOneAccountsFromInstruments)
                {
                    Console.WriteLine(
                        $"{account.TenantId} - Instrument#: {account.Instrument.Number} Account#: {account.AccountNumber}");
                }

                Console.WriteLine(Environment.NewLine);
            }


            _currentReadTenantIds = new Guid[] { TenantTwo };
            using (var uow = new NHUnitOfWork())
            {
                uow.OpenSession(_currentReadTenantIds);
                uow.BeginTransaction();

                var tenantTwoInstruments             = GetInstruments(uow);
                var tenantTwoAccountsFromInstruments = tenantTwoInstruments.SelectMany(i => i.Accounts);
                var tenantTwoAccounts = GetAccounts(uow);

                Console.WriteLine("Tenant Two Instruments:");
                foreach (var instrument in tenantTwoInstruments)
                {
                    Console.WriteLine($"{instrument.TenantId} - Instrument#: {instrument.Number}");
                }

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Tenant Two Accounts:");
                foreach (var account in tenantTwoAccounts)
                {
                    Console.WriteLine(
                        $"{account.TenantId} - Instrument#: {account.Instrument.Number} Account#: {account.AccountNumber}");
                }

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Tenant Two Accounts From Instruments:");
                foreach (var account in tenantTwoAccountsFromInstruments)
                {
                    Console.WriteLine(
                        $"{account.TenantId} - Instrument#: {account.Instrument.Number} Account#: {account.AccountNumber}");
                }

                Console.WriteLine(Environment.NewLine);
            }


            _currentReadTenantIds = new Guid[] { TenantOne };
            using (var uow = new NHUnitOfWork())
            {
                uow.OpenSession(_currentReadTenantIds);
                uow.BeginTransaction();

                var tenantOneRefDataItems = GetReferenceDataItems(uow);

                Console.WriteLine("Tenant One Reference Data Items:");
                foreach (var refDataItem in tenantOneRefDataItems)
                {
                    Console.WriteLine($"{refDataItem.Value}");
                }

                Console.WriteLine(Environment.NewLine);
            }


            _currentReadTenantIds = new Guid[] { TenantTwo };
            using (var uow = new NHUnitOfWork())
            {
                uow.OpenSession(_currentReadTenantIds);
                uow.BeginTransaction();

                var tenantTwoRefDataItems = GetReferenceDataItems(uow);

                Console.WriteLine("Tenant Two Reference Data Items:");
                foreach (var refDataItem in tenantTwoRefDataItems)
                {
                    Console.WriteLine($"{refDataItem.Value}");
                }

                Console.WriteLine(Environment.NewLine);
            }
        }
Ejemplo n.º 12
0
        public void Comitting_Transaction_Releases_Transaction_From_UnitOfWork()
        {
            var mockSession = MockRepository.GenerateMock<ISession>();
            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
                       .Return(MockRepository.GenerateStub<global::NHibernate.ITransaction>());

            var unitOfWork = new NHUnitOfWork(mockSession);
            var transaction = unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            transaction.Commit();
            Assert.That(!unitOfWork.IsInTransaction);
            mockSession.VerifyAllExpectations();
        }
Ejemplo n.º 13
0
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockSession = MockRepository.GenerateMock<ISession>();
            var mockTransaction = MockRepository.GenerateMock<global::NHibernate.ITransaction>();

            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
                       .Return(mockTransaction)
                       .Repeat.Once(); //Expect BeginTransaction to be called only once.

            var unitOfWork = new NHUnitOfWork(mockSession);
            unitOfWork.BeginTransaction();
            unitOfWork.TransactionalFlush();

            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Ejemplo n.º 14
0
        public void Dispose_UnitOfWork_Disposed_Underlying_Transaction_And_Session()
        {
            var mockSession = MockRepository.GenerateMock<ISession>();
            var mockTransaction = MockRepository.GenerateMock<global::NHibernate.ITransaction>();
            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
                       .Return(mockTransaction);
            mockTransaction.Expect(x => x.Dispose());
            mockSession.Expect(x => x.Dispose());

            using (var unitOfWork = new NHUnitOfWork(mockSession))
            {
                unitOfWork.BeginTransaction();
            }
            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Ejemplo n.º 15
0
 public ArticlesRepository(NHUnitOfWork unitOfWork)
 {
     _unitOfWork = unitOfWork;
     _unitOfWork.BeginTransaction();
 }