简体   繁体   English

两个应用程序,一个数据库,NHibernate

[英]Two applications, one database, NHibernate

I am currently developing two separate applications, one Microsoft WPF application and a Windows Service application. 我目前正在开发两个独立的应用程序,一个Microsoft WPF应用程序和一个Windows Service应用程序。

The Windows Service application is responsible for opening a mailbox on specific time intervals and if there is new mail, create some entries in a SQL Server database corresponding to these mails. Windows Service应用程序负责按特定的时间间隔打开邮箱,如果有新邮件,请在与这些邮件相对应的SQL Server数据库中创建一些条目。 This application uses NHibernate as an ORM against the database. 该应用程序将NHibernate用作针对数据库的ORM。

On the other hand, I have a Microsoft WPF application that lets its users interact with the mails that is created by the Windows Service application. 另一方面,我有一个Microsoft WPF应用程序,它的用户可以与Windows Service应用程序创建的邮件进行交互。 This interaction involves both reading and writing to the same exact database as the Windows Service application is using. 这种交互涉及读取和写入与Windows Service应用程序正在使用的相同的数据库。 The Microsoft WPF application also uses NHibernate as an ORM against the database (the exact same project, repository/unit of work pattern). Microsoft WPF应用程序还将NHibernate用作针对数据库(完全相同的项目,存储库/工作单元)的ORM。

The problem is that when both applications run at the same time (as is the intention and design), the data provided to the respective applications through NHibernate will not be the same. 问题在于,当两个应用程序同时运行时(意图和设计都是如此),通过NHibernate提供给各个应用程序的数据将是不同的。 I assume this is because of caching functionality and that the Microsoft WPF application's NHibernate instance is unaware of the changes that the Windows Service application's NHibernate instance is making to the database (and vice versa). 我认为这是由于缓存功能引起的,并且Microsoft WPF应用程序的NHibernate实例不知道Windows Service应用程序的NHibernate实例对数据库所做的更改(反之亦然)。

Any suggestions on how to remedy this behavior? 关于如何纠正这种行为有什么建议吗? I looked into the IStatelessSession interface but I did not like it that much. 我查看了IStatelessSession接口,但我不太喜欢它。 Is there a way to force NHibernate to refresh its state only at a programmatically specified time? 有没有办法强制NHibernate仅在以编程方式指定的时间刷新其状态?

Thanks for any response! 感谢您的任何回复!

NHibernate will keep track of its objects during the lifetime of a session, unless you manually Evict() entities from the session. NHibernate将在会话的生存期内跟踪其对象,除非您手动从会话中Evict()实体。 Depending on how your application works you could create new short-lived sessions to query the list of database entries and create another session that handles getting and updating individual items. 根据您的应用程序的工作方式,您可以创建新的短期会话来查询数据库条目列表,并创建另一个会话来处理获取和更新单个项目。 Creating a session isn't too expensive (creating the session factory is, but you only need one) and you wouldn't be forced to work around NHibernate's session cache. 创建一个会话并不太昂贵(创建会话工厂很昂贵,但是您只需要一个),并且不会被迫处理NHibernate的会话缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM