Exemplo n.º 1
0
        // 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);
        }