5

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.

Anyone can help me? Thank you!

G.Mennuni
  • 51
  • 3
  • Your requirements sound like an ETL process. If that is the case then an ORM like NHibernate probably is not a good fit. – Paul Sasik Apr 01 '11 at 22:56
  • Paul is correct, specifically because ORMs typically perform very slowly for large amounts of data. – Chad Ruppert Apr 01 '11 at 23:02
  • Yes, probably your're right! Maybe NHibernate is not the best way to do my Application. Thank You! – G.Mennuni Apr 01 '11 at 23:03
  • @G.Mennuni: I added a suggestion with a particular technology that you might find useful in your solution. – Paul Sasik Apr 01 '11 at 23:17
  • @All: thank you guys! I've bookmarked all links posted! Now it's time to study! :) – G.Mennuni Apr 01 '11 at 23:29
  • @G.Mennuni: You're welcome, and welcome to Stack Overflow. While you're studying, don't forget to study the SO FAQ! But kudos on a successful first post! ;-) – Paul Sasik Apr 01 '11 at 23:53

3 Answers3

2

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.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • Yes I would strongly agree with this. SSIS will be much better suited to the job, and will allow for ease of configuration changes as the XLS file format requirements change, without the need to recompile and deploy the code. – Sean Hunter Apr 01 '11 at 23:47
2

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

Marcote
  • 2,977
  • 1
  • 25
  • 24
0

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.

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221