Using Unit of Work design pattern / NHibernate Sessions in an MVVM WPF

て烟熏妆下的殇ゞ 提交于 2019-11-28 06:59:57
Mat

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.

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.

Kent Boogaart

What is your actual concern with having 10+ sessions active? Sessions are light-weight objects that can be used for heavy-weight operations. If the session isn't currently doing anything, it's kind of insignificant.

Using UnitOfWork is seriously limiting in a client app, as all lazy-loading breaks. You lose part of what NHibernate is good at. You're also setting yourself up for runtime exceptions, because there is nothing in the NHibernate model to remind you not to use these features. I'd say Ayendes advice is good.

Personally I try to keep sessions open for as little time as possible. The main reason being that we use pessimistic locking on our main aggregate root (we're using domain driven design) so we want to release the lock as quickly as possible. Don't forget that since you're using NHibernate client side you can close your session and when you open a new one you can reconnect your disconnected entities back.

Personally even in an app which had many windows/tabs open at once I'd still use only one session at a time. I would open a fresh one when I need to retrieve data for a view or when changes need to be persisted.

At one point in our current app we implemented multi session support in our Unit Of Work but ended up taking it out when we realized we didn't have any use for it and it just complicated things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!