質問

このブログ投稿で見たパターンが気に入っています( 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が、プロパティが既に設定されているかどうかを示すものをどこに公開するかわかりません。これは可能ですか?

TIA、 ベリル

役に立ちましたか?

解決 3

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

他のヒント

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top