Basically, replacing switch with design mode is definitely a bug and over-design. Consider that a few lines of code to modify a file can solve the problem, become modified 3-6 classes to achieve the same functionality. Not stupid, what?

Source: Internet
Author: User

Those superstitious design patterns, let's revise this method. See how your final code expands to several times ...

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;
}

Basically, replacing switch with design mode is definitely bug and over-design. Consider that a few lines of code to modify a file can solve the problem, become modified 3-6 classes to achieve the same functionality. Not stupid, what?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.