public void DoWork(ISessionFactory sessions) { var provider = new SessionFactoryProviderStub(sessions); var cf = new DefaultConversationFactory(provider, new FakeSessionWrapper()); var cca = new NhConversationsContainerAccessor(provider); var tc1 = cca.Container; tc1.Bind(cf.CreateConversation("1")); var dao = new SillyDao(sessions); tc1.SetAsCurrent("1"); tc1.CurrentConversation.Start(); var o = new Other3 { Name = "some other silly" }; var e = new Silly3 { Name = "somebody", Other = o }; dao.MakePersistent(e); tc1.CurrentConversation.End(); }
public void PauseShouldNotFlushUnitOfWork() { NhConversation c = NewStartedConversation(); var persistedObj = new Silly3(); c.GetSession(sessions).Save(persistedObj); c.Pause(); AssertDoesNotExistInDb(persistedObj); }
public void EndShouldNotFlushPausedUnitOfWork() { NhConversation c = NewPausedConversation(); ISession s = c.GetSession(sessions); var persistedObj = new Silly3(); s.Save(persistedObj); c.End(); AssertDoesNotExistInDb(persistedObj); }
public void EndShouldFlushStartedUnitOfWork() { NhConversation c = NewStartedConversation(); ISession s = c.GetSession(sessions); var persistedObj = new Silly3(); s.Save(persistedObj); c.End(); AssertExistsInDb(persistedObj); }
public void ConversationUsage() { Assert.Throws <ConversationException>(() => sessions.GetCurrentSession()); var tc1 = cca.Container; tc1.Bind(cf.CreateConversation("1")); tc1.Bind(cf.CreateConversation("2")); var dao = new SillyDao(sessions); tc1.SetAsCurrent("1"); tc1.CurrentConversation.Start(); var o = new Other3 { Name = "some other silly" }; var e = new Silly3 { Name = "somebody", Other = o }; dao.MakePersistent(e); tc1.CurrentConversation.Pause(); tc1.SetAsCurrent("2"); tc1.CurrentConversation.Start(); IList <Silly3> sl = dao.GetAll(); Assert.That(sl.Count, Is.EqualTo(0), "changes in c1 should not have been flushed to db yet"); tc1.CurrentConversation.Pause(); tc1.SetAsCurrent("1"); tc1.CurrentConversation.Resume(); tc1.CurrentConversation.End(); //commit the changes tc1.SetAsCurrent("2"); using (var c2 = tc1.CurrentConversation) { c2.Start(); sl = dao.GetAll(); c2.Pause(); Assert.That(sl.Count, Is.EqualTo(1), "changes in c1 should now be in db"); Assert.That(!NHibernateUtil.IsInitialized(sl[0].Other)); // working with entities, even using lazy loading no cause problems Assert.That("some other silly", Is.EqualTo(sl[0].Other.Name)); Assert.That(NHibernateUtil.IsInitialized(sl[0].Other)); sl[0].Other.Name = "nobody"; c2.Resume(); dao.MakePersistent(sl[0]); } Assert.Throws <ConversationException>(() => tc1.SetAsCurrent("1")); Assert.Throws <ConversationException>(() => tc1.SetAsCurrent("2")); }
public void ConversationUsage() { CommitInNewSession(session => { var o = new Other3 { Name = "some other silly" }; var e = new Silly3 { Name = "somebody", Other = o }; session.Save(e); }); using (NhConversation c = NewConversation()) { c.Start(); ISession s = c.GetSession(sessions); IList <Silly3> sl = s.CreateQuery("from Silly3").List <Silly3>(); c.Pause(); Assert.That(sl.Count == 1); Assert.That(!NHibernateUtil.IsInitialized(sl[0].Other)); // working with entities, even using lazy loading Assert.That(!s.Transaction.IsActive); Assert.That("some other silly", Is.EqualTo(sl[0].Other.Name)); Assert.That(NHibernateUtil.IsInitialized(sl[0].Other)); sl[0].Other.Name = "nobody"; c.Resume(); s = c.GetSession(sessions); s.SaveOrUpdate(sl[0]); // the dispose auto-end the conversation } using (NhConversation c = NewConversation()) { c.Start(); ISession s = c.GetSession(sessions); s.Delete("from Silly3"); c.End(); } }
private void AssertDoesNotExistInDb(Silly3 persistedObject) { Assert.That(ExistsInDb <Silly3>(persistedObject.Id), Is.False, "object expected not to be in db"); }
private void AssertExistsInDb(Silly3 persistedObject) { Assert.That(ExistsInDb <Silly3>(persistedObject.Id), Is.True, "object expected to be in db"); }
public void MakeTransient(Silly3 entity) { factory.GetCurrentSession().Delete(entity); }
public Silly3 MakePersistent(Silly3 entity) { factory.GetCurrentSession().SaveOrUpdate(entity); return(entity); }