Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IUnitOfWork : IDisposable
- {
- void Rollback();
- void Commit();
- }
- public class UnitOfWork : IUnitOfWork
- {
- private readonly ISessionFactory sessionFactory;
- private readonly ITransaction transaction;
- public UnitOfWork(ISessionFactory sessionFactory)
- {
- this.sessionFactory = sessionFactory;
- Session = this.sessionFactory.OpenSession();
- transaction = Session.BeginTransaction();
- }
- public ISession Session { get; private set; }
- public void Dispose()
- {
- Session.Close();
- Session = null;
- }
- public void Rollback()
- {
- if(transaction.IsActive)
- transaction.Rollback();
- }
- public void Commit()
- {
- if(transaction.IsActive)
- transaction.Commit();
- }
- }
- [TestFixture]
- public class UnitOfWorkTest
- {
- private ISessionFactory SessionFactory { get; set; }
- [TestFixtureSetUp]
- public void CreateSessionFactory()
- {
- SessionFactory = Fluently.Configure()
- .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
- "Server=(local);Database=NhibernateDemo;Trusted_Connection=True;"))
- .Mappings(m => m.FluentMappings.AddFromAssemblyOf<StickyNote>())
- .BuildSessionFactory();
- }
- [Test]
- public void Uow_Commit_ShouldWork()
- {
- var note = new StickyNote() { Note = "Voo"};
- using (var uow = new UnitOfWork(SessionFactory))
- {
- uow.Session.SaveOrUpdate(note);
- uow.Commit();
- }
- using (var uow = new UnitOfWork(SessionFactory))
- {
- Assert.IsNotNull(uow.Session.Get<StickyNote>(note.Id));
- }
- }
- [Test]
- public void Uow_Rollback_ShouldWork()
- {
- var note = new StickyNote {Note = "Woo"};
- using (var uow = new UnitOfWork(SessionFactory))
- {
- uow.Session.SaveOrUpdate(note);
- uow.Rollback();
- }
- using (var uow = new UnitOfWork(SessionFactory))
- {
- Assert.IsTrue(note.Id > 0);
- Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
- }
- }
- [Test]
- public void Uow_RollbackThenCommit_ShouldRollback()
- {
- var note = new StickyNote { Note = "Xoo" };
- using (var uow = new UnitOfWork(SessionFactory))
- {
- uow.Session.SaveOrUpdate(note);
- uow.Rollback();
- uow.Commit();
- }
- using (var uow = new UnitOfWork(SessionFactory))
- {
- Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
- }
- }
- [Test]
- public void Uow_CommitThenRollback_ShouldCommit()
- {
- var note = new StickyNote { Note = "Yoo" };
- using (var uow = new UnitOfWork(SessionFactory))
- {
- uow.Session.SaveOrUpdate(note);
- uow.Commit();
- uow.Rollback();
- }
- using (var uow = new UnitOfWork(SessionFactory))
- {
- Assert.IsNotNull(uow.Session.Get<StickyNote>(note.Id));
- }
- }
- [Test]
- public void Uow_WithNoCommit_ShouldRollback()
- {
- var note = new StickyNote { Note = "Zoo" };
- using (var uow = new UnitOfWork(SessionFactory))
- {
- uow.Session.SaveOrUpdate(note);
- }
- using (var uow = new UnitOfWork(SessionFactory))
- {
- Assert.IsTrue(note.Id >0);
- Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
- }
- }
- }
Add Comment
Please, Sign In to add comment