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?