// 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 Sample", Version = "v1"
                });
            });

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

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

                // Register methods with [CommandHandler] attribute.
                attributeRegistration.Register(() => new RegisterProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));
                attributeRegistration.Register(() => new ActivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));
                attributeRegistration.Register(() => new DeactivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));

                return(attributeRegistration);
            });

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

            services.AddMvc();
        }
        private static void SetupAttributeRegistration(IServiceCollection services)
        {
            services.AddSingleton <CommandHandlerAttributeRegistration>((serviceProvider) =>
            {
                // Register methods marked with [CommandHandler] attribute.
                var attributeRegistration = new CommandHandlerAttributeRegistration();
                attributeRegistration.Register(() => new DeactivateProductCommandHandler(serviceProvider.GetRequiredService <IProductRepository>()));

                return(attributeRegistration);
            });
        }
示例#3
0
            public async Task Should_Invoke_Registered_Attributed_Command_Handler()
            {
                var commandHandler = new TestAttributedCommandHandler(_outputHelper);
                var registration   = new CommandHandlerAttributeRegistration();

                registration.Register(() => 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);
            }
 public void Should_Not_Allow_Sync_Methods_With_Cancellation_Token()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         try
         {
             var registration = new CommandHandlerAttributeRegistration();
             registration.Register(() => new TestAttributedSyncCommandHandlerWithCancellationToken(_outputHelper));
         }
         catch (Exception ex)
         {
             _outputHelper.WriteLine(ex.ToString());
             throw;
         }
     });
 }
 public void Should_Not_Allow_Async_Void_CommandHandler_Methods()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         try
         {
             var registration = new CommandHandlerAttributeRegistration();
             registration.Register(() => new TestAttributedCommandHandlerWithAsyncVoid(_outputHelper));
         }
         catch (Exception ex)
         {
             _outputHelper.WriteLine(ex.ToString());
             throw;
         }
     });
 }
示例#6
0
            public void Should_Throw_When_No_Registered_Attribute_Command_Handler_Is_Found()
            {
                Assert.Throws <NoCommandHandlerResolvedException>(() =>
                {
                    var registration = new CommandHandlerAttributeRegistration();
                    var dispatcher   = new CommandDispatcher(registration);

                    try
                    {
                        dispatcher.Dispatch(new DoSomethingCommand());
                    }
                    catch (Exception ex)
                    {
                        _outputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                });
            }
            public void Should_Register_All_Command_Handlers_Methods()
            {
                var commandHandler = new TestAttributedCommandHandler(_outputHelper);

                var registration = new CommandHandlerAttributeRegistration();

                registration.Register(() => commandHandler);

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

                Assert.NotNull(commandHandlerDelegate);

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

                Assert.Equal(1, commandHandler.HandledCommands.Count);
                Assert.Contains(commandHandler.HandledCommands, c => c is DoSomethingCommand);
            }