In NHV (NHibernate.Validator) we have annoying issue regarding Entity validators (a validator for an entity instance).
You need an entity-validator when the constraint involves more than one property.
A simple and classic example is a range:
public class RangeTo validate it, before today, you must create an Attribute to mark the class Range and then an implementation of IValidator to validate the instance; annoying, no?
{
public int Start { get; set; }
public int End { get; set; }
}
Well… we done because:
- if you want configure NHV by attribute you need an attribute
- we like separation of concern so, the attribute is used to mark the entity and to hold some properties needed by the validator, and the concrete implementation of IValidator is used to validate an instance.
- Using configuration by XML we still need the attribute to define parameters and which will be the concrete implementation of IValidator used to validate an instance.
Clear… but… but now NHV has Loquacious configuration (configuration by fluent-interface) and it is all pure C# code, ergo we can use lambdas:
public class RangeDef : ValidationDef<Range>
{
public RangeDef()
{
ValidateInstance.By((instance, context) => instance.Start <= instance.End);
}
}
Obviously this usage is available only using Loquacious-configuration.
Nice work, this is asked a lot!
ReplyDelete@Gustavo
ReplyDeleteSure?