I want to present you my windows forms application scenario:
I want to create an invoice, so I open new window with invoice details. Then I want to add new customer (to the database), that I want to use in my invoice. After entering all the information (including new customer info) I click Save to save my new document.
The question is:
should I do all the work in one NH session? So saving new customer and saving invoice in one unit of work.
Or maybe saving new customer should be done separately? If yes, then if I add new customer and click cancel in invoice details form, then invoice creation is canceled, but customer is still in database.
I use one unit of work for the whole conversation. Maybe I’m wrong.
Yes, use one NHibernate session. Mapping the life time of the session to a single unit of work is generally the easiest way to go.
Don’t confuse sessions with transactions. If you want to rollback both creations if one creation fails, that requires a transaction and has (mostly) nothing to do with the NHibernate session.