Ejemplo n.º 1
0
        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);

            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);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 该方法包含了积分系统累积客户积分的逻辑和规则
 /// </summary>
 /// <param name="agreement">租赁协议实体</param>
 public void Accrue(RentalAgreement agreement)
 {
     //防御性编程
     if (agreement == null)
     {
         throw new Exception("agreement为null!");
     }
     //日志
     Console.WriteLine("Accrue:{0}", DateTime.Now);
     Console.WriteLine("Customer:{0}", agreement.Customer.Id);
     Console.WriteLine("Vehicle:{0}", agreement.Vehicle.Id);
     try
     {
         using (var ts = new TransactionScope()) //开始一个新事务
         {
             var retries   = 3;                  //重试事务3次
             var succeeded = false;
             while (!succeeded)                  //一直循环,直到成功
             {
                 try
                 {
                     var rentalTimeSpan = agreement.EndDate.Subtract(agreement.StartDate);
                     var numberOfDays   = (int)rentalTimeSpan.TotalDays;
                     var pointsPerDay   = 1;
                     if (agreement.Vehicle.Size >= Size.Luxury)
                     {
                         pointsPerDay = 2;
                     }
                     var points = numberOfDays * pointsPerDay;
                     //调用数据服务存储客户获得的积分
                     _loyaltyDataService.AddPoints(agreement.Customer.Id, points);
                     ts.Complete();                                          //调用Complete方法表明事务成功提交
                     succeeded = true;                                       //成功后设置为true,确保最后一次循环迭代
                     Console.WriteLine("Accrue Complete:{0}", DateTime.Now); //这句移入try里
                 }
                 catch
                 {
                     if (retries >= 0)
                     {
                         retries--;//直到尝试完次数时才重抛异常
                     }
                     else
                     {
                         throw;//没有调用Complete方法,事务会回滚
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         if (!ExceptionHelper.Handle(ex))//如果没有处理异常,继续重抛
         {
             throw ex;
         }
     }
 }
Ejemplo n.º 3
0
        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 totalPoints = days * pointsPerDay;

            _loyaltyDataService.AddPoints(agreement.Customer.Id, totalPoints);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 包含积分系统累计客户积分逻辑和规则
        /// </summary>
        /// <param name="agreement"></param>
        public void Accrue(RentalAgreement agreement)
        {
            var rentalTimeSpan = agreement.EndDate.Subtract(agreement.StartDate);
            var numberOfDays   = (int)rentalTimeSpan.TotalDays;
            var pointsPerDay   = 1;

            if (agreement.Vehicle.Size >= Size.Luxury)
            {
                pointsPerDay = 2;
            }
            var points = pointsPerDay * numberOfDays;

            // 调用数据服务存储客户获得的积分
            _loyaltyDataService.AddPoints(agreement.Customer.Id, points);
        }
Ejemplo n.º 5
0
        public void Accrue(RentalAgreement agreement)
        {
            var pointsPerDay   = 1;
            var rentalTimeSpan = agreement.EndDate.Subtract(agreement.StartDate);
            var numberOfDays   = (int)Math.Floor(rentalTimeSpan.TotalDays);

            if (agreement.Vehicle.Size > Size.FullSize)
            {
                pointsPerDay = 2;
            }

            var totalPoints = numberOfDays * pointsPerDay;

            _dataService.AddPoints(agreement.Customer.Id, totalPoints);
        }
Ejemplo n.º 6
0
        public void Accrue(RentalAgreement agreement)
        {
            // Add Defensive Programming
            if (agreement == null)
            {
                throw new ArgumentNullException(nameof(agreement));
            }

            // Add Logging
            Console.WriteLine($"Accrue : {DateTime.Now}");
            Console.WriteLine($"Customer: {agreement.Customer.Id}");
            Console.WriteLine($"Vehicule: {agreement.Vehicule.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 rentalTimeSpan = agreement.EndDate.Subtract(agreement.StartDate);
                            var numberOfDays   = (int)Math.Floor(rentalTimeSpan.TotalDays);
                            var pointsPerDay   = 1;

                            if (agreement.Vehicule.Size >= Size.Luxury)
                            {
                                pointsPerDay = 2;
                            }

                            var points = numberOfDays * pointsPerDay;
                            _loyaltyDataService.AddPoints(agreement.Customer.Id, points);
                            // Business logic

                            // Complete transaction
                            scope.Complete();
                            succeded = true;

                            // Add Logging
                            Console.WriteLine($"Accrue 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;
            }
        }
        public void Accrue(RentalAgreement agreement)
        {
            // defensive programming
            if (agreement == null)
            {
                throw new ArgumentNullException(nameof(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);
        }