I personally don't like how that article describes passing errors from a service layer back to the controller (with IValidationDictionary), I would make it work more like this instead:
[Authorize]
public class AccountController : Controller
{
private readonly IMembershipService membershipService;
// service initialization is handled by IoC container
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
// .. some other stuff ..
[AllowAnonymous, HttpPost]
public ActionResult Register(RegisterModel model)
{
if (this.ModelSteate.IsValid)
{
var result = this.membershipService.CreateUser(
model.UserName, model.Password, model.Email, isApproved: true
);
if (result.Success)
{
FormsAuthentication.SetAuthCookie(
model.UserName, createPersistentCookie: false
);
return this.RedirectToAction("Index", "Home");
}
result.Errors.CopyTo(this.ModelState);
}
return this.View();
}
}
Or.. as mikalai mentioned, make the service throw validation exceptions, catch them in a global filter and insert into model state.