内置模板
模型非常瘦小,基本上没有代码。
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}另一方面,当控制器看起来很胖的时候,做更简单的路由.
[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}发布于 2011-06-02 00:07:17
控制器在那里所做的一切都是它的工作,它是相当基本的东西。不会为您生成业务逻辑。当您将有用的功能封装到模型中时,模型就会变胖;您不能期望从模板中看到这一点。
发布于 2011-09-20 15:07:30
我很好奇你怎么认为控制器可以节食?老实说,我看不到任何东西可以在不违反SRP的情况下移动到模型上。
https://softwareengineering.stackexchange.com/questions/80879
复制相似问题