Hibernate Interceptor

时间:2013-02-20 03:24:02

标签: c# hibernate onload interceptor

onload只带来实体的id,其他属性为null。

我需要验证实体是否采样,具体取决于IAccount的某些属性的值。到目前为止,这是我的代码:

public bool OnLoad(object entity, object id, System.Collections.IDictionary state)
{
    IAccount account = (IAccount)entity;
    account.xxxxxx        
    return true;
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

OnLoad在实体对象实际初始化之前发生,因此“实体”将具有默认属性值,如您所见。评估或改变实体状态的方式是通过传入的“状态”。

您的示例对于您要评估的内容并不十分具体,但如果您的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

相关问题