Guest User

Untitled

a guest
Jan 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public interface IUnitOfWork : IDisposable
  2. {
  3. void Rollback();
  4. void Commit();
  5. }
  6.  
  7. public class UnitOfWork : IUnitOfWork
  8. {
  9. private readonly ISessionFactory sessionFactory;
  10. private readonly ITransaction transaction;
  11.  
  12. public UnitOfWork(ISessionFactory sessionFactory)
  13. {
  14. this.sessionFactory = sessionFactory;
  15. Session = this.sessionFactory.OpenSession();
  16. transaction = Session.BeginTransaction();
  17. }
  18.  
  19. public ISession Session { get; private set; }
  20.  
  21. public void Dispose()
  22. {
  23. Session.Close();
  24. Session = null;
  25. }
  26.  
  27. public void Rollback()
  28. {
  29. if(transaction.IsActive)
  30. transaction.Rollback();
  31. }
  32.  
  33. public void Commit()
  34. {
  35. if(transaction.IsActive)
  36. transaction.Commit();
  37. }
  38. }
  39.  
  40. [TestFixture]
  41. public class UnitOfWorkTest
  42. {
  43. private ISessionFactory SessionFactory { get; set; }
  44.  
  45. [TestFixtureSetUp]
  46. public void CreateSessionFactory()
  47. {
  48. SessionFactory = Fluently.Configure()
  49. .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
  50. "Server=(local);Database=NhibernateDemo;Trusted_Connection=True;"))
  51. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<StickyNote>())
  52. .BuildSessionFactory();
  53. }
  54.  
  55. [Test]
  56. public void Uow_Commit_ShouldWork()
  57. {
  58. var note = new StickyNote() { Note = "Voo"};
  59. using (var uow = new UnitOfWork(SessionFactory))
  60. {
  61. uow.Session.SaveOrUpdate(note);
  62. uow.Commit();
  63. }
  64.  
  65. using (var uow = new UnitOfWork(SessionFactory))
  66. {
  67. Assert.IsNotNull(uow.Session.Get<StickyNote>(note.Id));
  68. }
  69. }
  70.  
  71. [Test]
  72. public void Uow_Rollback_ShouldWork()
  73. {
  74. var note = new StickyNote {Note = "Woo"};
  75. using (var uow = new UnitOfWork(SessionFactory))
  76. {
  77. uow.Session.SaveOrUpdate(note);
  78. uow.Rollback();
  79. }
  80.  
  81. using (var uow = new UnitOfWork(SessionFactory))
  82. {
  83. Assert.IsTrue(note.Id > 0);
  84. Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
  85. }
  86. }
  87.  
  88. [Test]
  89. public void Uow_RollbackThenCommit_ShouldRollback()
  90. {
  91. var note = new StickyNote { Note = "Xoo" };
  92. using (var uow = new UnitOfWork(SessionFactory))
  93. {
  94. uow.Session.SaveOrUpdate(note);
  95. uow.Rollback();
  96. uow.Commit();
  97. }
  98. using (var uow = new UnitOfWork(SessionFactory))
  99. {
  100. Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
  101. }
  102. }
  103.  
  104. [Test]
  105. public void Uow_CommitThenRollback_ShouldCommit()
  106. {
  107. var note = new StickyNote { Note = "Yoo" };
  108. using (var uow = new UnitOfWork(SessionFactory))
  109. {
  110. uow.Session.SaveOrUpdate(note);
  111. uow.Commit();
  112. uow.Rollback();
  113. }
  114. using (var uow = new UnitOfWork(SessionFactory))
  115. {
  116. Assert.IsNotNull(uow.Session.Get<StickyNote>(note.Id));
  117. }
  118. }
  119.  
  120. [Test]
  121. public void Uow_WithNoCommit_ShouldRollback()
  122. {
  123. var note = new StickyNote { Note = "Zoo" };
  124. using (var uow = new UnitOfWork(SessionFactory))
  125. {
  126. uow.Session.SaveOrUpdate(note);
  127. }
  128. using (var uow = new UnitOfWork(SessionFactory))
  129. {
  130. Assert.IsTrue(note.Id >0);
  131. Assert.IsNull(uow.Session.Get<StickyNote>(note.Id));
  132. }
  133. }
  134. }
Add Comment
Please, Sign In to add comment