I wanted to comment on the use of IDataErrorInfo in the Asp.Net Mvc Release Candidate. Take a look at a sample that David Hayden put together to demonstrate how to use this feature.
try
{
UpdateModel<icreatecustomerform></icreatecustomerform>(customer);
// Do Something
return RedirectToAction(“Index”);
}
catch (InvalidOperationException ex)
{
return View(customer);
}
// …
}
The updateModel function will throw an InvalidOperationException if a member of the IDataErrorInfo returns a value indicating there was a validation error. I call this Exception Based Programming and I believe this is a horrible practice. Exceptions should be for exceptional cases, not a validation error which is a common ocurance in a line of business appliaction. I would propose that a better way to do this would be as follows:
I think this is a better way to handle this sort of code because it is very obvious about the call to IsValid . This does not deal with any exception handling non-sense thorugh the introduction of a ValidateModel filter. The validation step can be run and ModelState is populated before the real business starts.
The ValidateModel filter is part of the Code Camp Server sample application which you can find at www.CodeCampServer.org , it is a open source sample application build on top of the Asp.Net MVC framework.
Closing Words:
Do not write code that explicitly throws exceptions as part of the normal opperations of an application. This is bad for so many reasons but the worst reason is that the code is not obvious in what and why it is acting that way.
Comments ?
Post Footer automatically generated by Add Post Footer Plugin for wordpress.

