Fluent NHibernate and class with indexer mapping
How can I map the class AttributeSet with Fluent NHibernate using a fluent mapping
开发者_JAVA百科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; }
}
I don't think there's a way to map your indexer directly, but you can expose the underlying dictionary type and map that instead.
If you don't want to expose the dictionary as public you can map it as a private property instead as explained here. For mapping a dictionary, see here.
An example might be
HasMany<object>(Reveal.Member<AttributeSet>("Cache"))
.KeyColumn("AttributeSetId")
.AsMap<string>(idx => idx.Column("Index"), elem => elem.Column("Value"))
.Access.CamelCaseField(Prefix.Underscore)
Mapping the object type in the dictionary might be interesting, I don't know if NHibernate will be able to translate it directly to an underlying database type.
精彩评论