Try fast search NHibernate

19 October 2009

NHibernate.Validator : easy Entity Validator via Loquacious

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 Range
{
public int Start { get; set; }
public int End { get; set; }
}
To 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?

Well… we done because:
  1. if you want configure NHV by attribute you need an attribute
  2. 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.
  3. 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.

Happy coding with NHV.

2 comments: