2009-04-30 36 views
0

我喜歡我在這篇博客文章(http://marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html)中看到的模式,作者正在檢查在應用約定之前是否已經創建了表名更改。Fluent NHibernte Alterations/Conventions

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()增加了一個 「改變」(Action<XmlElement>),以改變的列表被生成的XML映射時適用。不幸的是,該字段爲private,並且沒有財產訪問更改列表,所以恐怕目前沒有辦法檢查是否已應用WithLengthOf地圖。

+0

想法,可能是這筆交易;我不介意爲字符串長度組添加約定,但許多情況都是由該類驅動的一次性事件。因此,如果EmployeeNumber字符串長度應該爲6個字符,則它可以是Employee字段串長度常量,或者是DefaultStringLengthConvention中的if語句。 Thx快速響應。 – Berryl 2009-04-30 17:04:30

1

直到有更好的選擇出現,您可以使用HasAttribute("length")

+0

這將足夠好,但它不起作用。我幾個星期沒有更新FNH - 是最近的更新嗎?該屬性可能是「長度」以外的東西嗎? 感謝您的回覆。 – Berryl 2009-05-02 03:25:57