What is the reason to use the ModelState / ModelStateDictionary instead of a service-layer validation check

89 views Asked by At

In many online examples and tutorials we see if (!ModelState.IsValid) { return View(model); } or something similar. I have written a ModelState filter to actually make that check before each action execution. But the built-in component model attributes are quite limited: they only employ checks that would be possible to do on the client itself, offering nothing in the way of uniqueness checks, word filtering against a database, etc. So I often find myself writing a companion to my service layer base that involves rules for checking the "input model" to the service command and throwing it back if it fails some more detailed checks.

My question is, if I am not using any of the unobtrusive AJAX HTML helpers from Microsoft, is there any reason that I would bother with the very basic component model attributes? After all, they require you to make a new model, annotate each field, keep it up to date with the domain model, etc.

Also, if there is a benefit to using the ModelState, what is the preferred method to wire it up to service-layer validation checks? Or do people not do this for other reasons?

0

There are 0 answers