ASP.NET MVC操作中的冗余消除

时间:2009-06-16 17:19:22

标签: c# asp.net-mvc refactoring

这个动作是否过于冗余 - 是否有更好的方法来简化它?

[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangePassword(string oldPassword, string newPassword, string confirmPassword)
{
    var oldPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(oldPassword);
    oldPasswordValidationResults.Where(r => !r.Passed)
                                .Each(r => ModelState.AddModelError("OldPassword", "Please enter your old password."));


    var newPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(newPassword);
    newPasswordValidationResults.Where(r => !r.Passed)
                                .Each(r => ModelState.AddModelError("NewPassword", "Please enter a new password."));

    if (!ModelState.IsValid)
        return View();

    if (newPassword != confirmPassword)
        ModelState.AddModelError("ConfirmPassword", "The passwords do not match.");

    if (!ModelState.IsValid)
        return View();

    if (!_userMembershipService.ChangePassword(oldPassword, newPassword))
        ModelState.AddModelError("_FORM", "Unable to change your password.");

    if (!ModelState.IsValid)
        return View();

    return View("ChangePasswordSuccessful");
}

所有这些在我看来都有代码味道......

if (!ModelState.IsValid)
    return View();

5 个答案:

答案 0 :(得分:1)

这种改变似乎可以更好地保留你原来的意图:

if (newPassword != confirmPassword)
{
    ModelState.AddModelError("ConfirmPassword", "The passwords do not match.");
    return View();
}

if (!_userMembershipService.ChangePassword(oldPassword, newPassword))
{
    ModelState.AddModelError("_FORM", "Unable to change your password.");
    return View();
}

return View("ChangePasswordSuccessful");

答案 1 :(得分:0)

不,我说你只需要最后一次IsValid检查。

当然,您可能希望保留倒数第二个,因为如果密码与旧密码相同,则更改密码可能会导致不必要的日志事件或其他内容,具体取决于底层成员资格框架。

答案 2 :(得分:0)

将所有基本验证(如字符串长度和类型)移至模型级别,这将减少大量代码,您可以查看xval框架

答案 3 :(得分:0)

是的,我会保留最后一次IsValid检查,所以:

[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangePassword(string oldPassword, string newPassword, string confirmPassword)
{
    var oldPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(oldPassword);
    oldPasswordValidationResults.Where(r => !r.Passed).Each(r => ModelState.AddModelError("OldPassword", "Please enter your old password."));


    var newPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(newPassword);
    newPasswordValidationResults.Where(r => !r.Passed).Each(r => ModelState.AddModelError("NewPassword", "Please enter a new password."));


    if (newPassword != confirmPassword)
        ModelState.AddModelError("ConfirmPassword", "The passwords do not match.");

    if (!_userMembershipService.ChangePassword(oldPassword, newPassword))
        ModelState.AddModelError("_FORM", "Unable to change your password.");

    if (!ModelState.IsValid)
        return View();

    return View("ChangePasswordSuccessful");
}

尽管如此,@ j-steen对于倒数第二次检查提出了一个很好的观点,可能会为您节省一些开销。

答案 4 :(得分:0)

嵌套if语句可能有助于简化代码:

[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangePassword(string oldPassword, string newPassword, string confirmPassword)
{
    var oldPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(oldPassword);
    oldPasswordValidationResults.Where(r => !r.Passed)
                                .Each(r => ModelState.AddModelError("OldPassword", "Please enter your old password."));


    var newPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(newPassword);
    newPasswordValidationResults.Where(r => !r.Passed)
                                .Each(r => ModelState.AddModelError("NewPassword", "Please enter a new password."));

    if (ModelState.IsValid) {
        if (newPassword == confirmPassword) {
            if (_userMembershipService.ChangePassword(oldPassword, newPassword)) {
                return View("ChangePasswordSuccessful");
            }
            else {
                ModelState.AddModelError("_FORM", "Unable to change your password.");
            }
        }
        else {
            ModelState.AddModelError("ConfirmPassword", "The passwords do not match.");
        }
    }

    return View();
}