码迷,mamicode.com
首页 > 其他好文 > 详细

基本上,把switch,用设计模式代替,肯定是bug和过度设计。想想,本来修改一个文件几行代码可以解决的问题,变成修改3-6个类才能实现一样的功能。不是傻是什么?

时间:2014-12-10 07:03:10      阅读:503      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   for   on   文件   问题   bs   代码   

 

那些迷信设计模式的人,来修改一下这个方法吧。看看你最终的代码膨胀为几倍。。。

public virtual PasswordChangeResult ChangePassword(ChangePasswordRequest request)
{
if (request == null)
throw new ArgumentNullException("request");

var result = new PasswordChangeResult();
if (String.IsNullOrWhiteSpace(request.Email))
{
result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailIsNotProvided"));
return result;
}
if (String.IsNullOrWhiteSpace(request.NewPassword))
{
result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.PasswordIsNotProvided"));
return result;
}

var customer = _customerService.GetCustomerByEmail(request.Email);
if (customer == null)
{
result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailNotFound"));
return result;
}


var requestIsValid = false;
if (request.ValidateRequest)
{
//password
string oldPwd = "";
switch (customer.PasswordFormat)
{
case PasswordFormat.Encrypted:
oldPwd = _encryptionService.EncryptText(request.OldPassword);
break;
case PasswordFormat.Hashed:
oldPwd = _encryptionService.CreatePasswordHash(request.OldPassword, customer.PasswordSalt, _customerSettings.HashedPasswordFormat);
break;
default:
oldPwd = request.OldPassword;
break;
}

bool oldPasswordIsValid = oldPwd == customer.Password;
if (!oldPasswordIsValid)
result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.OldPasswordDoesntMatch"));

if (oldPasswordIsValid)
requestIsValid = true;
}
else
requestIsValid = true;


//at this point request is valid
if (requestIsValid)
{
switch (request.NewPasswordFormat)
{
case PasswordFormat.Clear:
{
customer.Password = request.NewPassword;
}
break;
case PasswordFormat.Encrypted:
{
customer.Password = _encryptionService.EncryptText(request.NewPassword);
}
break;
case PasswordFormat.Hashed:
{
string saltKey = _encryptionService.CreateSaltKey(5);
customer.PasswordSalt = saltKey;
customer.Password = _encryptionService.CreatePasswordHash(request.NewPassword, saltKey, _customerSettings.HashedPasswordFormat);
}
break;
default:
break;
}
customer.PasswordFormat = request.NewPasswordFormat;
_customerService.UpdateCustomer(customer);
}

return result;
}

基本上,把switch,用设计模式代替,肯定是bug和过度设计。想想,本来修改一个文件几行代码可以解决的问题,变成修改3-6个类才能实现一样的功能。不是傻是什么?

标签:io   ar   sp   for   on   文件   问题   bs   代码   

原文地址:http://www.cnblogs.com/apachestorm/p/4154573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!