(N)Hibernate如何做到这一点? [英] How can this be done with (N)Hibernate?

查看:168
本文介绍了(N)Hibernate如何做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用NHibernate创建一个windows窗体应用程序。这是一个MDI应用程序,所以用户可以同时打开多少个表单(可能很多)没有限制。



对于我想要的大多数表单一个确定和一个取消按钮。两者都关闭表单,但确定也将修改的数据保存到数据库。这些表单可能非常复杂,而且这些修改可能会触及整个对象图形,添加一些对象,删除一些对象,并更改一些对象。如果可以自动检测并根据需要坚持更改,这将是一件好事,无需手动跟踪每个更改。



这将是一种好方法做到这一点?



额外信息:我可以制作任何我想要的数据库模式。我正在使用MSSQL 2008,并且目前已经决定了GUID主键(带有guid.comb生成器)和一个TIMESTAMP列用于乐观并发。



我试图简单地设置 FlushMode 一个NHibernate ISession 从不,做所有修改为如果用户点击确定,然后调用 Flush()但那并不奏效。

解决方案

这应该有助于:使用ASP.NET的1.2版NHibernate最佳实践(我知道它是ASP.NET,但您应该找到有用的信息并且可以轻松转换为WinForms。)简而言之,应该选择每个窗口结构的sesson。无法为相同的确切任务打开两个不同的窗口在这里很有意义。然后,你可能想要在Form_Load()上创建一个NHibernate ISession API的实例。最后,如果用户点击OK,那么只需要BeginTransaction(),Flush()会话以及Commit()事务,否则将它回滚。


I'm creating a windows forms application with NHibernate. It's an MDI application, so there is no limit to how many forms the user can have open at the same time (probably many).

For most forms I want to have an "OK" and a "Cancel" button. Both close the form, but "OK" also saves the modified data to the DB. The forms can be pretty complex, and the modifications are likely to touch a whole graph of objects, adding some, deleting some, and changing some more. It would be good if the changes could be automatically detected and persisted as needed, without the need to manually keep track of each of them.

What would be a good way to do this?

Extra information: I can make whatever DB schema I want. I'm using MSSQL 2008 and currently have decided for GUID primary keys (with guid.comb generator) and a TIMESTAMP column for optimistic concurrency.

I tried to simply set FlushMode of a NHibernate ISession to Never, doing all modifications as needed, and then calling Flush() if the user clicked OK. But that didn't work.

解决方案

This should help: NHibernate Best Practices with ASP.NET, 1.2nd Ed. (I know it's ASP.NET, but you should find the information useful and easily transferable to WinForms.)

In short, one should opt for a sesson per window architecture. Making impossible to open two different windows for the same exact task makes sense here. Then, you perhaps want to create an instance of the NHibernate ISession API on Form_Load(). At the end, if the user clicks OK, then just BeginTransaction(), Flush() the session, and Commit() the transaction, otherwise rolls it back.

这篇关于(N)Hibernate如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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