NHibernate映射类名并用反射解析
我有一个类的属性,该类映射到另一个不能存储在数据库中且不能序列化的类;它实现了状态模式。 所以我有这样的想法:NHibernate映射类名并用反射解析,nhibernate,iusertype,Nhibernate,Iusertype,我有一个类的属性,该类映射到另一个不能存储在数据库中且不能序列化的类;它实现了状态模式。 所以我有这样的想法: public IState MyState { get; set; } 我有两种不同的状态 public class LockedState : IState ... public class UnlockedState : IState ... 在数据库中,我需要保留当前状态的名称,该名称可以通过以下方式实现,例如: string name = myState.GetType(
public IState MyState { get; set; }
我有两种不同的状态
public class LockedState : IState ...
public class UnlockedState : IState ...
在数据库中,我需要保留当前状态的名称,该名称可以通过以下方式实现,例如:
string name = myState.GetType().Name;
我是否必须编写一个自定义且详细的IUserState,或者是否有其他内容?为此,我必须以以下方式实现自定义IUserType:
public sealed class StateMapper : IUserType
// get
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
string objectName = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
Type stateType = Type.GetType(objectName, false, true);
if (stateType == null)
{
return null;
}
// StateFacility is used by my code to create a new Type
return StateFacility.CreateState(stateType);
}
// set
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
return;
}
NHibernateUtil.String.NullSafeSet(cmd, value, index);
}