public void ShouldUnbindOnEndException()
        {
            IServiceLocator serviceLocator = NewServiceLocator();

            RegisterAsTransient <ISillyCrudModel, SillyCrudModel>(serviceLocator);
            var convFactory = new ConversationFactoryStub(delegate(string id)
            {
                IConversation result = new ExceptionOnFlushConversationStub(id);
                result.OnException  += ((sender, args) => args.ReThrow = false);
                return(result);
            });

            RegisterInstanceForService <IConversationFactory>(serviceLocator, convFactory);

            var scm = serviceLocator.GetInstance <ISillyCrudModel>();

            scm.GetIfAvailable(Guid.NewGuid());
            var conversationContainer =
                (ThreadLocalConversationContainerStub)serviceLocator.GetInstance <IConversationContainer>();

            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Don't start and bind the conversation inmediately");
            scm.AcceptAll();
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(0),
                        "Don't unbind the conversation with exception catch by custom event handler");

            conversationContainer.Reset();
        }
        public void ShouldSupportsDefaultEndMode()
        {
            IServiceLocator serviceLocator = NewServiceLocator();

            RegisterAsTransient <ISillyCrudModel, SillyCrudModelDefaultEnd>(serviceLocator);

            bool endedCalled = false;
            var  convFactory = new ConversationFactoryStub(delegate(string id)
            {
                IConversation result = new NoOpConversationStub(id);
                result.Ended        += ((s, a) => endedCalled = true);
                return(result);
            });

            RegisterInstanceForService <IConversationFactory>(serviceLocator, convFactory);
            var scm = serviceLocator.GetInstance <ISillyCrudModel>();

            scm.GetIfAvailable(Guid.NewGuid());
            Assert.That(endedCalled);
            var conversationContainer =
                (ThreadLocalConversationContainerStub)serviceLocator.GetInstance <IConversationContainer>();

            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(0),
                        "Don't unbind the conversation after end. The Adapter are changing the conversation AutoUnbindAfterEndConversation");

            // cleanup
            conversationContainer.Reset();
        }
        public virtual void ShouldWorkWithServiceCtorInterceptor()
        {
            IServiceLocator serviceLocator = NewServiceLocator();

            RegisterAsTransient <ISillyCrudModel, InheritedSillyCrudModelWithServiceConversationCreationInterceptor>(
                serviceLocator);
            var convFactory = new ConversationFactoryStub(delegate(string id)
            {
                IConversation result = new NoOpConversationStub(id);
                return(result);
            });

            RegisterInstanceForService <IConversationFactory>(serviceLocator, convFactory);

            // Registr the IConversationCreationInterceptor implementation
            RegisterAsTransient <IMyServiceConversationCreationInterceptor, ConversationCreationInterceptor>(serviceLocator);

            var scm = serviceLocator.GetInstance <ISillyCrudModel>();

            Assert.That(
                Spying.Logger <ConversationCreationInterceptor>().Execute(() => scm.GetIfAvailable(Guid.NewGuid())).MessageSequence,
                Is.EqualTo(new[] { ConversationCreationInterceptor.StartingMessage, ConversationCreationInterceptor.StartedMessage }));

            // cleanup
            var conversationContainer =
                (ThreadLocalConversationContainerStub)serviceLocator.GetInstance <IConversationContainer>();

            conversationContainer.Reset();
        }
        public void ShouldSupportsImplicitMethodsInclusion()
        {
            IServiceLocator serviceLocator = NewServiceLocator();

            RegisterAsTransient <ISillyCrudModelExtended, SillyCrudModelWithImplicit>(serviceLocator);

            bool resumedCalled = false;
            bool startedCalled = false;
            var  convFactory   = new ConversationFactoryStub(delegate(string id)
            {
                IConversation result = new NoOpConversationStub(id);
                result.Resumed      += ((s, a) => resumedCalled = true);
                result.Started      += ((s, a) => startedCalled = true);
                return(result);
            });

            RegisterInstanceForService <IConversationFactory>(serviceLocator, convFactory);
            var conversationContainer =
                (ThreadLocalConversationContainerStub)serviceLocator.GetInstance <IConversationContainer>();

            var scm = serviceLocator.GetInstance <ISillyCrudModelExtended>();

            scm.GetEntirelyList();
            Assert.That(startedCalled, "An implicit method inclusion don't start the conversation.");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Should have one active conversation because the default mode is continue.");
            resumedCalled = false;
            startedCalled = false;

            scm.GetIfAvailable(Guid.NewGuid());
            Assert.That(resumedCalled, "An implicit method inclusion don't resume the conversation.");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Should have one active conversation because the default mode is continue.");
            resumedCalled = false;

            scm.DoSomethingNoPersistent();
            Assert.That(!resumedCalled, "An explicit method exclusion resume the conversation; shouldn't");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Should have one active conversation because the default mode is continue.");

            string value = scm.PropertyOutConversation;

            Assert.That(!resumedCalled, "An explicit method exclusion resume the conversation; shouldn't");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Should have one active conversation because the default mode is continue.");

            value = scm.PropertyInConversation;
            Assert.That(resumedCalled, "An implicit method inclusion don't resume the conversation.");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(1),
                        "Should have one active conversation because the default mode is continue.");
            resumedCalled = false;

            scm.AcceptAll();
            Assert.That(resumedCalled, "An explicit method inclusion should resume the conversation");
            Assert.That(conversationContainer.BindedConversationCount, Is.EqualTo(0),
                        "Should have NO active conversation because the method AcceptAll end the conversation.");
        }