IValidatableObject vs Single Responsibility
Posted
by
Boris Yankov
on Programmers
See other posts from Programmers
or by Boris Yankov
Published on 2012-06-15T12:13:19Z
Indexed on
2012/06/15
15:26 UTC
Read the original article
Hit count: 419
I like the extnesibility point of MVC, allowing view models to implement IValidatableObject, and add custom validation.
I try to keep my Controllers lean, having this code be the only validation logic:
if (!ModelState.IsValid)
return View(loginViewModel);
For example a login view model implements IValidatableObject, gets ILoginValidator object via constructor injection:
public interface ILoginValidator
{
bool UserExists(string email);
bool IsLoginValid(string userName, string password);
}
It seems that Ninject, injecting instances in view models isn't really a common practice, may be even an anti-pattern?
Is this a good approach? Is there a better one?
© Programmers or respective owner