那些迷信设计模式的人,来修改一下这个方法吧。看看你最终的代码膨胀为几倍。。。
1 public virtual PasswordChangeResult ChangePassword(ChangePasswordRequest request) 2 { 3 if (request == null) 4 throw new ArgumentNullException("request"); 5 6 var result = new PasswordChangeResult(); 7 if (String.IsNullOrWhiteSpace(request.Email)) 8 { 9 result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailIsNotProvided")); 10 return result; 11 } 12 if (String.IsNullOrWhiteSpace(request.NewPassword)) 13 { 14 result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.PasswordIsNotProvided")); 15 return result; 16 } 17 18 var customer = _customerService.GetCustomerByEmail(request.Email); 19 if (customer == null) 20 { 21 result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailNotFound")); 22 return result; 23 } 24 25 26 var requestIsValid = false; 27 if (request.ValidateRequest) 28 { 29 //password 30 string oldPwd = ""; 31 switch (customer.PasswordFormat) 32 { 33 case PasswordFormat.Encrypted: 34 oldPwd = _encryptionService.EncryptText(request.OldPassword); 35 break; 36 case PasswordFormat.Hashed: 37 oldPwd = _encryptionService.CreatePasswordHash(request.OldPassword, customer.PasswordSalt, _customerSettings.HashedPasswordFormat); 38 break; 39 default: 40 oldPwd = request.OldPassword; 41 break; 42 } 43 44 bool oldPasswordIsValid = oldPwd == customer.Password; 45 if (!oldPasswordIsValid) 46 result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.OldPasswordDoesntMatch")); 47 48 if (oldPasswordIsValid) 49 requestIsValid = true; 50 } 51 else 52 requestIsValid = true; 53 54 55 //at this point request is valid 56 if (requestIsValid) 57 { 58 switch (request.NewPasswordFormat) 59 { 60 case PasswordFormat.Clear: 61 { 62 customer.Password = request.NewPassword; 63 } 64 break; 65 case PasswordFormat.Encrypted: 66 { 67 customer.Password = _encryptionService.EncryptText(request.NewPassword); 68 } 69 break; 70 case PasswordFormat.Hashed: 71 { 72 string saltKey = _encryptionService.CreateSaltKey(5); 73 customer.PasswordSalt = saltKey; 74 customer.Password = _encryptionService.CreatePasswordHash(request.NewPassword, saltKey, _customerSettings.HashedPasswordFormat); 75 } 76 break; 77 default: 78 break; 79 } 80 customer.PasswordFormat = request.NewPasswordFormat; 81 _customerService.UpdateCustomer(customer); 82 } 83 84 return result; 85 }