2009-04-30 3 views
0

私はこのブログの記事(http://marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html)で見たパターンが好きです。ここでは、慣習を適用する前にテーブル名の変更が既に行われているかどうかを確認しています。流暢NHibernte改正/表記

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、

Berryl

答えて

0

は、ここではどのような作品です:取引かもしれない

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

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

+0

考え。私は文字列の長さのグループのための規則を追加しても構わないが、多くの場合は実際にクラスによって駆動される一回限りである。 EmployeeNumber文字列の長さが6文字の場合は、1つのフィールドのEmployeeNumberStringLengthConventionかDefaultStringLengthConventionのif文のいずれかになります。 クイックレスポンスのThx。 – Berryl

1

さらに良い方法が見つかるまで、HasAttribute("length")を使用できます。スチュアートとジェイミーは言っているコードに明確にするために

+0

これで十分ですが、動作しません。私は数週間でFNHを更新していない - それは最近の更新ですか?属性が「長さ」以外のものであってもかまいませんか? 返信いただきありがとうございます。 – Berryl

+0

SetAttributeを使用する場合、Berryl、HasAttributeが動作するはずです。リビジョン458以降、WithLengthOfはcolumnPropertiesというコレクションに "length"属性を格納します。 SetAttributeはextendedPropertiesに格納されます。これはHasAttributeがどこにあるかを示します。 SetAttribute/HasAttributeは、私が知る限り、この方法で働いていますので、あなたが使っているバージョンであればおそらく動作します。 –