5

From Microsoft's own built in template for MVC3

The model is extremely skinny, having basically no code.

Model

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

While the Controller on the other hand seems to be fat, doing more that simple routing...

Controller

[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);
}
Robert Harvey
  • 200,592
MVCylon
  • 597

2 Answers2

6

Everything the controller is doing there is its job, its pretty basic stuff. Business logic is not going to be generated for you. Models get fat when you encapsulate useful functionality into them; you can't expect to see that from a template.

Robert Harvey
  • 200,592
Jeremy
  • 4,597
0

I'm curious how you think the controller could be put on a diet? I honestly can't see anything there that could move to the model without violating SRP.

Bryan B
  • 2,814