IValidatableObject vs Single Responsibility
- by Boris Yankov
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?