迭代 IPersistentCollection 项目

发布于 2024-09-14 09:11:48 字数 527 浏览 5 评论 0原文

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

我想迭代 @event.Collection 元素。

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

此处已提出问题: http://osdir.com/ml/nhuusers /2010-02/msg00472.html,但没有确凿的答案。

I am listening to audit events in NHibernate, specifically to OnPostUpdateCollection(PostCollectionUpdateEvent @event)

I want to iterate through the @event.Collection elements.

The @event.Collection is an IPersistenCollection which does not implements IEnumerable. There is the Entries method that returns an IEnumerable, but it requires an ICollectionPersister which I have no idea where I can get one.

The questions is already asked here: http://osdir.com/ml/nhusers/2010-02/msg00472.html, but there was no conclusive answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-09-21 09:11:48

Pedro,

搜索 NHibernate 代码我可以找到以下关于 IPercientCollection (@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();

这样,我们可以得出结论,您可以将集合转换为 IEnumerable,并且一切都会正常工作。

我已经构建了一个映射包的小示例,事情就在这里:

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

希望这会有所帮助!

菲利佩

Pedro,

Searching NHibernate code I could found the following doc about GetValue method of IPersistentCollection (@event.Collection):

/// <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();

With that, we can conclude that you can cast your collection to an IEnumerable and things will work fine.

I've built a little sample mapping a bag and things got like that over here:

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

Hope this helps!

Filipe

小嗷兮 2024-09-21 09:11:48

如果您需要对集合执行更复杂的操作,您可能需要集合持久化器,您实际上可以通过以下扩展方法获得它(本质上,您需要通过 AbstractCollectionEvent 来解决可见性问题)。 GetLoadedCollectionPersister 方法):

public static class CollectionEventExtensions
{
    private class Helper : AbstractCollectionEvent
    {
        public Helper(ICollectionPersister collectionPersister, IPersistentCollection collection, IEventSource source, object affectedOwner, object affectedOwnerId)
            : base(collectionPersister, collection, source, affectedOwner, affectedOwnerId)
        {
        }

        public static ICollectionPersister GetCollectionPersister(AbstractCollectionEvent collectionEvent)
        {
            return GetLoadedCollectionPersister(collectionEvent.Collection, collectionEvent.Session);
        }
    }

    public static ICollectionPersister GetCollectionPersister(this AbstractCollectionEvent collectionEvent)
    {
        return Helper.GetCollectionPersister(collectionEvent);
    }
}

希望有帮助!

最好的问候,
奥利弗·哈纳皮

If you need to do more complex operations with the collection, you are probably going to need the collection persister, which you can actually get with the following extension method (essentially, you need to work around the visibility by of the AbstractCollectionEvent.GetLoadedCollectionPersister method):

public static class CollectionEventExtensions
{
    private class Helper : AbstractCollectionEvent
    {
        public Helper(ICollectionPersister collectionPersister, IPersistentCollection collection, IEventSource source, object affectedOwner, object affectedOwnerId)
            : base(collectionPersister, collection, source, affectedOwner, affectedOwnerId)
        {
        }

        public static ICollectionPersister GetCollectionPersister(AbstractCollectionEvent collectionEvent)
        {
            return GetLoadedCollectionPersister(collectionEvent.Collection, collectionEvent.Session);
        }
    }

    public static ICollectionPersister GetCollectionPersister(this AbstractCollectionEvent collectionEvent)
    {
        return Helper.GetCollectionPersister(collectionEvent);
    }
}

Hope it helps!

Best Regards,
Oliver Hanappi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文