What is the basic pattern for using (N)Hibernate?
- by Vilx-
I'm creating a simple Windows Forms application with NHibernate and I'm a bit confused about how I'm supposed to use it. To quote the manual:
ISession (NHibernate.ISession)
A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps an ADO.NET connection. Factory for ITransaction. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
Now, suppose I have the following scenario:
I have a simple classifier which is a MSSQL table with two columns - ID (auto_increment) and Name (nvarchar). To edit this classifier I create a form which contains a single gridview and two buttons - OK and Cancel. The user can nearly directly edit the table in the gridview, and when he hits OK the changes he made are persisted to the DB (or if he hits cancel, nothing happens).
Now, I have several questions about how to organize this:
What should the lifetime of my ISession be?
Should I create a single ISession for my whole application; an ISession for each of my forms (the application is single-threaded MDI); or an ISession for every DB operation/transaction?
Does NHibernate offer some kind of built-in dirty tracking or must I do this myself? The manual mentions something like it here and there but does not go into details.
How is this done?
Is there not a huge overhead?
Is it somehow tied with the cache(s) that NHibernate has?
What are these caches for?
Are they not specific to a single ISession? That is, if I use a seperate ISession for every transaction, won't it break the dirty tracking?
How does the built-in dirty tracking detect deleted objects?