NHibernate的,每个ISession的presenter,多个presenters开放 [英] NHibernate, ISession per presenter, multiple presenters open

查看:212
本文介绍了NHibernate的,每个ISession的presenter,多个presenters开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你将如何解决以下情况:应用程序是基于最有价值球员(的WinForms),并在同一时间,我可以有多个presenters显示。每个presenter拥有的presenter一生中连接会话,并打开。

How would you solve the following situation: the app is MVP based (WinForms) and at the same time I can have multiple presenters showing. Each presenter has a session attached, and open during the lifetime of the presenter.

如果我加载在一个presenter一个实体,并通过它传递到另一个表现出来,如果​​该实体具有一定的收藏它,我得到非法企图集合了两个开放式的会议联系起来。

If I load an entity in one presenter and pass it through to another to show it, and if that entity has some collection to it, I get "Illegal attempt to associate a collection with two open sessions".

现在,我是不是做错了什么与我的架构?我没有看到一个问题多发的会议开(连接当然不是打开所有的时间)。

Now, am I doing something wrong with my architecture? I don't see a problem with mutiple sessions open (connections are of course not open all the time).

任何意见,多AP preciated!

Any opinions are much appreciated!

推荐答案

在检索使用NHibernate的数据库中的数据,返回的实体对象获得相关的的ISession 实例化它们。所有涉及的任何对象的数据库,随后的操作都必须通过同样的的ISession 实例来执行。这包括通过延迟加载抓取的实体关联集合。

When you retrieve data from a database using NHibernate, the returned entity objects get associated to the ISession instance that materialized them. All subsequent operations involving the database on any of those objects must be performed through the same ISession instance. This includes fetching associated collections of entities via lazy loading.

要解决这个问题的方法是确保已填充关联到实体对象的任何集合传递到另一个会话之前。有一对夫妇的实现这一目标的方式:

One way to solve this problem is making sure that any collections associated to the entity object are already populated before passing it to another session. There are a couple of ways of achieving this:

  1. 您可以加载集合实体对象已被检索通过简单的实体对象上访问他们的相关性,从而在触发延迟加载
  2. 您可以和相关的收藏品,通过检索实体对象的立即加载
  1. You could load the collections after the entity object has been retrieved by simply accessing their relative properties on the entity object, thus triggering lazy loading.
  2. You could retrieve the entity object together with its associated collections through eager loading.

请注意,在这两种情况下,你应该完全通过调用 ISession.Evict 方法转给另一个人之前分离从本届会议的实体对象。

Note that in both cases you should completely detach the entity object from its session before passing it to another one by calling the ISession.Evict method.

这篇关于NHibernate的,每个ISession的presenter,多个presenters开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆