0

このブログ投稿 ( http://marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html ) で見たパターンが気に入っています。ここでは、テーブル名の変更が既に行われているかどうかを著者がチェックしています。規則を適用する前に。

public bool Accept(IClassMap target)
{
    //apply this convention if table wasn't specified with WithTable(..) method
    return string.IsNullOrEmpty(target.TableName);
}

文字列の長さに使用している規約インターフェイスは IProperty です。

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        //apply if the string length hasn't been already been specified
        return ??; <------ ??
    }

    public void Apply(IProperty property) {
        property.WithLengthOf(50);
    }
}

IProperty が、プロパティが既に設定されているかどうかを示す情報を公開している場所がわかりません。これは可能ですか?

ティア、ベリル

4

3 に答える 3

1

より良い代替手段が登場するまでは、 を使用できますHasAttribute("length")

于 2009-05-01T19:30:06.977 に答える
1

.WithLengthOf()Action<XmlElement>XML マッピングの生成時に適用する変更のリストに「変更」( ) を追加します。残念ながら、そのフィールドはprivateあり、変更リストにアクセスするためのプロパティがないため、(現在) プロパティ マップがWithLengthOf適用されているかどうかを確認する方法はありません。

于 2009-04-30T16:48:52.537 に答える
0

Stuart と Jamie が何を言っているのかをコードで明確にするために、次のように動作します。

public class UserMap : IAutoMappingOverride<User>
{
    public void Override(AutoMap<User> mapping) {
        ...
        const int emailStringLength = 30;
        mapping.Map(x => x.Email)
            .WithLengthOf(emailStringLength)                        // actually set it
            .SetAttribute("length", emailStringLength.ToString());  // flag it is set
        ...

    }
}

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        return true;
    }

    public void Apply(IProperty property) {
        // only for strings
        if (!property.PropertyType.Equals(typeof(string))) return;

        // only if not already set
        if (property.HasAttribute("length")) return;

        property.WithLengthOf(50);
    }
}
于 2009-07-06T16:26:58.627 に答える