Exemplo n.º 1
0
        public async Task <IActionResult> HireWarrior(String id, CharacterType characterType)
        {
            try
            {
                CaptainId captainId = new CaptainId(id);
                Command <Captain, CaptainId> command = null;
                switch (characterType)
                {
                case CharacterType.Warrior:
                    command = new HireWarrior(captainId, new Warrior());
                    break;

                case CharacterType.Wizard:
                    command = new HireWizard(captainId, new Wizard());
                    break;
                }
                if (command is null)
                {
                    return(BadRequest());
                }

                await _commandBus.PublishAsync(command, CancellationToken.None)
                .ConfigureAwait(false);
            }
            catch (System.Exception)
            {
                return(BadRequest());
            }
            return(Accepted());
        }
Exemplo n.º 2
0
        public async Task <CaptainReadModel> Captain(String id)
        {
            CaptainId captainId        = new CaptainId(id);
            var       captainReadModel = await _queryProcessor.ProcessAsync(new ReadModelByIdQuery <CaptainReadModel>(captainId), CancellationToken.None)
                                         .ConfigureAwait(false);


            return(captainReadModel);
        }
Exemplo n.º 3
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            using (var resolver = EventFlowOptions.New
                                  .AddEvents(typeof(CaptainCreated))
                                  .AddEvents(typeof(WizardHired))
                                  .AddEvents(typeof(WarriorHired))
                                  .AddCommands(typeof(CreateCaptain))
                                  .AddCommands(typeof(HireWarrior))
                                  .AddCommands(typeof(HireWizard))
                                  .AddCommandHandlers(typeof(CreateCaptainCommandHandlers))
                                  .AddCommandHandlers(typeof(HireWarriorCommandHandlers))
                                  .AddCommandHandlers(typeof(HireWizardCommandHandlers))
                                  .AddQueryHandlers(typeof(GetAllCaptainsQueryHandler))
                                  .UseInMemoryReadStoreFor <CaptainReadModel>()

                                  .CreateResolver())
            {
                CaptainId captainId = CaptainId.New;
                // Resolve the command bus and use it to publish a command
                var commandBus = resolver.Resolve <ICommandBus>();
                await commandBus.PublishAsync(new CreateCaptain(captainId), CancellationToken.None)
                .ConfigureAwait(false);

                // Resolve the query handler and use the built-in query for fetching
                // read models by identity to get our read model representing the
                // state of our aggregate root
                var queryProcessor   = resolver.Resolve <IQueryProcessor>();
                var captainReadModel = await queryProcessor.ProcessAsync(new ReadModelByIdQuery <CaptainReadModel>(captainId), CancellationToken.None)
                                       .ConfigureAwait(false);

                // Verify that the read model has the expected magic number
                //System.Console.WriteLine($"warriors: {captainReadModel.warriors} wizzards: {captainReadModel.wizards}");

                await commandBus.PublishAsync(new HireWarrior(captainId, new Warrior()), CancellationToken.None)
                .ConfigureAwait(false);

                await commandBus.PublishAsync(new HireWarrior(captainId, new Warrior()), CancellationToken.None)
                .ConfigureAwait(false);

                await commandBus.PublishAsync(new HireWizard(captainId, new Wizard()), CancellationToken.None)
                .ConfigureAwait(false);

                captainReadModel = await queryProcessor.ProcessAsync(new ReadModelByIdQuery <CaptainReadModel>(captainId), CancellationToken.None)
                                   .ConfigureAwait(false);

                System.Console.WriteLine($"warriors: {captainReadModel.warriors} wizzards: {captainReadModel.wizards}");

                // Resolve the command bus and use it to publish a command
                await commandBus.PublishAsync(new CreateCaptain(CaptainId.New), CancellationToken.None)
                .ConfigureAwait(false);

                captainId = CaptainId.New;
                await commandBus.PublishAsync(new CreateCaptain(captainId), CancellationToken.None)
                .ConfigureAwait(false);

                await commandBus.PublishAsync(new HireWizard(captainId, new Wizard()), CancellationToken.None)
                .ConfigureAwait(false);

                var captains = await queryProcessor.ProcessAsync(new GetAllCaptainQuery(), CancellationToken.None).ConfigureAwait(false);

                System.Console.WriteLine("------------------------");
                foreach (var captain in captains)
                {
                    System.Console.WriteLine($"ID: {captain.captainId}, warr: {captain.warriors}, wiz: {captain.wizards}");
                }
                System.Console.WriteLine("------------------------");
            }
        }