views:

151

answers:

1

I have a component with a number of properties that have various attributes

Normally when these attributes are added to a plain old domain object they are picked up by my custom AttributeConventions.

For the Component properties they are not. Is there some extra wiring needed for these?

e.g.

public class Component
{
    [Length(Max=50)]
    public virtual string Name {get; set;}
}

public class MyClass
{
    public virtual Component Component {get; set;}

    [Length(Max=50)]
    public virtual string Color {get; set;}
}

I get a table MyClass with columns Color & ComponentName

Color is an nvarchar(50) whilst ComponentName is an nvarchar(255) (the default)

A: 

OK so relying on the built-in magic that ties the NHibernate.Validators LengthAttribute to the length of your table column seems not to be a good idea. The magic is that for bog standard classes this gets picked up by Fluent naturally. In order to force it I've created my own Convention to handle it:

public class LengthConvention : AttributePropertyConvention<LengthAttribute>
    {
        protected override void Apply(LengthAttribute attribute, IPropertyInstance instance)
        {
            // override the default column length
            if (attribute.Max != default(int)) instance.Length(attribute.Max);
        }
    }
BobTodd