コード例 #1
0
            public Task Should_Check_For_Correct_Command_Type()
            {
                return(Assert.ThrowsAnyAsync <ArgumentException>(async() =>
                {
                    var commandHandler = new TestCommandHandler(_testOutputHelper);

                    var registration = new CommandHandlerRegistration();
                    registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                    CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                    Assert.NotNull(commandHandlerDelegate);

                    try
                    {
                        // This delegate handles DoSomethingCommand, but was passed in a DoSomethingWithCancellationCommand.
                        await commandHandlerDelegate.Invoke(new DoSomethingForSpecifiedDurationCommand(100));
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
コード例 #2
0
            public Task Should_Throw_When_Cancelled()
            {
                return(Assert.ThrowsAnyAsync <OperationCanceledException>(async() =>
                {
                    var commandHandler = new TestCommandHandler(_outputHelper);
                    var registration = new CommandHandlerRegistration();
                    registration.Register(() => (ICommandAsyncHandler <DoSomethingForSpecifiedDurationCommand>)commandHandler);

                    var cts = new CancellationTokenSource();

                    var dispatcher = new CommandDispatcher(registration);
                    Task task = dispatcher.DispatchAsync(new DoSomethingForSpecifiedDurationCommand(2000), cts.Token);

                    cts.Cancel();

                    try
                    {
                        await task;
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
コード例 #3
0
        private void PassCommandToCommandBus(DomoTroller2.Common.ICommand handlerCommand)
        {
            CommandHandlerRegistration.RegisterCommandHandler();
            var commandBus = CommandBus.Instance;

            commandBus.Execute(handlerCommand);
        }
コード例 #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Swagger.
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "AspNetCore Basic Registration Sample", Version = "v1"
                });
            });

            // Repository.
            services.AddSingleton <IProductRepository, InMemoryProductRepository>();

            // Register command handler resolver. This is resolved by CommandDispatcher.
            services.AddSingleton <ICommandHandlerResolver>((serviceProvider) =>
            {
                // This object implements ICommandHandlerResolver.
                var registration = new CommandHandlerRegistration();

                // Needed to cast to ICommandHandler because below handlers implements both ICommandAsyncHandler and ICommandHandler.
                // The Register method accepts both interfaces so compiling is complaining that it is ambiguous.
                // We could also cast to ICommandAsyncHandler instead but decided to go with this
                // because I already did the ICommandAsyncHandler in the container registration sample.
                registration.Register(() => (ICommandHandler <RegisterProductCommand>) new RegisterProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));
                registration.Register(() => (ICommandHandler <ActivateProductCommand>) new ActivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));
                registration.Register(() => (ICommandHandler <DeactivateProductCommand>) new DeactivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));

                return(registration);
            });

            // Command dispatcher.
            services.AddSingleton <ICommandAsyncDispatcher, CommandDispatcher>();

            services.AddMvc();
        }
コード例 #5
0
        private static void SetupBasicRegistration(IServiceCollection services)
        {
            services.AddSingleton <CommandHandlerRegistration>((serviceProvider) =>
            {
                // Needed to cast to ICommandHandler because below handlers implements both ICommandAsyncHandler and ICommandHandler.
                // The Register method accepts both interfaces so compiling is complaining that it is ambiguous.
                var registration = new CommandHandlerRegistration();
                registration.Register(() => (ICommandAsyncHandler <ActivateProductCommand>) new ActivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));

                return(registration);
            });
        }
コード例 #6
0
            public async Task Should_Invoke_Registered_Command_Handler()
            {
                var commandHandler = new TestCommandHandler(_outputHelper);
                var registration   = new CommandHandlerRegistration();

                registration.Register(() => (ICommandAsyncHandler <DoSomethingCommand>)commandHandler);

                var dispatcher = new CommandDispatcher(registration);
                await dispatcher.DispatchAsync(new DoSomethingCommand());

                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }
コード例 #7
0
            public void Should_Register_All_Command_Handlers()
            {
                var commandHandler = new TestCommandHandler(_testOutputHelper);

                var registration = new CommandHandlerRegistration();

                registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                Assert.NotNull(commandHandlerDelegate);

                // Delegate should invoke the actual command handler - TestCommandHandler.
                commandHandlerDelegate.Invoke(new DoSomethingCommand());

                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }
コード例 #8
0
            public Task Should_Throw_When_No_Registered_Command_Handler_Is_Found()
            {
                return(Assert.ThrowsAsync <NoCommandHandlerResolvedException>(async() =>
                {
                    var registration = new CommandHandlerRegistration();
                    var dispatcher = new CommandDispatcher(registration);

                    try
                    {
                        await dispatcher.DispatchAsync(new DoSomethingCommand());
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
コード例 #9
0
            public void Should_Throw_When_No_Registered_Command_Handler_Is_Found()
            {
                Assert.Throws <NoCommandHandlerResolvedException>(() =>
                {
                    var registration = new CommandHandlerRegistration();
                    var dispatcher   = new CommandDispatcher(registration);

                    try
                    {
                        dispatcher.Dispatch(new DoSomethingCommand());
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                });
            }
コード例 #10
0
            public async Task Should_Invoke_The_Actual_Registered_Command_Handler()
            {
                var commandHandler = new TestCommandHandler(_testOutputHelper);

                var registration = new CommandHandlerRegistration();

                registration.Register(() => (ICommandHandler <DoSomethingCommand>)commandHandler);

                CommandHandlerDelegate commandHandlerDelegate = registration.ResolveCommandHandler <DoSomethingCommand>();

                Assert.NotNull(commandHandlerDelegate);

                // Invoke.
                await commandHandlerDelegate.Invoke(new DoSomethingCommand());

                // Check if actual command handler instance was invoked.
                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }
コード例 #11
0
            public void Should_Propagate_Exception_From_Command_Handler()
            {
                Assert.ThrowsAny <Exception>(() =>
                {
                    var registration = new CommandHandlerRegistration();
                    registration.Register(() => (ICommandHandler <ThrowExceptionCommand>) new TestCommandHandler(_outputHelper));

                    var dispatcher = new CommandDispatcher(registration);

                    try
                    {
                        dispatcher.Dispatch(new ThrowExceptionCommand());
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                });
            }
コード例 #12
0
            public Task Should_Propagate_Exception_From_Command_Handler()
            {
                return(Assert.ThrowsAnyAsync <Exception>(async() =>
                {
                    var commandHandler = new TestCommandHandler(_outputHelper);
                    var registration = new CommandHandlerRegistration();
                    registration.Register(() => (ICommandAsyncHandler <ThrowExceptionCommand>)commandHandler);

                    var dispatcher = new CommandDispatcher(registration);

                    try
                    {
                        await dispatcher.DispatchAsync(new ThrowExceptionCommand());
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
コード例 #13
0
ファイル: Startup.cs プロジェクト: Malrig/Expenses-Api
        /// <summary>
        /// This function adds all the commands and queries used by the application
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureApplicationServices(IServiceCollection services)
        {
            // Add all queries as available services
            services
            .AddScoped <IQueryHandler <FindAllExpenses, ExpensesOverview>, GetExpensesOverview>()
            .AddScoped <IQueryHandler <FindExpenseById, ExpenseDetail>, GetExpenseDetail>();
            //.AddScoped<IQueryHandler<FindExpenseLinesByExpense, ExpenseLineList>, GetExpenseLinesForExpense>();

            // Add all commands which don't require validation
            services
            .AddScoped <ICommandHandler <DeleteExpenseInfo>, DeleteExpense>()
            .AddScoped <ICommandHandler <DeleteExpenseLineInfo>, DeleteExpenseLine>();

            // Add commands which have validators associated with them
            // Should be of the format Command, CommandInfo, Validator
            CommandHandlerRegistration
            .RegisterCommandHandler <AddExpense, AddExpenseInfo, AddExpenseValidator>(services);
            CommandHandlerRegistration
            .RegisterCommandHandler <UpdateExpense, UpdateExpenseInfo, UpdateExpenseValidator>(services);
            CommandHandlerRegistration
            .RegisterCommandHandler <AddUpdateExpense, AddUpdateExpenseInfo, AddUpdateExpenseValidator>(services);
            CommandHandlerRegistration
            .RegisterCommandHandler <AddUpdateExpenseLine, AddUpdateExpenseLineInfo, AddUpdateExpenseLineValidator>(services);
        }