[翻译]  Do Fluent conventions break lazy loading? (uNhAddIns)

[CHINESE]  Fluent惯例是否会破坏延迟加载? (uNhAddIns)


I have a simple entity class in a WPF application that essentially looks like this:

我在WPF应用程序中有一个简单的实体类,基本上如下所示:

public class Customer : MyBaseEntityClass
{
    private IList<Order> _Orders;
    public virtual IList<Order> Orders
    {
        get { return this._Orders; }
        set {this._Orders = new ObservableCollection<Order>(value);}
    }
}

I'm also using the Fluent automapper in an offline utility to create an NHibernate config file which is then loaded at runtime. This all works fine but there's an obvious performance hit due to the fact that I'm not passing the original collection back to NHibernate, so I'm trying to add a convention to get NHibernate to create the collection for me:

我还在离线实用程序中使用Fluent自动化程序来创建NHibernate配置文件,然后在运行时加载该文件。这一切都运行良好,但由于我没有将原始集合传递回NHibernate,因此有明显的性能损失,所以我试图添加一个约定来让NHibernate为我创建集合:

public class ObservableListConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        Type collectionType =
            typeof(uNhAddIns.WPF.Collections.Types.ObservableListType<>)
            .MakeGenericType(instance.ChildType);
        instance.CollectionType(collectionType);
    }
}

As you can see I'm using one of the uNhAddIns collections which I understand is supposed to provide support for both the convention and INotification changes, but for some reason doing this seems to break lazy-loading. If I load a custom record like this...

正如您所看到的,我正在使用其中一个uNhAddIns集合,我理解它应该为约定和INotification更改提供支持,但出于某种原因,这样做似乎打破了延迟加载。如果我加载这样的自定义记录......

var result = this.Session.Get<Customer>(id);

...then the Orders field does get assigned an instance of type PersistentObservableGenericList but its EntityId and EntityName fields are null, and attempting to expand the orders results in the dreaded "illegal access to loading collection" message.

...然后Orders字段确实被分配了PersistentObservableGenericList类型的实例,但其EntityId和EntityName字段为null,并且尝试扩展订单导致可怕的“非法访问加载集合”消息。

Can anyone tell me what I'm doing wrong and/or what I need to do to get this to work? Am I correct is assuming that the original proxy object (which normally contains the Customer ID needed to lazy-load the Orders member) is being replaced by the uNhAddIns collection item which isn't tracking the correct object?

任何人都能告诉我我做错了什么和/或我需要做些什么才能让它发挥作用?我是否正确假设原始代理对象(通常包含延迟加载Orders成员所需的客户ID)正在被未跟踪正确对象的uNhAddIns集合项替换?

UPDATE: I have created a test project demonstrating this issue, it doesn't reference the uNhAddins project directly but the collection classes have been added manually. It should be pretty straightforward how it works but basically it creates a database from the domain, adds a record with a child list and then tries to load it back into another session using the collection class as the implementation for the child list. An assert is thrown due to lazy-loading failing.

更新:我创建了一个演示此问题的测试项目,它没有直接引用uNhAddins项目,但是手动添加了集合类。它应该非常直接如何工作,但基本上它从域创建一个数据库,添加一个带有子列表的记录,然后尝试使用集合类作为子列表的实现将其加载回另一个会话。由于延迟加载失败而抛出断言。

1 个解决方案

#1


1  

I FINALLY figured out the answer to this myself...the problem was due to my use of ObservableListType. In NHibernate semantics a list is an ordered collection of entities, if you want to use something for IList then you want an unordered collection i.e. a Bag.

我最终自己想出了答案......问题是由于我使用了ObservableListType。在NHibernate语义中,列表是实体的有序集合,如果您想为IList使用某些东西,那么您需要一个无序集合,即Bag。

The Eureka moment for me came after reading the answer to another StackOverflow question about this topic.

在阅读关于此主题的另一个StackOverflow问题的答案之后,我找到了尤里卡时刻。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号