Beispiel #1
0
        public async Task When_The_Input_Same_CommandHandler_Should_Throw_InvalidOperationException()
        {
            Dictionary <Type, ICommandHandler> commandHandlers = new Dictionary <Type, ICommandHandler>();
            var            commandBus     = new CommandBus();
            CommandHandler commandHandler = new CommandHandler();
            await commandBus.SubscribeAsync(commandHandler);

            await Assert.ThrowsAsync <InvalidOperationException>(() => commandBus.SubscribeAsync(commandHandler));
        }
Beispiel #2
0
        public async Task SendAsync_Should_Handle_The_HandleAsync()
        {
            Dictionary <Type, ICommandHandler> commandHandlers = new Dictionary <Type, ICommandHandler>();
            var            commandBus     = new CommandBus();
            CommandHandler commandHandler = new CommandHandler();
            await commandBus.SubscribeAsync(commandHandler);

            var testCommand = new TestCommand();
            await commandBus.SendAsync(testCommand);

            Assert.True(testCommand.TestCommandValue);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddTransient <ITextSerializer, JsonTextSerializer>();

            services.AddTransient <Func <ITextSerializer> >(container =>
                                                            container.GetService <ITextSerializer>);
            services.AddTransient(typeof(IAsyncRepository <>), typeof(AsyncRepository <>));
            services.AddScoped(typeof(IAggregateRepositoryService <>), typeof(AggregateRepositoryService <>));

            //Command
            services.AddScoped <ICommandHandler <AddItemToBasketCommand>, AddItemToBasketHandler>();
            services.AddScoped <ICommandHandler <CreateBasketForUserCommand>, CreateBasketForUserCommandHandler>();
            services.AddScoped <ICommandHandler <CreatePurchaseOrderCommand>, CreatePurchaseOrderCommandHandler>();
            services.AddScoped <ICommandHandler <ProcessPurchaseOrderCommand>, ProcessPurchaseOrderCommandHandler>();

            services.AddScoped <ICommandBus>(container =>
            {
                var commandBus = new CommandBus();
                commandBus.SubscribeAsync(container.GetService <ICommandHandler <CreateBasketForUserCommand> >()).Wait();
                commandBus.SubscribeAsync(container.GetService <ICommandHandler <AddItemToBasketCommand> >()).Wait();
                commandBus.SubscribeAsync(container.GetService <ICommandHandler <CreatePurchaseOrderCommand> >()).Wait();
                commandBus.SubscribeAsync(container.GetService <ICommandHandler <ProcessPurchaseOrderCommand> >()).Wait();
                return(commandBus);
            });

            //Event
            services.AddScoped <IEventHandler <BasketCreatedEvent>, BasketViewModelGenerator>();
            services.AddScoped <IEventHandler <ItemAddedToBasketEvent>, BasketItemAddedViewModelGenerator>();
            services.AddScoped <IEventHandler <ProductPurchasedEvent>, ShippingInvoiceViewModelGenerator>();
            services.AddScoped <IEventHandler <SubscriptionItemPurchasedEvent>, SubscriptionItemPurchasedEventHandler>();
            services.AddScoped <IEventBus>(container =>
            {
                var eventBus = new EventBus();
                eventBus.SubscribeAsync(container.GetService <IEventHandler <BasketCreatedEvent> >()).Wait();
                eventBus.SubscribeAsync(container.GetService <IEventHandler <ItemAddedToBasketEvent> >()).Wait();
                eventBus.SubscribeAsync(container.GetService <IEventHandler <ProductPurchasedEvent> >()).Wait();
                eventBus.SubscribeAsync(container.GetService <IEventHandler <SubscriptionItemPurchasedEvent> >()).Wait();
                return(eventBus);
            });

            //Query
            services.AddScoped <IQueryHandler <GetAllBuyers,
                                               IReadOnlyList <Buyer> >, GetAllBuyersQueryHandler>();
            services.AddScoped <IQueryHandler <GetBasketByBuyerId,
                                               ApplicationCore.Basket.Query.ViewModel.Basket>, BasketQueryHandlers>();
            services.AddScoped <IQueryHandler <ShippingInvoiceQuery, ShippingInvoice>, GetShippingInvoiceQueryHandler>();
            services.AddScoped <BasketQueryHandlers>();
            services.AddScoped <IQueryBus>(container =>
            {
                var queryBus = new QueryBus();
                queryBus.SubscribeAsync(container.GetService <IQueryHandler <GetBasketByBuyerId,
                                                                             ApplicationCore.Basket.Query.ViewModel.Basket> >()).Wait();
                queryBus.SubscribeAsync(container.GetService <IQueryHandler <GetAllBuyers,
                                                                             IReadOnlyList <Buyer> > >()).Wait();
                queryBus.SubscribeAsync(container.GetService <IQueryHandler <ShippingInvoiceQuery,
                                                                             ShippingInvoice> >()).Wait();
                return(queryBus);
            });

            //logger
            services.AddScoped(typeof(IAppLogger <>), typeof(LoggerAdapter <>));

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMissingTypeMaps = true;
                cfg.AddProfile(new AutoMappingConfiguration());
            });
            var mapper = config.CreateMapper();

            services.AddSingleton(mapper);
        }
Beispiel #4
0
 public async Task When_The_Input_Command_Is_Null_Throw_ArgumentNullException()
 {
     var commandBus = new CommandBus();
     await Assert.ThrowsAsync <ArgumentNullException>(() => commandBus.SubscribeAsync((CommandHandler)null));
 }