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

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

问题描述

我认为我陷入了分析的瘫痪.请帮忙!

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

我目前有一个项目

  • 在SQLite上使用NHibernate
  • 实施存储库和工作单元模式:工作单元实现一次支持一个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

    他说服听众说,当与presenter/viewmodel关联的视图被释放时,应该创建/释放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:

    • 取消我对工作单元的实施
    • 直接从viewmodel中退出使用NHibernate的ISession和IStatelessSession对象.虽然我认为这并不理想,但我已经在工作单元上花费了太多时间,而且还没有达到预期的效果.必须在某个时候应用KISS和YAGNI.我至少可以安慰一下,因为Ayende的文章和其他一些文章指出,直接使用它们是可以的.
    • 如果我真的不想公开ISession,则可以始终使用Castle.ActiveRecord,但是我认为这不是必需的.
    • 我可以重用会话工厂代码,因此工作单元的实现不会造成浪费.
    • 重构了我的存储库,以允许同时注入StatelessSession和Session,并在可用的情况下使用无状态:否则,请使用常规会话.

    毕竟,我可以应用为每个视图模型打开一个会话/无状态会话的策略,并且在放置视图时,请刷新视图模型/配置会话/无状态会话.

    听起来像计划吗?

    推荐答案

    我知道这是从不久前开始的,但是我一直在网上寻找一个不错的答案已有3天了. 我读了您提到的两个博客,其中介绍了Nhibernate 3.0食谱,他们在MVP应用程序中也谈到了Nhibernate,但这与我的MVVM上下文(带有存储库和Ninject for 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天全站免登陆