简体   繁体   English

NHibernate在C#应用程序中,如何管理会话

[英]NHibernate in C# application, how to manage session

I'm new in NHibernate world and I'm starting to build a simple C# Windows Form Application that imports some XLS files into a DB (SQL2008), elaborates data and than exports a CSV file. 我是NHibernate世界的新手,我开始构建一个简单的C#Windows窗体应用程序,它将一些XLS文件导入数据库(SQL2008),详细说明数据,然后导出CSV文件。 I've tried to search some examples to how use and manage NHibernate session; 我试图搜索一些如何使用和管理NHibernate会话的例子; some of them are useful for Web Application. 其中一些对Web应用程序很有用。 I've seen that in MVC Application the NHibernate session is created on Application Start, but I can't understand when I must create the NHibernate session into a Windows Form Application. 我已经看到在MVC应用程序中,NHibernate会话是在Application Start上创建的,但我无法理解何时必须在Windows窗体应用程序中创建NHibernate会话。

Anyone can help me? 有人可以帮帮我吗? Thank you! 谢谢!

Per the feedback I'll suggest that you look into using SSIS for this kind of work. 根据反馈,我建议您考虑使用SSIS进行此类工作。 Besides being designed for ETL processes like these SSIS can also be re-executed as needed and there's no need for custom code at all. 除了为这些ETL过程设计之外,SSIS还可以根据需要重新执行,根本不需要自定义代码。 Though if you want, it's not hard to write .NET code run SSIS packages as necessary. 虽然如果你愿意,但是根据需要编写.NET代码运行SSIS软件包并不难。 Here's an example . 这是一个例子 Beware though that SSIS APIs still often carry DTS prefixes. 请注意,SSIS API仍然经常带有DTS前缀。 DTS (Data Transformation Services) is the precursor to SSIS (SQL Server Integration Services) and much of the technology is reused. DTS(数据转换服务)是SSIS(SQL Server Integration Services)的前身,并且大部分技术都被重用。

First of all, I don't think you are using the right tool for the job. 首先,我认为你没有使用正确的工具来完成工作。 But if still want to use NH for learning purposes, these are my advises: 但如果仍然想将NH用于学习目的,这些是我的建议:

I highly recommend this lecture: http://msdn.microsoft.com/en-us/magazine/ee819139.aspx 我强烈推荐这个讲座: http//msdn.microsoft.com/en-us/magazine/ee819139.aspx

Ayende talks about most of the issues about session handling in non-web scenarios. Ayende讨论了关于非Web场景中会话处理的大多数问题。 What we used to do is to follow a pattern-like Model-Per Form. 我们过去做的是遵循类似模式的模型 - 每个表单。 A Model contains a session, but the model lifetime is tied to the life time of the form. 模型包含会话,但模型生命周期与表单的生命周期相关联。 This prevents having one session per-application which a very bad decision, in fact Fabio Maulo (NH Lead) says is like having a time-bomb in your application. 这可以防止每个应用程序有一个会话这是一个非常糟糕的决定,事实上Fabio Maulo(NH Lead)说你的应用程序中有一个定时炸弹。

Goods new this is not the only approach. 货物新的这不是唯一的方法。 Fabio Maulo and a very smart guy named Gustavo Ringel came up with this: Fabio Maulo和一个名叫Gustavo Ringel的聪明人提出了这个问题:

http://fabiomaulo.blogspot.com/2009/01/aspect-conversation-per.html http://fabiomaulo.blogspot.com/2009/01/aspect-conversation-per.html

http://gustavoringel.blogspot.com/2009/02/unhaddins-persistence-conversation-part.html http://gustavoringel.blogspot.com/2009/02/unhaddins-persistence-conversation-part.html

Good news is not all theory, unNHAddins has a fully functional example of this concepts. 好消息不是所有的理论,unNHAddins有一个功能齐全的例子。

HTH HTH

For a start I wouldn't recommend NHibernate for this scenario - imports/exports and multiple data stores are not really it's thing. 首先,我不建议NHibernate用于这种情况 - 导入/导出和多个数据存储并不是真的。

That said... Web applications generally create the NHibernate Session per page request (eg in the Session Start event, or as an action filter). 也就是说...... Web应用程序通常会在每页请求中创建NHibernate会话(例如,在Session Start事件中,或作为动作过滤器)。 The session factory is usually created in the Application Start though. 会话工厂通常在Application Start中创建。

For a windows forms application you might want to take a look at the ' unit of work ' pattern. 对于Windows窗体应用程序,您可能需要查看“ 工作单元 ”模式。 Your session would probably want to follow this. 你的会话可能想要遵循这个。

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

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