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.AddControllers();

            // registra repository in memory per le categorie
            services.AddSingleton <ICategoriesRepository, InMemoryCategoriesRepository>(sp =>
            {
                var categoriesRepo = new InMemoryCategoriesRepository();
                categoriesRepo.Add(new Categoria(1, "Networking")); // prodotti presenti nel negozio locale
                categoriesRepo.Add(new Categoria(20, "Game"));      // prodotti da richiedere al fornitore
                return(categoriesRepo);
            });

            // registra repository in memory per i prodotti
            services.AddSingleton <IProductsRepository, InMemoryProductsRepository>(sp =>
            {
                // aggiunge alcuni prodotti nel negozio locale
                var productsRepo       = new InMemoryProductsRepository();
                var categoriesRepo     = sp.GetService <ICategoriesRepository>();
                var networkingCategory = categoriesRepo.GetByDescription("Networking").Result;
                var router             = new Prodotto(1, "Router", networkingCategory);
                var accessPoint        = new Prodotto(2, "Access point", networkingCategory);
                var switchDevice       = new Prodotto(3, "Switch", networkingCategory);
                productsRepo.Add(router);
                productsRepo.Add(accessPoint);
                productsRepo.Add(switchDevice);
                return(productsRepo);
            });


            services.AddSingleton <IProductsRetrieverFactory, ProductsRetrieverCustomFactory>(sp =>
            {
                // registra un factory method per definire come verranno ottenuti i prodotti
                return(new ProductsRetrieverCustomFactory(category =>
                {
                    if (category.Id <= 10)
                    {
                        return new LocalStoreProductsRetriever(category, sp.GetService <IProductsRepository>());
                    }
                    else
                    {
                        return new RemoteStoreProductsRetriever("https://localhost:5011/", "catalogofornitore", category);
                    }
                }));
            });


            // registra un servizio per la gestione custom degli errori della pipeline
            services.AddTransient <IErrorHandlerService, CustomErrorHandlerService>();
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services
            .AddAuthentication("BasicAuthentication")
            .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

            // registra un semplice sistema di controllo delle credenziali utente ricevute
            // che in questo caso convalida qualunque username/password
            // Un sistema più evoluto si appoggerebbe ad un repository di utenti
            services.AddScoped <IUserAuthenticatorService, EveryUserAuthenticatorService>();

            // registra repository in memory per le categorie
            services.AddSingleton <ICategoriesRepository, InMemoryCategoriesRepository>(sp =>
            {
                var categoriesRepo = new InMemoryCategoriesRepository();
                categoriesRepo.Add(new Categoria(20, "Game")); // categoria di prodotti gestiti dal fornitore
                return(categoriesRepo);
            });

            // registra repository in memory per i prodotti
            services.AddSingleton <IProductsRepository, InMemoryProductsRepository>(sp =>
            {
                // aggiunge alcuni prodotti del fornitore
                var productsRepo       = new InMemoryProductsRepository();
                var categoriesRepo     = sp.GetService <ICategoriesRepository>();
                var networkingCategory = categoriesRepo.GetByDescription("Game").Result;
                productsRepo.Add(new Prodotto(101, "Playstation", networkingCategory));
                productsRepo.Add(new Prodotto(102, "XBox", networkingCategory));
                productsRepo.Add(new Prodotto(103, "Nintendo", networkingCategory));
                return(productsRepo);
            });

            services.AddSingleton <IProductsRetrieverFactory, ProductsRetrieverCustomFactory>(sp =>
            {
                // registra una factory per il retriever dei prodotti prelevandoli solo localmente
                return(new ProductsRetrieverCustomFactory(category =>
                                                          new LocalStoreProductsRetriever(category, sp.GetService <IProductsRepository>())));
            });

            // registra un servizio per la gestione custom degli errori della pipeline
            services.AddTransient <IErrorHandlerService, CustomErrorHandlerService>();
        }