Exemple #1
0
        public void Transaction_Method()
        {
            var entity = new TestEntity();

            using (var repository = new NHibernateRepository <TestEntity>(this.configuration))
            {
                using (repository.Transaction())
                {
                    repository.Persist(new TestEntity());
                }
                Assert.Equal(0, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Persist(new TestEntity());
                    transaction.Rollback();
                }
                Assert.Equal(0, repository.Count());

                try
                {
                    using (repository.Transaction())
                    {
                        repository.Persist(new TestEntity());
                        throw new Exception();
                    }
                }
                catch
                {
                    Assert.True(true);
                }
                Assert.Equal(0, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Persist(entity);
                    transaction.Commit();
                }
                Assert.Equal(1, repository.Count());


                using (repository.Transaction())
                {
                    repository.Delete(entity);
                }
                Assert.Equal(1, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Delete(entity);
                    transaction.Rollback();
                }
                Assert.Equal(1, repository.Count());

                try
                {
                    using (repository.Transaction())
                    {
                        repository.Delete(entity);
                        throw new Exception();
                    }
                }
                catch
                {
                    Assert.True(true);
                }
                Assert.Equal(1, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Delete(entity);
                    transaction.Commit();
                }
                Assert.Equal(0, repository.Count());
            }
        }