With loquacious, validators defined on base classes are not considered

Description

Consider the following example:

public class BaseEntity
{
public string StringValue { get; set; }
}
public class Entity : BaseEntity {}
public class BaseEntityValidation : ValidationDef<BaseEntity>
{
public BaseEntityValidation()
{
Define(x => x.StringValue).NotNullableAndNotEmpty();
}
}

When validating Entity, rules defined for BaseEntity aren't considered. The workaround is to add an empty class like the following:

public class EntityValidation : ValidationDef<Entity> {}

Attached is a test that demonstrate the bug.

Environment

None

Attachments

1

Activity

Show:

Fabio Maulo 
December 5, 2010 at 1:41 PM

Checked.
It is by design.
In practice without use Attributes we have to "guess" which is the best validator for an entity after the configuration and it is a "little bit" expensive especially when the entity implements interfaces.

For the Loquacious registration you can implements a simple extension like:

public static class FluentConfigurationExtensions
{
public static IFluentConfiguration RegisterEmptyValidator<TEntity>(this IFluentConfiguration source) where TEntity : class
{
source.Register<ValidationDef<TEntity>, TEntity>();
return source;
}
public static IFluentConfiguration RegisterEmptyValidator(this IFluentConfiguration source, IEnumerable<System.Type> entities)
{
source.Register(entities.Select(et => typeof(ValidationDef<>).MakeGenericType(new[] { et })));
return source;
}
}

With the first method you can register each "special case" one by one.
With the second method you can register all entities without an implementation of ValidationDef<T>.

Fabio Maulo 
December 5, 2010 at 12:26 PM

hmmmm define this as bug is a little bit strong.
btw I'll check.

Kamran Ayub 
September 23, 2010 at 6:03 AM

I am not well-versed in this but don't you need to actually be using NHibernate for this component to work?

Won't Fix

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created May 24, 2010 at 6:35 AM
Updated December 5, 2010 at 1:41 PM
Resolved December 5, 2010 at 1:41 PM
Who's Looking?