迭代IPersistentCollection项

Ped*_*dro 1 nhibernate audit

我正在听NHibernate中的审计事件,特别是 OnPostUpdateCollection(PostCollectionUpdateEvent @event)

我想迭代@event.Collection元素.

@ event.Collection是一个IPersistenCollection没有实现的IEnumerable.有一种Entries方法可以返回一个IEnumerable,但它需要一个ICollectionPersister我不知道在哪里可以获得一个.

这里已经提出了这些问题:http://osdir.com/ml/nhusers/2010-02/msg00472.html,但没有确定的答案.

提前致谢

jfn*_*eis 5

佩德罗,

搜索NHibernate代码我可以找到以下关于IPersistentCollection(@ event.Collection)的GetValue方法的文档:

/// <summary>
/// Return the user-visible collection (or array) instance
/// </summary>
/// <returns>
/// By default, the NHibernate wrapper is an acceptable collection for
/// the end user code to work with because it is interface compatible.
/// An NHibernate PersistentList is an IList, an NHibernate PersistentMap is an IDictionary
/// and those are the types user code is expecting.
/// </returns>
object GetValue();
Run Code Online (Sandbox Code Playgroud)

有了它,我们可以得出结论,你可以将你的集合转换为IEnumerable,事情会很好.

我已经建立了一个小样本来映射一个包,这里有类似的东西:

public void OnPostUpdateCollection(PostCollectionUpdateEvent @event)
{
    foreach (var item in (IEnumerable)@event.Collection.GetValue())
    {
        // DO WTVR U NEED
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

菲利佩