views:

871

answers:

5

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?

+1  A: 

If you mean for ORMs then:

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.

Chris S
Thank you. I am not making web projects, so I'm asking about validations in general. Maybe they are called "object" validations (I don't know). Something in line with "Validate.NotNull", "Validate.InRange", etc.
Hosam Aly
+1  A: 

The Validation Application Block covers a wide range of validation cases, a little bit heavy though

Heartless Angel
+2  A: 

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.

As I recently blogged:

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.

Bevan
@Bevan: it seems to me you don't know Validation Application Block well enough. VAB supports the concept of Rule Sets. You can create two rule sets: one for errors, one for warnings. When the customer saves you check for errors, if none, you check for warnings and display any warnings to the customer (if any). If the customer is sure he wants to safe, you go save changes to the database (after checking again for errors of course).
Steven
That's good news - I haven't looked at the Validation Application Block in (literally) years, and wasn't aware that they had made that much needed enhancement.
Bevan
+1  A: 

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
}
Steven