views:

390

answers:

2

Hello, everyone!

I've run into the problem with NHibernate proxy validation using Castle.Validator component. It's looks like validator could not fetch attributes from entity proxy's properties.

I've tried to define validation attributes using Inherited = true while Castle.Validator runner fetch em using following statement:

property.GetCustomAttributes(typeof(IValidatorBuilder), true);

So it should fetch attributes form my entities while they are technically base classes for object under validation (proxy)? Why does it not working? GetCustomAttribute always return empty array.

+1  A: 

The same thing happened to me. The problem is that NHibernate doesn't inherit the attributes when creating the proxies. All I found is this forum post in which the problem is recognized and gives a simple workaround (just don't use proxies when validating) and that's what I ended up doing.

May I recommend this validation library: Fluent Validation, I haven't used it yet but I'm planning to, mainly because I'm tired of using attributes (because of problems like this).

gcores
A: 

Attributes are fine because they are truly part of the domain model.

I think I found solution for the problem. I've patched Castle.Validator to do so thought. It's insane, but this code:

property.GetCustomAttributes(true /*inherit*/);

returns empty array, while in my humble opinion fully equivalent code returns attributes:

System.Attribute.GetCustomAttributes(property);

What the @#$% ?

Artem Tikhomirov