开发者

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. I've tried to search some examples to how use and manage NHibernate session; some of them are useful for Web Application. 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.开发者_Python百科

Anyone can help me? Thank you!


Per the feedback I'll suggest that you look into using SSIS for this kind of work. 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. Though if you want, it's not hard to write .NET code run SSIS packages as necessary. Here's an example. Beware though that SSIS APIs still often carry DTS prefixes. DTS (Data Transformation Services) is the precursor to SSIS (SQL Server Integration Services) and much of the technology is reused.


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:

I highly recommend this lecture: http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

Ayende talks about most of the issues about session handling in non-web scenarios. 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.

Goods new this is not the only approach. Fabio Maulo and a very smart guy named Gustavo Ringel came up with this:

http://fabiomaulo.blogspot.com/2009/01/aspect-conversation-per.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.

HTH


For a start I wouldn't recommend NHibernate for this scenario - imports/exports and multiple data stores are not really it's thing.

That said... Web applications generally create the NHibernate Session per page request (e.g. in the Session Start event, or as an action filter). The session factory is usually created in the Application Start though.

For a windows forms application you might want to take a look at the 'unit of work' pattern. Your session would probably want to follow this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜