I was searching for a validation framework for .NET. I saw a few, but I didn't see any comparisons. Which one do you prefer to use, and why?
If you mean for ORMs then:
- NPersist has Validation built in
- ActiveRecord also has validation
ASP.NET already has a fairly decent one built in. If you are happy with client side form validation, there's a decent JQuery plugin to do it. You just give your input field classes for the type of validation you want, and call validate:
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
<input id="cemail" name="email" size="25" class="required email" />
Windows forms already has built in regex, and WPF I don't know about.
The Validation Application Block covers a wide range of validation cases, a little bit heavy though
I use my own validation framework because all of the ones I've seen online (including both the Microsoft Validation Application Block and EViL) treat validity as a boolean value.
Consider for example the “Part Maintenance” screen of an inventory system. Changing the PartNumber of a part is a big deal – it’s the real world primary key for this kind of part. Somebody has to have an access level allowing the change to be made, because sometimes part numbers do get changed – and sometimes errors occur. But, making such a change has pretty serious consequences – anyone trying to find that part needs to know that the number has changed.
How do you validate this situation? You can’t throw an error, because the change is acceptable, but you would like to let the user know that this is an action not to be taken lightly – “Changing the Part Number of this part will impact upon future data entry and searching.”
The key, in my opinion anyway, is that validation can't be entirely evaluated by your code - there will often (usually!) be aspects that have to be evaluated by the end user. In effect, you'll need to be relying on the judgment of your users. What you need to do is to prevent them from making serious errors (like leaving a delivery address off an order) and provide them with guidance to avoid other issues.
So, my validation uses Error for fatal issues that prevent further progress; Warning for serious issues that the user should consider, and Hint for guidance to help the user out. And, yes, those labels were inspired by compiler messages.
When you're looking for validation of method arguments, look at CuttingEdge.Conditions. It allows you to write code like this:
public ICollection GetData(Nullable<int> id, string xml, ICollection col)
{
// Check all preconditions:
Condition.Requires(id)
.IsNotNull() // throws ArgumentNullException on failure
.IsInRange(1, 999) // ArgumentOutOfRangeException on failure
.IsNotEqualTo(128); // throws ArgumentException on failure
Condition.Requires(xml)
.StartsWith("<data>") // throws ArgumentException on failure
.EndsWith("</data>"); // throws ArgumentException on failure
Condition.Requires(col)
.IsNotNull() // throws ArgumentNullException on failure
.IsEmpty(); // throws ArgumentException on failure
// Do some work
}