Castle Validator uses attributes to specify validation rules. How can you hook these up with Subsonic's generated classes (or any classes where you can't define the attributes on)? Is there a way to programatically specify validation rules without using the attribute method?
There are a few ways to do this - attributes is the lowest friction option, but obviously doesn't deal well with generated code or validation of multiple properties better expressed in code.
Take a look at the following link for some indications on how to do this blog post: Castle Validator Enhancements
If you have a look at the castle source code these are some good starting points:
Hope that helps.
I think the best way to do that is using MetadataType
.
It's a DataAnnotations
that let's your class have like a pair or something like that. I don't know how to explain it better so, let's for the samples:
You first need to add this directive to your code:
using System.ComponentModel.DataAnnotations;
Them you should create the partial class of you generated class with an attribute specifying that this class has an MetadataType
:
[MetadataType(typeof(UserMetadata))]
public partial class User
{
}
Then you create your metadata class with your castle validation:
public class UserMetadata
{
[ValidateNonEmpty]
[ValidateLength(6, 24)]
public string Username { get; set; }
[ValidateNonEmpty]
[ValidateLength(6, 100)]
[ValidateEmail]
public string Email { get; set; }
[ValidateNonEmpty]
[ValidateLength(6, 24)]
public string Password { get; set; }
}
Hope it helps