public void Redeem(Invoice invoice, int numberOfDays) { // defensive programming if (invoice == null) { throw new ArgumentNullException(nameof(invoice)); } if (numberOfDays <= 0) { throw new ArgumentException("", nameof(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; } } }
public void Redeem(Invoice invoice, int numberOfDays) { // Add Defensive Programming if (invoice == null) { throw new ArgumentNullException(nameof(invoice)); } if (numberOfDays <= 0) { throw new ArgumentException(nameof(numberOfDays)); } // Add Logging Console.WriteLine($"Redeem : {DateTime.Now}"); Console.WriteLine($"Invoice: {invoice.Id}"); // Add More exception Handling try { // Add Transaction using (var scope = new TransactionScope()) { // Add Retry logic var retries = 3; var succeded = false; while (!succeded) { try { // Business logic var pointsPerDay = 10; if (invoice.Vehicule.Size >= Size.Luxury) { pointsPerDay = 15; } var points = numberOfDays * pointsPerDay; _loyaltyDataService.SubtractPoints(invoice.Customer.Id, points); invoice.Discount = numberOfDays * invoice.CostPerDay; // Business logic // Complete transaction scope.Complete(); succeded = true; // Add Logging Console.WriteLine($"Redeem complete: {DateTime.Now}"); } catch { // Don't rethrow until the limit is reached if (retries >= 0) { retries--; } else { throw; } } } } } catch (Exception ex) { // Some exception handling logic. The Book code doesn't work, Something missing? //if (!ExceptionHandler.Handle(ex)) // throw; } }