示例#1
0
        public void Configure(Configure config)
        {
            Builder    = config.Builder;
            Configurer = config.Configurer;

            NcqrsEnvironment.Configure(new NsbEnvironmentConfiguration(Builder));
            var compositeBus = new CompositeEventBus();

            _inProcessEventBus = new InProcessEventBus(false);
            compositeBus.AddBus(new NsbEventBus());
            compositeBus.AddBus(_inProcessEventBus);
            _sendingEventHandler = new MessageSendingEventHandler();
            _inProcessEventBus.RegisterHandler(_sendingEventHandler);
            NcqrsEnvironment.SetDefault(compositeBus);
            _messageService = new MessageService();
            config.Configurer.RegisterSingleton(typeof(IMessageService), _messageService);
        }
示例#2
0
        public void New_cargo_handling_event_is_registrered()
        {
            var cargoId        = Guid.NewGuid();
            var firstEventId   = Guid.NewGuid();
            var messageService = new MessageService();

            messageService.UseReceivingStrategy(
                new ConditionalReceivingStrategy(
                    x => x.GetType() == typeof(BookCargoMessage),
                    new MappingReceivingStrategy <BookCargoMessage>(
                        x => new IncomingMessage()
            {
                MessageId = x.MessageId,
                Payload   = x,
                ProcessingRequirements = MessageProcessingRequirements.RequiresNew,
                ReceiverId             = x.CargoId,
                ReceiverType           = typeof(Cargo),
                SenderId = "Client"
            })));

            //messageService
            //    .ForIncomingMessage<RegisterHandlingEventTransportMesasge>()
            //    .AsPayloadUse(x => x)
            //    .AsMessageIdUse(x => x.MessageId)
            //    .AsReceiverIdUse(x => x.EventId)
            //    .AsSenderUse("Client");

            //messageService
            //    .Map<RegisterHandlingEventTransportMesasge>().To<RegisterHandlingEventMessage>();

            messageService.UseReceivingStrategy(
                new ConditionalReceivingStrategy(
                    x => x.GetType() == typeof(RegisterHandlingEventMesasge),
                    new MappingReceivingStrategy <RegisterHandlingEventMesasge>(
                        x => new IncomingMessage()
            {
                MessageId = x.MessageId,
                Payload   = x,
                ProcessingRequirements = MessageProcessingRequirements.RequiresNew,
                ReceiverId             = x.EventId,
                ReceiverType           = typeof(HandlingEvent),
                SenderId = "Client"
            })));
            messageService.UseReceivingStrategy(new ConditionalReceivingStrategy(x => true, new LocalReceivingStrategy()));

            var messageSendingEventHandler = new MessageSendingEventHandler();
            var sendingStrategy            = new FakeSendingStrategy();

            messageSendingEventHandler.UseStrategy(new ConditionalSendingStrategy(x => true, sendingStrategy));
            ((InProcessEventBus)NcqrsEnvironment.Get <IEventBus>()).RegisterHandler(messageSendingEventHandler);

            //Book new cargo
            messageService.Process(new BookCargoMessage
            {
                CargoId   = cargoId,
                MessageId = Guid.NewGuid(),
            });

            //Register new handling event
            messageService.Process(new RegisterHandlingEventMesasge
            {
                EventId   = firstEventId,
                MessageId = Guid.NewGuid(),
                CargoId   = cargoId
            });

            //Process message from event to cargo
            object message = sendingStrategy.DequeueMessage();

            messageService.Process(message);

            using (var uow = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var cargo = uow.GetById <Cargo>(cargoId);
                Assert.AreEqual(1, cargo.HandlingEventCount);
            }
        }