我在RestorableEnvironment
和IBaselineEntity
个对象之间存在一对多的关系:给定的RestorableEnvironment
将只有一个IBaselineEntity
个对象,但每个IBaselineEntity
{1}}对象可以绑定到0-n RestorableEnvironment
个对象。但是,IBaselineEntity
以两种方式之一实现:通过文件或通过数据库。我的课程(通常)如下:
public interface IBaselineEntity
{
BaselineImage BuildImage();
//Remainder of interface
}
public class BaselineFile : IBaseline
{
//implementation
}
public class BaselineDatabase : IBaseline
{
//implementation
}
public class RestorableEnvironment
{
public IBaselineEntity BaselineEntity { get; set; }
//Remainder of class
}
NHibernate需要在references语句中具体实现IBaselineEntity
。为了解决这个问题,我已将RestorableEnvironment
更新为:
public class RestorableEnvironment
{
public IBaselineEntity BaselineEntity
{
get { return BaselineDatabase ?? BaselineFile; }
set
{
BaselineFile = value as BaselineFile;
BaselineDatabase = value as BaselineDatabase;
}
}
private BaselineFile _baselineFile;
public BaselineFile BaselineFile
{
get { return _baselineFile; }
protected set
{
_baselineFile = value;
if (value != null)
BaselineDatabase = null;
}
}
private BaselineDatabase _baselineDatabase;
public BaselineDatabase BaselineDatabase
{
get { return _baselineDatabase; }
protected set
{
_baselineDatabase= value;
if (value != null)
BaselineFile = null;
}
}
// Remainder of class
}
现在我有了具体的类,我现在可以在NHibernate中映射,但这感觉就像一个黑客。有任何改进建议吗?
答案 0 :(得分:1)
将其映射为任何参考
使用流畅的地图
// ClassMap<RestorableEnvironment>
RestorableEnvironmentMap()
{
ReferenceAny(e => e.BaselineEntity)
.EntityIdentifierColumn("entirtyid")
.EntityTypeColumn("entitytype")
.IdentityType<int>()
.MetaType<string>()
.AddMetaValue<E1>("e1")
.AddMetaValue<E2>("e2");
}