Example #1
0
        public void Insert()
        {
            using (IUnitOfWork unitOfWork = new NHibernateUnitOfWork())
            {
                var role = new Role {
                    Name = "Admin"
                };
                unitOfWork.RoleRepository.Add(role);
                //unitOfWork.Commit();

                var user = new User
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "ayman1",
                    LastName  = "mohammed1",
                    Roles     = new List <Role> {
                        role
                    }
                };

                unitOfWork.UserRepository.Add(user);
                unitOfWork.Commit();
                Assert.AreNotEqual(user.Id, Guid.Empty);
            }
        }
Example #2
0
 public void Commit_Transaction_When_Called()
 {
     using (var uow = new NHibernateUnitOfWork(_mockSession.Object, IsolationLevel.ReadUncommitted))
     {
         uow.Commit();
         _mockTransaction.Verify(mt => mt.Commit());
     }
 }
Example #3
0
 public void Select()
 {
     using (IUnitOfWork unitOfWork = new NHibernateUnitOfWork())
     {
         var users = unitOfWork.UserRepository.Get();
         unitOfWork.Commit();
     }
 }
Example #4
0
 public void AddPerson(Person person)
 {
     using (IUnitOfWork uow = new NHibernateUnitOfWork())
     {
         try
         {
             _repository.Add(person);
             uow.Commit();
         }
         catch (Exception ex)
         {
             uow.RollBack();
         }
     }
 }
Example #5
0
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateMock <ITransaction>();

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

            var unitOfWork = new NHibernateUnitOfWork(mockSession);

            unitOfWork.BeginTransaction();
            unitOfWork.Commit();

            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #6
0
        public void TransactionalFlush_Starts_A_Transaction_With_Specified_IsolationLevel_And_Commits_When_Flush_Succeeds()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateMock <ITransaction>();

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

            mockTransaction.Expect(x => x.Commit());

            var unitOfWork = new NHibernateUnitOfWork(mockSession);

            unitOfWork.Commit(IsolationLevel.ReadUncommitted);

            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #7
0
        public void TransactionalFlush_Rollsback_Transaction_When_Flush_Throws_Exception()
        {
            var mockSession     = MockRepository.GenerateMock <ISession>();
            var mockTransaction = MockRepository.GenerateMock <ITransaction>();


            mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
            .Return(mockTransaction);
            mockSession.Expect(x => x.Flush()).Throw(new Exception());

            mockTransaction.Expect(x => x.Rollback());

            var unitOfWork = new NHibernateUnitOfWork(mockSession);

            Assert.Throws <Exception>(() => { unitOfWork.Commit(); });

            mockSession.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #8
0
 public void Commit_should_commit_the_transaction()
 {
     _uow.Commit();
     _transaction.AssertWasCalled(t => t.Commit());
 }
		public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
		{
			var mockSession = MockRepository.GenerateMock<ISession>();
			var mockTransaction = MockRepository.GenerateMock<ITransaction>();

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

			var unitOfWork = new NHibernateUnitOfWork(mockSession);
			unitOfWork.BeginTransaction();
			unitOfWork.Commit();

			mockSession.VerifyAllExpectations();
			mockTransaction.VerifyAllExpectations();
		}
		public void TransactionalFlush_Starts_A_Transaction_With_Specified_IsolationLevel_And_Commits_When_Flush_Succeeds()
		{
			var mockSession = MockRepository.GenerateMock<ISession>();
			var mockTransaction = MockRepository.GenerateMock<ITransaction>();

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

			mockTransaction.Expect(x => x.Commit());

			var unitOfWork = new NHibernateUnitOfWork(mockSession);
			unitOfWork.Commit(IsolationLevel.ReadUncommitted);

			mockSession.VerifyAllExpectations();
			mockTransaction.VerifyAllExpectations();
		}
		public void TransactionalFlush_Rollsback_Transaction_When_Flush_Throws_Exception()
		{
			var mockSession = MockRepository.GenerateMock<ISession>();
			var mockTransaction = MockRepository.GenerateMock<ITransaction>();


			mockSession.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted))
				.Return(mockTransaction);
			mockSession.Expect(x => x.Flush()).Throw(new Exception());

			mockTransaction.Expect(x => x.Rollback());

			var unitOfWork = new NHibernateUnitOfWork(mockSession);
			Assert.Throws<Exception>(() => { unitOfWork.Commit(); });

			mockSession.VerifyAllExpectations();
			mockTransaction.VerifyAllExpectations();
		}