public IEntity Load(int id, SqlDataReader dataReader) { Discount discount = new Discount(); discount.Id = id; int productId = (int)dataReader["Product_Id"]; discount.Product = (Product)MapperRegistry.GetMapper(typeof(Product)).GetById(productId); return discount; }
static void Main(string[] args) { Database.SetInitializer(new DropCreateDatabaseAlways<PromoContext>()); Product rocketFuel = new Product { Name = "Rocket fuel", Price = 25.0m }; Product doubleBlast = new Product { Name = "Double blast", Price = 40.0m }; Product tripleBlast = new Product { Name = "Triple blast", Price = 40.0m }; PromoContext dbContext = new PromoContext(); IEnumerable<Product> products = dbContext.Products.ToArray(); var asd = products.ToArray(); //dbContext.Products.Add(rocketFuel); //dbContext.Products.Add(doubleBlast); dbContext.Products.Add(tripleBlast); Discount rocketFuelDiscount = new Discount { Product = rocketFuel, DiscountPolicy = new PromoDay { DayDate = DateTime.Today, DiscountPercentage = new Percentage(20) } }; Client josephDeer = new Client { Name = "Joseph Deer", Address = "Czerwone Maki 84", }; josephDeer.Offer(rocketFuelDiscount); Discount doubleBlastAccount = new Discount { Product = doubleBlast, DiscountPolicy = new DiscountUntilExpired { DiscountPercentage = new Percentage(50), FromTo = new DateSpan { StartDate = DateTime.Today, EndDate = DateTime.Today.AddDays(2) } } }; josephDeer.Offer(doubleBlastAccount); Client regularClient = new Client { Name = "Jack Ryan", Address = "Rynek 5" }; dbContext.Clients.Add(regularClient); dbContext.Clients.Add(josephDeer); Discount discount = new Discount { Product = new Product { Name = "discounted product", Price = 12.0m } }; dbContext.Discounts.Add(discount); dbContext.SaveChanges(); int relatedProductId = discount.Product.Id; Discount insertedDiscount = dbContext.Discounts.Single(d => d.Id == discount.Id); dbContext.Discounts.Remove(insertedDiscount); dbContext.SaveChanges(); Product releatedProduct = dbContext.Products.Single(p => p.Id == relatedProductId); insertedDiscount = dbContext.Discounts.Single(d => d.Id == discount.Id); }
public void DiscountShouldSaveRelatedProductIfNotSaved() { Product relatedProduct = new Product { Name = "Oil", Price = 89.0m }; Discount discount = new Discount { Product = relatedProduct }; mappingContext.DiscountsMapper.Insert(discount); mappingContext.SaveChanges(); int expectedRelatedProductId = relatedProduct.Id; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); using (SqlCommand sqlcommand = new SqlCommand("SELECT Product_Id FROM Discounts WHERE Id = " + discount.Id, sqlConnection)) { int actualProductId = (int)sqlcommand.ExecuteScalar(); Assert.AreEqual(expectedRelatedProductId, actualProductId); } } }
private static void DomainTest() { Product rocketFuel = new Product {Id = 1, Name = "Rocket fuel", Price = 25.0m}; Product doubleBlast = new Product {Id = 2, Name = "Double blast", Price = 40.0m}; Discount rocketFuelDiscount = new Discount { Product = rocketFuel, DiscountPolicy = new PromoDay {DayDate = DateTime.Today, DiscountPercentage = new Percentage(20)} }; Client josephDeer = new Client { Name = "Joseph Deer", Address = "Czerwone Maki 84", }; josephDeer.Offer(rocketFuelDiscount); Discount doubleBlastAccount = new Discount { Product = doubleBlast, DiscountPolicy = new DiscountUntilExpired { DiscountPercentage = new Percentage(50), FromTo = new DateSpan {StartDate = DateTime.Today, EndDate = DateTime.Today.AddDays(2)} } }; josephDeer.Offer(doubleBlastAccount); Client regularClient = new Client { Name = "Jack Ryan", Address = "Rynek 5" }; decimal regularPrice = regularClient.GetPriceForThisClient(doubleBlast); decimal discountedPrice = josephDeer.GetPriceForThisClient(doubleBlast); Console.WriteLine("double Blast regular price ${0}, discounted price only ${1}!", regularPrice, discountedPrice); regularPrice = regularClient.GetPriceForThisClient(rocketFuel); discountedPrice = josephDeer.GetPriceForThisClient(rocketFuel); Console.WriteLine("rocket fuel regular price ${0}, discounted price only ${1}!", regularPrice, discountedPrice); Console.ReadKey(); }
public void DiscountShouldGetRelatedProduct() { Product discountedProduct = new Product { Name = "Oil", Price = 89.0m }; mappingContext.ProductMapper.Insert(discountedProduct); mappingContext.SaveChanges(); Discount discount = new Discount { Product = discountedProduct }; mappingContext.DiscountsMapper.Insert(discount); mappingContext.SaveChanges(); int idToGet = discount.Id; Discount sut = new MappingContext(ConnectionStringName).DiscountsMapper.GetById(idToGet); Assert.AreEqual(discountedProduct.Id, sut.Product.Id); }
public void Offer(Discount discount) { Offers.Add(discount); }