Simple Object Validation?
-
Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad
-
Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad
There are several methods for this. I created one using Extension methods in C#. The basic idea would be to create a class that implements a static method to implement a generic object. This method should accept the object to validate and a set of rules to validate the object against. If the validation fails the method should throw an exception containing the reason why the validation failed (Preferrably a message configured on a rule that failed validation). Implementation could look like this:
public static class ObjectValidator {
public static void Validate(object objectToValidate,ValidationRuleSet ruleSet) {
foreach(ValidationRule rule in ruleSet) {
if(!rule.Validate(objectToValidate)) {
throw new ValidationException(rule.ErrorMessage);
}
}
}
}Of course you need to expand this to support validation of specific properties each with multiple rules to check required fields and validate the value of the field against some pattern for example. There should be some standard components out there that can be quite a timesaver here. Last time I checked Microsoft had a validation application block in their enterprise library which should fit quite nicely.
WM. What about weapons of mass-construction? "What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson My blog
-
Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad
Take a look at Validation Application Block (http://msdn2.microsoft.com/en-us/library/bb410105.aspx[^]).
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
Take a look at Validation Application Block (http://msdn2.microsoft.com/en-us/library/bb410105.aspx[^]).
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne MetcalfeIn my search for information on how to design validation, I did find this as well. The only problem is that I'm working with VS 2003. I don't think I can do the .NET 3.0 stuff with that. Can I? If I cannot do the 3.0 stuff, I'm torn, because it doesn't make any sense to re-work what's already been done with the Application Blocks. I'm already using the older application blocks for data access, logging, and exception logging.