在 MVVM WPF 中使用工作单元设计模式/NHibernate 会话 [英] Using Unit of Work design pattern / NHibernate Sessions in an MVVM WPF

查看:23
本文介绍了在 MVVM WPF 中使用工作单元设计模式/NHibernate 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我陷入了分析的瘫痪状态.请帮忙!

I think I am stuck in the paralysis of analysis. Please help!

我目前有一个项目

在我的案例中,工作单元实现一次支持一个 NHibernate 会话.我当时认为这是有道理的;它从 ViewModel 中隐藏了 NHibernate 会话的内部工作.

Unit of Work implementation in my case supports one NHibernate session at a time. I thought at the time that this makes sense; it hides inner workings of NHibernate session from ViewModel.

现在,根据 Oren Eini (Ayende) 的说法:http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

Now, according to Oren Eini (Ayende): http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

他说服观众在处理与演示者/视图模型关联的视图时应该创建/处理 NHibernate 会话.他提出了为什么您不希望每个 Windows 应用程序有一个会话,也不希望为每个事务创建/处置会话的问题.不幸的是,这带来了一个问题,因为我的 UI 可以轻松地在应用程序中存在 10 多个视图/视图模型.他正在使用 MVP 策略进行展示,但他的建议是否转化为 MVVM?

He convinces the audience that NHibernate sessions should be created / disposed when the view associated with the presenter / viewmodel is disposed. He presents issues why you don't want one session per windows app, nor do you want a session to be created / disposed per transaction. This unfortunately poses a problem because my UI can easily have 10+ view/viewmodels present in an app. He is presenting using a MVP strategy, but does his advice translate to MVVM?

这是否意味着我应该废弃工作单元并让 viewmodel 直接创建 NHibernate 会话?WPF 应用程序一次应该只有一个工作会话吗?如果这是真的,我应该什么时候创建/处理 NHibernate 会话?

Does this mean that I should scrap the unit of work and have viewmodel create NHibernate sessions directly? Should a WPF app only have one working session at a time? If that is true, when should I create / dispose a NHibernate session?

而且我还没有考虑 NHibernate 无状态会话如何适应所有这些!我的大脑要爆炸了.请帮忙!

And I still haven't considered how NHibernate Stateless sessions fit into all this! My brain is going to explode. Please help!

更新:

我在 Rhino Tools 中找到了 Ayende 的工作单元实现.我发现他的实现与我所做的存在显着差异.他绝对支持多个会话.经过进一步研究,我认为最好执行以下操作:

I found Ayende's Unit of Work implementation in Rhino Tools. I discovered that there are significant differences between his implementation and the one I did. His definitely supported multiple sessions. After further research I think it is best that I do the following:

  • 废弃我的工作单元实现
  • 放弃直接从视图模型使用 NHibernate 的 ISession 和 IStatelessSession 对象.虽然在我看来它并不理想,但我已经在工作单元上花费了太多时间,但它并没有达到现在的状态.在某个时候必须应用 KISS 和 YAGNI.我至少可以从 Ayende 的文章和其他一些文章中指出,直接使用这些文章是可以的.
  • 如果我真的不想暴露 ISession,我可以随时使用 Castle.ActiveRecord,但我认为这没有必要.
  • 我可以重用会话工厂代码,因此工作单元实现并不完全是浪费.
  • 重构我的存储库以允许注入 StatelessSession 和 Session,并在可用时使用无状态:否则使用常规会话.

毕竟,然后我可以应用每个视图模型打开一个会话/无状态会话的策略,并在处理视图时,让视图模型刷新/处理会话/无状态会话.

听起来像是一个计划?

推荐答案

我知道这是很久以前的事,但我已经在网上寻找了 3 天的体面答案.我阅读了您提到的两篇博客,其中有一篇关于 Nhibernate 3.0 食谱的文章,其中他们还讨论了 MVP 应用程序中的 Nhibernate,但这并不完全符合我的 MVVM 上下文与存储库和使用 Ninject 进行 IoC 的情况.

I know this dates from a while ago but I've been looking online for a decent answer for 3 days. I read the two blogs you mention had a look at the Nhibernate 3.0 cookbook where they also talk about Nhibernate in a MVP application but this didn't fit exactly in my MVVM context with repositories and using Ninject for IoC.

我发现了这个旧帖子,它是迄今为止最有用的页面:http://www.emidee.net/index.php/2010/08/23/ninject-use-one-database-session-per-view-model

I've found this old post which as so far been the most useful page: http://www.emidee.net/index.php/2010/08/23/ninject-use-one-database-session-per-view-model

我希望这可以帮助将来偶然发现这个问题的任何人.

I hope this helps anyone who will stumble upon this question in the future.

这篇关于在 MVVM WPF 中使用工作单元设计模式/NHibernate 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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