1

さまざまな属性を持ついくつかのプロパティを持つコンポーネントがあります

通常、これらの属性がプレーンな古いドメインオブジェクトに追加されると、カスタムAttributeConventionsによって取得されます。

コンポーネントのプロパティについては、そうではありません。これらに必要な追加の配線はありますか?

例えば

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

ColorとComponentNameの列を持つテーブルMyClassを取得します

Colorはnvarchar(50)であり、ComponentNameはnvarchar(255)(デフォルト)です。

4

1 に答える 1

2

OK、NHibernate.ValidatorsLengthAttributeをテーブル列の長さに結び付ける組み込みの魔法に頼るのは良い考えではないようです。魔法は、沼地の標準クラスの場合、これがFluentによって自然に取り上げられることです。それを強制するために、私はそれを処理するための独自のコンベンションを作成しました:

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);
        }
    }
于 2010-03-31T13:56:00.053 に答える