Example #1
0
        public void Run()
        {
            const string PATH = "msgpump_tests";

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var es = new FilesInFolderEventstore(PATH);

            var sut = new MessagePump(es);

            sut.RegisterCommandPipeline(new IncrementPipeline());
            sut.RegisterCommandPipeline(new DecrementPipeline());
            sut.RegisterQueryPipeline(new CounterValuePipeline());


            var result = sut.Handle(new Increment {
                CounterId = "a"
            });

            Assert.IsType <Success>(result.response);
            result = sut.Handle(new Increment {
                CounterId = "b"
            });
            Assert.IsType <Success>(result.response);
            result = sut.Handle(new Increment {
                CounterId = "a"
            });
            Assert.IsType <Success>(result.response);

            var qresult = sut.Handle <CounterValue.Result>(new CounterValue {
                CounterId = "a"
            });

            Assert.Equal(2, qresult.response.Value);

            result = sut.Handle(new Decrement()
            {
                CounterId = "x"
            });
            Assert.IsType <Failure>(result.response);

            result = sut.Handle(new Decrement()
            {
                CounterId = "a"
            });
            Assert.IsType <Success>(result.response);
            qresult = sut.Handle <CounterValue.Result>(new CounterValue {
                CounterId = "a"
            });
            Assert.Equal(1, qresult.response.Value);
        }
Example #2
0
        public CompetitorsQueryResult Get_competitors()
        {
            _logger.LogInformation("competitors query");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new CompetitorsQueryContextManager(_es);
                var message_processor = new CompetitorsQueryProcessor();
                msgpump.Register <CompetitorsQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new CompetitorsQuery()) as CompetitorsQueryResult;
                return(result);
            }
        }
        public TournamentStockQueryResult Get_tournaments_infos()
        {
            _logger.LogInformation("tournaments stock query");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new TournamentStockQueryContextManager(_es);
                var message_processor = new TournamentStockQueryProcessor();
                msgpump.Register <TournamentStockQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new TournamentStockQuery()) as TournamentStockQueryResult;
                return(result);
            }
        }
        public PersonAvatarsQueryResult Load_persons_avatars()
        {
            _logger.LogInformation($"person avatars query");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new PersonAvtarsQueryContextManager(_es);
                var message_processor = new PersonAvtarsQueryProcessor();
                msgpump.Register <PersonAvatarsQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new PersonAvatarsQuery()) as PersonAvatarsQueryResult;
                return(result);
            }
        }
Example #5
0
        public PersonTemplateQueryResult Get_person_template()
        {
            _logger.LogInformation("person template query");

            using (var msgpump = new MessagePump())
            {
                var id_provider = new IdProvider();

                var context_manager   = new PersonTemplateQueryContextManager();
                var message_processor = new PersonTemplateQueryProcessor(id_provider);
                msgpump.Register <PersonTemplateQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new PersonTemplateQuery()) as PersonTemplateQueryResult;
                return(result);
            }
        }
Example #6
0
        public TournamentCompetitorsQueryResult Load_tournament_competitors(string tournamentId)
        {
            _logger.LogInformation($"tournament competitors query, tournament: { tournamentId }");

            using (var msgpump = new MessagePump())
            {
                var context_manager   = new TournamentCompetitorsQueryContextManager(_es);
                var message_processor = new TournamentCompetitorsQueryContextProcessor();
                msgpump.Register <TournamentCompetitorsQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new TournamentCompetitorsQuery()
                {
                    TournamentId = tournamentId
                }) as TournamentCompetitorsQueryResult;
                return(result);
            }
        }
        public TournamentRoundQueryResult Get_tournament_round(string tournamentId)
        {
            _logger.LogInformation($"tournament round query, tournament: {tournamentId}");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new TournamentRoundQueryContextManager(_es);
                var message_processor = new TournamentRoundQueryProcessor();
                msgpump.Register <TournamentRoundQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new TournamentRoundQuery()
                {
                    TournamentId = tournamentId
                }) as TournamentRoundQueryResult;
                return(result);
            }
        }
        public PersonStatsQueryResult Load_persons_stats(string personId)
        {
            _logger.LogInformation($"person stats query: { personId }");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new PersonStatsQueryContextManager(_es);
                var message_processor = new PersonStatsQueryProcessor();
                msgpump.Register <PersonStatsQuery>(context_manager, message_processor);

                var result = msgpump.Handle(new PersonStatsQuery()
                {
                    PersonId = personId
                }) as PersonStatsQueryResult;
                return(result);
            }
        }
Example #9
0
        public IActionResult Delete_tournament(DeleteTournamentCommand delete_tournament_command)
        {
            _logger.LogInformation($"delete tournament command, id: { delete_tournament_command.Id }");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new DeleteTournamentCommandContextManager(_es);
                var message_processor = new DeleteTournamentCommandProcessor();
                msgpump.Register <DeleteTournamentCommand>(context_manager, message_processor);

                var result = msgpump.Handle(delete_tournament_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Example #10
0
        public IActionResult Change_options(ChangeOptionsCommand change_options_command)
        {
            _logger.LogInformation($"change options command: { change_options_command.TournamentId }");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new ChangeOptionsCommandContextManager();
                var message_processor = new ChangeOptionsCommandProcessor();
                msgpump.Register <ChangeOptionsCommand>(context_manager, message_processor);

                var result = msgpump.Handle(change_options_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Example #11
0
        public IActionResult Store_person(StorePersonCommand store_person_command)
        {
            _logger.LogInformation($"store person command, id: { store_person_command.Id }");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new StorePersonCommandContextManager(_es);
                var message_processor = new StorePersonCommandProcessor();
                msgpump.Register <StorePersonCommand>(context_manager, message_processor);

                var result = msgpump.Handle(store_person_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
        public IActionResult Reset_match_result(MatchResetCommand match_reset_command)
        {
            _logger.LogInformation($"reset match command: {match_reset_command.MatchId}");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new StoreMatchResetCommandContextManager(_es);
                var message_processor = new StoreMatchResetCommandContextProcessor();
                msgpump.Register <MatchResetCommand>(context_manager, message_processor);

                var result = msgpump.Handle(match_reset_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
        public IActionResult Store_match_result(MatchResultNotificationCommand match_result_command)
        {
            var results = string.Join(" ", match_result_command.Results);

            _logger.LogInformation($"match result command, match: {match_result_command.MatchId}, result notification: { results }");

            using (var msgpump = new MessagePump(_es))
            {
                var context_manager   = new StoreMatchResultCommandContextManager(_es);
                var message_processor = new StoreMatchResultCommandProcessor();
                msgpump.Register <MatchResultNotificationCommand>(context_manager, message_processor);

                var result = msgpump.Handle(match_result_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Example #14
0
        public IActionResult Create_round(CreateRoundCommand create_round_command)
        {
            _logger.LogInformation($"create round command: { create_round_command.TournamentId }");

            using (var msgpump = new MessagePump(_es))
            {
                var id_provider = new IdProvider();

                var context_manager   = new CreateRoundCommandContextManager(_es);
                var message_processor = new CreateRoundCommandProcessor(id_provider);
                msgpump.Register <CreateRoundCommand>(context_manager, message_processor);

                var result = msgpump.Handle(create_round_command) as CommandStatus;
                if (result is Success)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Example #15
0
        static void Main(string[] args)
        {
            var ex = new StockExchangeProvider();

            using (var es = new EventStore("eventstream.db"))
                using (var msgpump = new MessagePump(es)) {
                    IMessageContextManager mcm;
                    IMessageProcessor      mp;

                    mcm = new BuyStockCommandContextManager();
                    mp  = new BuyStockCommandProcessor();
                    msgpump.Register <BuyStockCommand>(mcm, mp);

                    mcm = new SellStockCommandManager(es);
                    mp  = new SellStockCommandProcessor();
                    msgpump.Register <SellStockCommand>(mcm, mp);

                    mcm = new UpdatePortfolioCommandContextManager(es);
                    mp  = new UpdatePortfolioCommandProcessor(ex);
                    msgpump.Register <UpdatePortfolioCommand>(mcm, mp);

                    mcm = new CandidateStocksQueryContextManager();
                    mp  = new CandidateStocksQueryProcessor(ex);
                    msgpump.Register <CandidateStocksQuery>(mcm, mp);

                    mcm = new PortfolioQueryContextManager(es);
                    mp  = new PortfolioQueryProcessor();
                    msgpump.Register <PortfolioQuery>(mcm, mp);

                    mcm = new PortfolioStockQueryContextManager(es);
                    mp  = new PortfolioStockQueryProcessor();
                    msgpump.Register <PortfolioStockQuery>(mcm, mp);

                    var frontend = new UserInterface();


                    frontend.OnPortfolioQuery += q => {
                        var result = msgpump.Handle(q) as PortfolioQueryResult;
                        frontend.Display(result);
                    };

                    frontend.OnUpdatePortfolioCommand += c => {
                        msgpump.Handle(c);
                        var result = msgpump.Handle(new PortfolioQuery()) as PortfolioQueryResult;
                        frontend.Display(result);
                    };

                    frontend.OnCandidateStocksQuery += q => {
                        var result = msgpump.Handle(q) as CandidateStocksQueryResult;
                        if (frontend.SelectStockToBuy(result, out var cmd))
                        {
                            msgpump.Handle(cmd);
                            frontend.DisplayBuyConfirmation(cmd.StockSymbol, cmd.Qty, cmd.StockPrice);
                        }
                    };

                    frontend.OnPortfolioStockQuery += q => {
                        var result = msgpump.Handle(q) as PortfolioStockQueryResult;
                        if (frontend.SelectStockToSell(result, out var cmd))
                        {
                            msgpump.Handle(cmd);
                            frontend.DisplaySellConfirmation(cmd.StockSymbol);
                        }
                    };


                    frontend.Run();
                }
        }
Example #16
0
 public CommandStatus Handle(SyncCommand cmd) => _mp.Handle(cmd).Msg as CommandStatus;
Example #17
0
        public void Fluent()
        {
            var es  = new InMemoryEventstore();
            var sut = new MessagePump(es);


            sut.On <AddTodoCmd>().Use(new AddTodoCmdCtxModelManager()).Do(new AddTodoCmdProcessor());
            sut.On <AllTodosQuery>().Use(new AllTodosQueryCtxModelManager()).Do(new AllTodosQueryProcessor());
            var checkToDoCtxModelManager = new CheckTodoCmdCtxModelManager();

            sut.On <CheckTodoCmd>().Load(checkToDoCtxModelManager).Finally(checkToDoCtxModelManager).Do(new CheckTodoCmdProcessor());


            // add a couple of tasks
            var response = sut.Handle(new AddTodoCmd {
                Subject = "foo"
            });

            Assert.IsType <Success>(response.Msg);
            Assert.Empty(response.Notifications);

            response = sut.Handle(new AddTodoCmd {
                Subject = "bar"
            });
            Assert.IsType <Success>(response.Msg);
            Assert.Empty(response.Notifications);

            response = sut.Handle(new AddTodoCmd {
                Subject = "baz"
            });
            Assert.IsType <Success>(response.Msg);
            Assert.Empty(response.Notifications);

            // what are the currently active tasks?
            response = sut.Handle(new AllTodosQuery());
            Assert.Empty(response.Notifications);
            var result = response.Msg as AllTodosQueryResult;

            Assert.Equal(new[] { "foo", "bar", "baz" }, result.Todos.Select(x => x.Description));

            // check a task as done
            response = sut.Handle(new CheckTodoCmd {
                Id = result.Todos[0].Id
            });
            Assert.IsType <Success>(response.Msg);
            Assert.Empty(response.Notifications);

            // ...and it still gets listed
            response = sut.Handle(new AllTodosQuery());
            Assert.Empty(response.Notifications);
            result = response.Msg as AllTodosQueryResult;
            Assert.Equal(new[] { "foo, done", "bar", "baz" }, result.Todos.Select(x => x.Description));

            // ...unless done tasks are explicitly excluded
            response = sut.Handle(new AllTodosQuery {
                ActiveOnly = true
            });
            Assert.Empty(response.Notifications);
            result = response.Msg as AllTodosQueryResult;
            Assert.Equal(new[] { "bar", "baz" }, result.Todos.Select(x => x.Description));

            // check all remaining tasks - and a notification gets created, once all are done
            sut.Handle(new CheckTodoCmd {
                Id = result.Todos[0].Id
            });
            response = sut.Handle(new CheckTodoCmd {
                Id = result.Todos[1].Id
            });
            Assert.IsType <Success>(response.Msg);
            Assert.Single(response.Notifications);
            Assert.IsType <FreeCapacityAvailableNotification>(response.Notifications[0]);
        }
 public CommandStatus Handle(ExpandNumber cmd)
 => _mp.Handle(cmd).response as CommandStatus;
Example #19
0
 public CommandStatus Handle(StartGame cmd)
 => _ms.Handle(cmd).response as CommandStatus;