private static void Initialize() { //install package Microsoft.Extensions.Configuration //install package Microsoft.Extensions.Configuration.FileExtensions //install package Microsoft.Extensions.Configuration.Json IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("DbSettings.json", optional: true, reloadOnChange: true); // plik json nie jest kopiowany do katalogu z programem wiec trzeba wejsc prawym properties // copy always IConfigurationRoot configuration = builder.Build(); //install package Microsoft.Extensions.DependencyInjection IServiceCollection services = new ServiceCollection(); services.AddSingleton <ICustomersService, DbCustomerService>(); services.AddSingleton <IItemsService, DbItemService>(); // install package Microsoft.Extensions.Logging.Console // services.AddSingleton<> services.AddDbContext <ShopContext>(options => options.UseSqlServer(configuration.GetConnectionString("ShopConnection"))); MyServiceProvider.RegisterServiceProvider(services.BuildServiceProvider()); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Initialize(); ICustomersService customerService = MyServiceProvider.GetServiceProvider().GetService <ICustomersService>(); foreach (Customer customer in customerService.Get()) { Console.WriteLine(customer); } Console.WriteLine("-------------------------------------------------------"); List <Customer> foundCustomers = customerService.SearchByCountry("Poland"); foreach (Customer customer in foundCustomers) { Console.WriteLine(customer); } Console.WriteLine("-------------------------------------------------------"); //GenerateItems(); //ICustomersService fakeCustomerService = new FakeCustomerService(); //List<Customer> customers = fakeCustomerService.Get(); // //foreach (Customer customer in customers) //{ // customerService.Add(customer); //} // FakeApproach(); }
private static void GenerateItems() { IItemsService itemService = MyServiceProvider.GetServiceProvider().GetService <IItemsService>(); Faker <Product> fakerProduct = new Faker <Product>() .StrictMode(true) .Ignore(p => p.Id) .RuleFor(p => p.Name, r => r.Commerce.ProductName()) .RuleFor(p => p.Color, r => r.Commerce.Color()) .RuleFor(p => p.Price, r => r.Finance.Amount()) .RuleFor(p => p.EAN, r => r.Finance.Iban()); Faker <Service> fakerService = new Faker <Service>() .StrictMode(true) .Ignore(s => s.Id) .RuleFor(s => s.Name, r => r.Commerce.ProductName()) .RuleFor(s => s.Price, r => r.Finance.Amount()) .RuleFor(s => s.EAN, r => r.Finance.Iban()); List <Item> items = new List <Item>(); Random random = new Random(); for (int i = 0; i < 200; i++) { if (0 == random.Next(0, 100) % 2) { items.Add(fakerProduct.Generate()); } else { items.Add(fakerService.Generate()); } } ShopContext context = MyServiceProvider.GetServiceProvider().GetService <ShopContext>(); context.Items.AddRange(items); context.SaveChanges(); }