public void It_sends_all_messages_at_once_after_transaction_is_committed()
        {
            var connectionMock = MockRepository.GenerateMock<IEventStoreConnection>();
            connectionMock.Expect(x => x.AppendToStream(Arg<string>.Is.Anything,
                Arg<int>.Is.Anything,
                Arg<IEnumerable<EventData>>.Is.Anything, Arg<UserCredentials>.Is.Anything)).Repeat.Once().Return(new WriteResult());

            var connectionManager = new FakeConnectionManager()
            {
                Connection = connectionMock
            };
            var uow = new TransactionalUnitOfWork(connectionManager)
            {
                EndpointAddress = new Address("q", "m")
            };

            using (var tx = new TransactionScope())
            {
                uow.Send(new EventData(Guid.NewGuid(), "", false, new byte[0], new byte[0]));
                uow.Send(new EventData(Guid.NewGuid(), "", false, new byte[0], new byte[0]));
                tx.Complete();
            }

            connectionMock.VerifyAllExpectations();
        }
 public void Send(TransportMessage message, Address address)
 {
     if (eventSourcedUnitOfWork.IsInitialized)
     {
         eventSourcedUnitOfWork.Publish(message.ToIndirectCommandEventData(address));
     }
     if (Transaction.Current != null)
     {
         transactionalUnitOfWork.Send(message.ToIndirectCommandEventData(address));
     }
     else
     {
         connectionManager.GetConnection()
         .AppendToStream(address.GetReceiveAddressFrom(EndpointAddress), ExpectedVersion.Any, message.ToDirectCommandEventData(address));
     }
 }
        public bool Publish(TransportMessage message, IEnumerable <Type> eventTypes)
        {
            var eventData = message.ToEventEventData(eventTypes);

            if (eventSourcedUnitOfWork.IsInitialized)
            {
                eventSourcedUnitOfWork.Publish(eventData);
            }
            else if (Transaction.Current != null)
            {
                transactionalUnitOfWork.Send(eventData);
            }
            else
            {
                connectionManager.GetConnection().AppendToStream(EndpointAddress.GetFinalOutgoingQueue(), ExpectedVersion.Any, eventData);
            }
            return(true);
        }
        public void It_does_not_send_events_to_store_before_transaction_is_committed()
        {
            var connectionMock = MockRepository.GenerateMock<IEventStoreConnection>();
            connectionMock.Expect(x => x.AppendToStream(Arg<string>.Is.Anything,
                Arg<int>.Is.Anything,
                Arg<EventData[]>.Is.Anything)).Repeat.Never();

            var connectionManager = new FakeConnectionManager()
                {
                    Connection = connectionMock
                };
            var uow = new TransactionalUnitOfWork(connectionManager)
                {
                    EndpointAddress = new Address("q","m")
                };

            using (new TransactionScope())
            {
                uow.Send(new EventData(Guid.NewGuid(), "", false, new byte[0], new byte[0]));
            }

            connectionMock.VerifyAllExpectations();
        }