NHibernate会议:多少?什么时候创建?何时关闭? [英] NHibernate sessions: How many? When to create? When to close?

查看:87
本文介绍了NHibernate会议:多少?什么时候创建?何时关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的在这里与NHibernate挣扎.我试图使我的DAL程序集非常通用,以便它可以在Web上下文,WCF和WinForms/WPF中工作.我的问题是会话.

Im really struggling with NHibernate here. I'm trying to keep my DAL-assembly very generic, so that it works in a webcontext, in WCF and in WinForms / WPF. My problem here is the sessions.

延迟加载是好的,这是我真正想要的,但是在加载对象所用的会话关闭之后,就无法再进行延迟加载了.事实证明,这对我来说意义重大,因为我不知道应该在哪个范围内开始/结束会议.

Lazy-loading is good, and something I really want, but after the session which you loaded the object with is closed, you can't lazy-load anymore. This turns out to be a huge deal for me, since I don't know in which scope I should start / end my sessions.

我见过很多人在HttpContext.Begin上启动它们,然后在.End上终止它们,但这对我来说是不可行的,因为我不希望我的程序集链接到System.Web或绑定到在HttpContext中被调用.

I've seen a lot of people starting them on HttpContext.Begin and ending them on .End, but this is not feasable for me, since I don't want my assembly to link to System.Web or be bound to be called in a HttpContext.

另一种选择是在整个DAL程序集生命周期中仅保留一个会话.这种方法有哪些利弊?该程序集将主要作为网站的后端工作,但还将为WinForms应用程序做一些工作.

Another option would be to just keep one session for the entire DAL-assembly lifecycle. What are some pros and cons onto this approach? The assembly will mainly work as a backend in a website, but also do some work for a WinForms application.

有没有人? :)

对我来说,另一种选择是编写自己的延迟加载代理集合,这反过来将打开一个新会话并获取该集合应包含的所有数据.对这种方法有何评论?

Another option for me would be to write my own lazy-loading proxy-collections, which in turn would open a new session and fetch whatever data the collection should contain. Any comments on this approach?

推荐答案

在我们的系统中,我们具有工作单元的概念.我们的工作单位是针对系统的单个操作.例如,从一个帐户中取钱或将钱存入一个帐户就是一个单一的工作单元.

In our system we have the concept of a unit of work. A unit of work to us is a single operation against the system. For instance withdrawing money from an account or depositing money into an account would be a single unit of work.

工作单元类包装NHibernate会话(以及其他一些东西),然后我们的存储库针对当前工作单元(通过线程存储)工作.

The unit of work class wraps the NHibernate session (and a few other things) and then our Repositories work against the current unit of work (via thread storage).

根据您要尝试执行的操作,我们将执行以下操作...

In terms of what you're trying to do we would then do the following...

  • 对于WCF,我们具有UnitOfWorkContext属性,该属性适用于负责开始/结束工作单元的操作.

  • For WCF we have a UnitOfWorkContext attribute which we apply to an operation that is responsible for beginning/ending the unit of work.

对于WinForms,我们只需要演示者开始/结束工作单元

For WinForms we would just have the presenter begin/end a unit of work

对于WebContext,我们将在HttpContext.Begin和.End

For WebContext we would do the same in HttpContext.Begin and .End

这篇关于NHibernate会议:多少?什么时候创建?何时关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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