public void Accrue(RentalAgreement agreement)
        {
            // defensive programming
            if(agreement == null) throw new ArgumentNullException("agreement");

            // logging
            Console.WriteLine("Accrue: {0}", DateTime.Now);
            Console.WriteLine("Customer: {0}", agreement.Customer.Id);
            Console.WriteLine("Vehicle: {0}", agreement.Vehicle.Id);

            // exception handling
            _exceptionHandler.Wrapper(() =>
                {
                    _transactionManager.Wrapper(() =>
                        {
                            var rentalTimeSpan =
                                (agreement.EndDate.Subtract(agreement.StartDate));
                            var numberOfDays = (int) Math.Floor(rentalTimeSpan.TotalDays);
                            var pointsPerDay = 1;
                            if (agreement.Vehicle.Size >= Size.Luxury)
                                pointsPerDay = 2;
                            var points = numberOfDays*pointsPerDay;
                            _loyaltyDataService.AddPoints(agreement.Customer.Id, points);

                            // logging
                            Console.WriteLine("Accrue complete: {0}", DateTime.Now);
                        });
                });
        }
        public void Accrue(RentalAgreement agreement)
        {
            // defensive programming
            if(agreement == null) throw new ArgumentNullException("agreement");

            // logging
            Console.WriteLine("Accrue: {0}", DateTime.Now);
            Console.WriteLine("Customer: {0}", agreement.Customer.Id);
            Console.WriteLine("Vehicle: {0}", agreement.Vehicle.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 rentalTime =
                                (agreement.EndDate.Subtract(
                                    agreement.StartDate));
                            var days = (int) Math.Floor(rentalTime.TotalDays);
                            var pointsPerDay = 1;
                            if (agreement.Vehicle.Size >= Size.Luxury)
                                pointsPerDay = 2;
                            var pts = days * pointsPerDay;
                            _dataService.AddPoints(agreement.Customer.Id, pts);

                            // complete transaction
                            scope.Complete();
                            succeeded = true;
                        }
                        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;
            }

            // logging
            Console.WriteLine("Accrue complete: {0}", DateTime.Now);
        }
 public void Accrue(RentalAgreement agreement)
 {
     var rentalTime =
         (agreement.EndDate.Subtract(
             agreement.StartDate));
     var days = (int) Math.Floor(rentalTime.TotalDays);
     var pointsPerDay = 1;
     if (agreement.Vehicle.Size >= Size.Luxury)
         pointsPerDay = 2;
     var pts = days * pointsPerDay;
     _dataService.AddPoints(agreement.Customer.Id, pts);
 }
Example #4
0
 static void SimulateAddingPoints(ILoyaltyAccrualService service)
 {
     var rentalAgreement = new RentalAgreement
     {
         Customer = new Customer
         {
             Id = Guid.NewGuid(),
             Name = "Matthew D. Groves",
             DateOfBirth = new DateTime(1980, 2, 10),
             DriversLicense = "RR123456"
         },
         Vehicle = new Vehicle
         {
             Id = Guid.NewGuid(),
             Make = "Honda",
             Model = "Accord",
             Size = Size.Compact,
             Vin = "1HABC123"
         },
         StartDate = DateTime.Now.AddDays(-3),
         EndDate = DateTime.Now
     };
     service.Accrue(rentalAgreement);
 }