2010-07-13 96 views
1

假設您有一組適用於特定映射組的Fluent約定,但不適用於所有這些約定。使用自定義C#屬性選擇流利約定

我的想法是,我將創建可應用於Fluent * Map類的自定義C#屬性,並編寫通過檢查* Map類來確定是否應用了自定義屬性來確定接受的約定。

這樣的話,我可以選擇約定的羣體,並通過只是一個自定義屬性標記它們將它們應用到各種映射 - [UseShortNamingConvention]等

我是新來的NHibernate(和流利,和C#就此而言) - 這種方法可行嗎?

它是否理智? :-)

謝謝!

回答

2

是的!我實際上做了一些類似的事情,但使用標記接口(INotCacheable,IComponent),但標記接口或屬性,應該沒有太大區別。

當你的應用約定,只是檢查你的屬性的存在和UR不錯:)

編輯:

添加一些代碼樣本

public class MyMappingConventions : IReferenceConvention, IClassConvention 
    { 
     public void Apply(IOneToManyCollectionInstance instance) 
     { 
      instance.Key.Column(instance.EntityType.Name + "ID"); 
      instance.LazyLoad(); 
      instance.Inverse(); 
      instance.Cascade.SaveUpdate(); 

      if ((typeof(INotCacheable).IsAssignableFrom(instance.Relationship.Class.GetUnderlyingSystemType()))) 
       return; 

      instance.Cache.ReadWrite(); 
     } 

     public void Apply(IClassInstance instance) 
     { 
      instance.Table(instance.EntityType.Name + "s"); 
      //If inheriting from IIMutable make it readonly 
      if ((typeof(IImmutable).IsAssignableFrom(instance.EntityType))) 
       instance.ReadOnly(); 

      //If not cacheable 
      if ((typeof(INotCacheable).IsAssignableFrom(instance.EntityType))) 
       return; 

      instance.Cache.ReadWrite(); 
     } 
} 
+0

你知道怎麼樣? ... instance.EntityType會給我被映射的實體的類型 - 是否有一個屬性,會給我的EntityMapType? – 2010-07-14 13:09:53

+0

添加代碼示例 – 2010-07-15 06:39:41