Ejemplo n.º 1
0
        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"));
        }
Ejemplo n.º 2
0
        public void OnEntry(MethodExecutionArgs eventArgs)
        {
            if (IsNoopConversationalMarkerActive)
            {
                return;
            }

            var           convId = GetConversationIdMethod.Invoke();
            IConversation c      = ConversationsContainerAccessor.Container.Get(convId);

            if (c == null)
            {
                IConversationFactory cf = ConversationFactory;
                if (cf == null)
                {
                    return;
                }
                c = cf.CreateConversation(convId);
                // we are using the event because a custom eventHandler can prevent the rethrow
                // but we must Unbind the conversation from the container
                // and we must dispose the conversation itself (high probability UoW inconsistence).
                c.OnException += ((conversation, args) => ConversationsContainerAccessor.Container.Unbind(c.Id).Dispose());
                ConfigureConversation(c, eventArgs.Instance);
                ConversationsContainerAccessor.Container.SetAsCurrent(c);
                c.Start();
                ConversationPausedWatcher.Watch(c);
            }
            else
            {
                ConversationsContainerAccessor.Container.SetAsCurrent(c);
                if (ConversationPausedWatcher.IsPaused(c))
                {
                    c.Resume();
                }
                else
                {
                    eventArgs.MethodExecutionTag = NestedMethodMarker;
                }
            }
        }