onload只带来实体的id,其他属性为null。
我需要验证实体采样是否有效,这取决于IAccount的某些属性的值。到目前为止,我的代码如下:
public bool OnLoad(object entity, object id, System.Collections.IDictionary state)
{
IAccount account = (IAccount)entity;
account.xxxxxx
return true;
}我该怎么做呢?
发布于 2013-07-10 21:51:25
OnLoad在entity对象实际初始化之前发生,因此"entity“将具有默认属性值,正如您已经看到的。评估或更改实体状态的方式是通过传入的" state“。
您的示例并没有详细说明您试图评估的内容,但假设您希望在IAccount的IsSampling属性为false时执行一些日志记录:
public bool OnLoad(object entity, object id, System.Collections.IDictionary state)
{
var isSampling = state["IsSampling"] as bool?;
if( entity is IAccount && isSampling.HasValue )
{
if( !isSampling )
Log.Write( string.Format( "Sampling for Account with id {0} is not active", id ) );
}
return false;
}另请注意,我返回的是false,以指示实体的状态尚未更改。如果要更改实体的状态,则必须通过传入的状态集合(而不是通过传入的实体对象)执行此操作,并且必须返回true。
可能很难找到涵盖此内容的文档,但这里有一个源码(尽管它有点过时):NHibernate.IInterceptor
https://stackoverflow.com/questions/14971752
复制相似问题