0
好的,所以我需要創建幾個表,除了一個字段外,它們需要幾乎相同。NHibernate - 映射運行時定義類型的屬性
我的模型將大致是這樣的:
class HouseGeometryModel
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
//More fields...
public virtual HouseAttributes Attributes { get; set; }
}
class DungeonGeometryModel
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
//More fields, all identical to HouseGeometryModel...
public virtual DungeonAttributes Attributes { get; set; }
}
class FortressGeometryModel
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
//More fields, all identical to HouseGeometryModel...
public virtual FortressAttributes Attributes { get; set; }
}
//More models...
所以,基本上只有Attributes
財產所有型號之間的區別在這裏,所以我想有可能是來統一一切都變成一個單一的(通用的方式? )類。
我可以想出兩種方式來實現這一點:
做一個泛型類
GeometryModel<TAttributes>
這將是這樣的:class GeometryModel<TAttributes> { public virtual int Id { get; set; } public virtual string Name { get; set; } //More fields... public virtual TAttributes Attributes { get; set; } }
這樣做的問題是,我不能指定流利的映射。映射也應該以這種方式變得通用(實現
ClassMap<GeometryModel<TAttributes>>
),因此不可能用NHibernate實例化它。使
Attributes
財產dynamic
。它不起作用,因爲NHibernate在創建ClassMap<>
時將dynamic
屬性視爲object
。
有沒有解決方法?