2010-12-10 123 views
1

如何使用流利的映射流利NHibernate和與索引映射類

public class AttributeSet : DictionaryBase 
{ 
    private readonly Dictionary<string, object> _cache; 

    public AttributeSet() 
    { 
     _cache = new Dictionary<string, object>(); 
    } 

    public object this[string index] 
    { 
     get 
     { 
      return _cache[index]; 
     } 
     set 
     { 
      _cache[index] = value; 
     } 
    } 
} 



public class Entity 
{ 
    protected Entity() 
    { 
     Attributes = new AttributeSet(); 
    } 

    public virtual int Id { get; set; } 
    public virtual string Label { get; set; } 
    public virtual string Description { get; set; } 
    public virtual DateTime CreatedDate { get; set; } 

    public virtual AttributeSet Attributes { get; set; } 
} 

回答

1

我不認爲有一種方法,以你的索引直接映射我把類的AttributeSet用流利的NHibernate的,但你可以暴露底層字典類型和地圖,而不是。

如果您不想公開字典作爲公共,您可以將其映射爲私有屬性而不是explained here。用於映射字典,see here

一個例子可能是

HasMany<object>(Reveal.Member<AttributeSet>("Cache")) 
    .KeyColumn("AttributeSetId") 
    .AsMap<string>(idx => idx.Column("Index"), elem => elem.Column("Value")) 
    .Access.CamelCaseField(Prefix.Underscore) 

映射在詞典中的對象類型可能是有趣的,我不知道NHibernate的將能直接轉換成底層數據庫類型。