开发者

Fluent-NHibernate - Component property attributes ignored by Convention

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 w开发者_运维技巧iring 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)


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);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜