public void Redeem(Invoice invoice, int numberOfDays) { // defensive programming if(invoice == null) throw new ArgumentNullException("invoice"); if(numberOfDays <= 0) throw new ArgumentException("","numberOfDays"); // logging Console.WriteLine("Redeem: {0}", DateTime.Now); Console.WriteLine("Invoice: {0}", invoice.Id); _exceptionHandler.Wrapper(() => { _transactionManager.Wrapper(() => { var pointsPerDay = 10; if (invoice.Vehicle.Size >= Size.Luxury) pointsPerDay = 15; var points = numberOfDays*pointsPerDay; _loyaltyDataService.SubtractPoints(invoice.Customer.Id, points); invoice.Discount = numberOfDays*invoice.CostPerDay; // logging Console.WriteLine("Redeem complete: {0}", DateTime.Now); }); }); }
public void Redeem(Invoice invoice, int numberOfDays) { var pointsPerDay = 10; if (invoice.Vehicle.Size >= Size.Luxury) pointsPerDay = 15; var points = numberOfDays*pointsPerDay; _dataService.SubtractPoints(invoice.Customer.Id, points); invoice.Discount = numberOfDays*invoice.CostPerDay; }
public void Redeem(Invoice invoice, int numberOfDays) { // defensive programming if(invoice == null) throw new ArgumentNullException("invoice"); if(numberOfDays <= 0) throw new ArgumentException("","numberOfDays"); // logging Console.WriteLine("Redeem: {0}", DateTime.Now); Console.WriteLine("Invoice: {0}", invoice.Id); // exception handling try { // start new transaction using (var scope = new TransactionScope()) { // retry up to three times var retries = 3; var succeeded = false; while (!succeeded) { try { var pointsPerDay = 10; if (invoice.Vehicle.Size >= Size.Luxury) pointsPerDay = 15; var points = numberOfDays*pointsPerDay; _dataService.SubtractPoints(invoice.Customer.Id, points); invoice.Discount = numberOfDays*invoice.CostPerDay; // complete transaction scope.Complete(); succeeded = true; // logging Console.WriteLine("Redeem complete: {0}", DateTime.Now); } catch { // don't re-throw until the // retry limit is reached if (retries >= 0) retries--; else throw; } } } } catch(Exception ex) { if (!Exceptions.Handle(ex)) throw; } }
static void SimulateRemovingPoints(ILoyaltyRedemptionService service) { var invoice = new Invoice { Customer = new Customer { Id = Guid.NewGuid(), Name = "Jacob Watson", DateOfBirth = new DateTime(1977, 4, 15), DriversLicense = "RR009911" }, Vehicle = new Vehicle { Id = Guid.NewGuid(), Make = "Cadillac", Model = "Sedan", Size = Size.Luxury, Vin = "2BDI" }, CostPerDay = 29.95m, Id = Guid.NewGuid() }; service.Redeem(invoice, 3); }