views:

781

answers:

4

Hi,

I'm trying to decide what validation approach to take for a new ASP.NET MVC project. (And wow there are plenty of options!)

The project uses NHibernate, so the first thing I considered was the NHibernate Validator (Because of tight integration with NHibernate). However, as far as I can see there are only a couple of benefits to this tight integration:

1) DB Schemas generated by NHibernate will include details of validation (e.g. column lengths will be set to max value allowed in validation). (This is not really of interest to me though, as I generate schemas manually.)

2) NHibernate will throw an exception if you try to save data that doesn't meet the validation specs. (This seems fairly redundant to me, since the data presumably will already be validated by whatever mechanism you choose before saving anyway)

If there are more benefits to NHibernate Validator please let me know!

Other libraries which I've been reading a little about include:

  • MS DataAnnotations
  • Castle Validator
  • Something else?

I've also been thinking about using xVal to provide client side validation from the same set of rules. However, I hear that ASP.NET MVC v2 will include something similar to xVal (integration with jquery) out of the box? Will this new included functionality render some of the others redundant?

So, I'm basically asking for people's advice on which direction to take here. I don't want to implement a particular scheme, only to have to rip it out when another one becomes the dominant tech.

What has worked for you? Which option do you think has/will have the edge?

Thanks!

+4  A: 

I like xVal.

You can implement very easily client and server validation with it. Also there is support for column (property) validation on entities that you would like to use.

Misha N.
Thanks. What underlying validation mechanism are you using? Castle? Data Annotations?
UpTheCreek
Not sure about Misha, but we use xVal + DataAnnotations. I built a T4 generator that emits our business objects and DAL, and it puts the appropriate attributes on the data members.
GalacticCowboy
DataAnnotations, but to be honest I've just chosen to work with DataAnnotations without investigation if some other mechanism would be better. So, I don't know why one would be better then another.
Misha N.
The thing that worries me about data annotation is that there currently is no runner provided by MS. The xVal project seems to supply one, but with caveats: [Quote from runner code comments]Runs each ValidationAttribute associated with a property on the supplied instance and returns an ErrorInfo relating to each validation failure. Caution: certain ValidationAttribute types claim to be valid even when they aren't - this runner would need to detect those special cases if you plan to rely on it. Fortunately, other validation runners...report all their errors correctly.[/Quote]
UpTheCreek
+1  A: 

DataAnnotations implemented by buddy classes and JQuery client validation

Make sure you're using MVC Preview 2

RailRhoad
Yeah, that does look like it will be the default choice in v2 doesn't it. I'm wondering if it's just them going the MS way with Data Annotations though, rather than a real best practice.
UpTheCreek
+4  A: 

I have been using FluentValidation along with jQuery validation plugin and still cannot find a situation they cannot handle.

Darin Dimitrov
This looks very interesting, thanks for posting. I'll take a deeper look later - first glance it seems it might be v good for business rule validation. Basic data validation may be a bit more verbose, but not a big deal. Do you know how well this setup handles internationalisation?
UpTheCreek
For i18n you could put all messages into a resources file and use them in the WithMessage FluentValidation method.
Darin Dimitrov
I've accepted this as the answer, as it's a library I've not seen before. It looks good, and I'm looking into implimenting it now.
UpTheCreek
+1  A: 

You might be interested in this delegate approach. I was because i didn't like the xVal idea (the solution im currently going with) and the fact that it didn't seem to cater for complex validation cases that crossed multiple properties of the same or even different class structures.

cottsak
Thanks, I'll look into this.
UpTheCreek