public void MongoDbProdManagment() { try { InsertProduct(); Console.WriteLine("********Product MongoDB implementation********"); RemoveProduct(new MongoProduct { Name = "Pepsodent Tootpaste", Price = 50.37m, ProductInStock = 10, ManufacturerDetails = new Manufacturer() { ManufacturerName = "ERE", PhoneNumber = 762892333, Place = "Redbergplasten" } }); UpdateProduct(new MongoProduct { Name = "Toothbrush", Price = 50.78m, Id = "5ef5c2a3ac609330f4f799bf", ProductInStock = 10, ManufacturerDetails = new Manufacturer() { ManufacturerName = "TTT", PhoneNumber = 762892530, Place = "Hjalmar" } }); GetAllProducts(); GetByProductId("5ef5c2a2ac609330f4f799b1"); //ProductFilter-Search by ProductName IMongoDbProductRepository productRep = new MongoDbProductRepository(); IMongoStore storeRep = new MongoDbStoreRepository(); ProductFilter productFilter = new ProductFilter(productRep, storeRep); var searchProduct = productFilter.SearchProductByName("Vaseline").FirstOrDefault(); Console.WriteLine(); Console.WriteLine("********Matched record by ProductName************"); Console.WriteLine($"Name : {searchProduct.Name} , ProductId:{searchProduct.Id} ,Price: {searchProduct.Price},ProductInStock:{searchProduct.ProductInStock}, ManufacturerDetails : {searchProduct.ManufacturerDetails.ManufacturerName} ,ManufacturerDetails : {searchProduct.ManufacturerDetails.Place}, ManufacturerDetails : {searchProduct.ManufacturerDetails.PhoneNumber}"); //ProductFilter-ProductsCostingLessThan Console.WriteLine("********Matched 10 records by ProductPrice ************"); var matchedProduct = productFilter.ProductsCostingLessThan(130.12m); foreach (var records in matchedProduct) { Console.WriteLine($"Name : {records.Name} , ProductId:{records.Id} ,Price: {records.Price},ProductInStock:{records.ProductInStock}, ManufacturerDetails : {records.ManufacturerDetails.ManufacturerName} ,ManufacturerDetails : {records.ManufacturerDetails.Place}, ManufacturerDetails : {records.ManufacturerDetails.PhoneNumber}"); } //ProductFilter-Manufacturerdetails productFilter.ListOfManufacturersWithProductCount(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
//public static void Main(string[] args) public static void Run() { //Read configuration files //using Microsoft.Extensions.Configuration; which must be added as nuget package. //Change connection string for database in file appsettings.json or appsettings.dev.json for a special developer //database. IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", false, true) .AddJsonFile("appsettings.dev.json", true, true) .Build(); var mongoConnectionString = configuration["ConnectionStrings:DefaultConnection"]; //Using Entity Framework as ORM for accessing Relational database. // var context = new WareHouseContext(); // // context.Products.Add(new Entities.Product()); // context.SaveChanges(); // // PrintList("",context.Products); // //Composition Root. Create all objects we need. var _client = new MongoClient(mongoConnectionString); var _database = _client.GetDatabase("Warehouse"); var _collection = _database.GetCollection <Product>("Products"); IProductRepository productRepository = new MongoDbProductRepository(_collection); //new FileProductRepository(); IShopRepository shopRepository = new FileShopRepository(); ManufacturerService manufacturerService = new ManufacturerService(); ProductFilter productFilter = new ProductFilter(productRepository, shopRepository); var shop = new Shop() { Id = "1", Name = "Ica Maxi" }; var shop1 = new Shop() { Id = "2", Name = "Coop" }; //Create new product var product = ConsoleHelper.NewProduct(); //Add shops to product shopRepository.Insert(shop); shopRepository.Insert(shop1); product.AddShop(shop); product.AddShop(shop1); //Insert new product into repository productRepository.Insert(product); PrintList("Products for each Manufacturer", productFilter.ListOfManufacturersWithProductCount()); PrintList("Products costing less than 20", productFilter.ProductsCostingLessThan(20)); //Do some changes PrintList("All products", productRepository.GetAll().Select(p => p.Price)); product.Price = (decimal)123.45; ((MongoDbProductRepository)productRepository).Update(product); PrintList("All products", productRepository.GetAll().Select(p => p.Price)); }