views:

428

answers:

3

I played around with nhibernate validator and got a nearly perfect solution.

I can define a property to be validated and it's done on pre-save. But I have some cases where it's not working.

Let's assume I have an object called person and via nhibernate I mapped the address(also an object) to be a property of person (in fact it's a list of addresses).

When I save the person my address is not validated.

The form to enter the information is made out of partial forms. Would be nice if the address validation could be added to the person's validation list, but thats not required.

I need a general solution, I cannot validate by "hand" e.g. if person then validate address... Somehow the validator should see that there is an object behind the property which I also have to validate.

Update: What I am looking for is a way to validate mapped objects (hasmany).

+1  A: 

If you're trying to put input validation in these classes, which is what I think you're trying to do, I would advise against it, as this is business logic. Anything you'll find in Hibernate that does this is only doing it to ensure constraints in the database (i.e., a not-null column).

What I would recommend is that you create a utility class or method to do the input validation for you. If you need to add database constraints to the entity properties, by all means do that, but I would not rely on them for input validation.

Jon Seigel
I need both, input validation and validation on business logic / server side. With xVal and nHibernate I get both with almost no effort. See my answer, we wrote about on the same time :)+1 for thinging into my problem ;)Generally I agree your suggestions, I was looking for a finished solution, writing this by myself would have been to much overhead for now.
griti
+1  A: 

After switching to the newest release of nhibernate validator the validation works for subclasses and mapped classes. Together with xVal 1.0 it's a very satisfying solution.

Now I can define on every property against what it should be validated (e.g. for a regex, length etc...) and I get the message on client side via xVal, on server side via nHibernate Validator. In fact they share the validation pattern and the error messages.

I would recommend this for any nHibernate-Project where a simple definition for validation and messaging is required.

griti
A: 

I guess my problem is the same. My object have a property that is list of list of custom att, and I want to validate them.

I'm doing something like this:

Validate(zoneToEdit);
List<ZoneCustomProperty> zcp = zoneToEdit.ZoneCustomProperties.ToList<ZoneCustomProperty>();
foreach ( ZoneCustomProperty aCustom in zcp ) {
    Validate( aCustom );            
}
if (!ValidationDictionary.IsValid) return false;

But this have no scalability and is really ugly.

This is your problem, right?

You can post the solution that you found?

Thanks in advance.

Luís Custódio
Are you using nHibernate and nHibernate Validator?In my case nHibernate Validator is called in DomainRepository before saving/merging the session.nHibernate is validating for defined properties in the class.You would have to define in ZoneCustomProperty class on the properties that they should be validated, and the message when the value is not correct.
griti
Yes, in my case too. This code that I posted was in my "zoneRepository", just before saving the session.My ZoneCustomProperty class have the validations of nHiberante Validator, but when I call validate to zone, the zcp validations are not called.
Luís Custódio