views:

71

answers:

1

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:

  1. 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?
  2. 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.
    1. How is this done?
    2. Is there not a huge overhead?
    3. Is it somehow tied with the cache(s) that NHibernate has?
    4. What are these caches for?
    5. 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?
    6. How does the built-in dirty tracking detect deleted objects?
+5  A: 

What should the lifetime of my ISession be?

Several questions address this:

Does NHibernate offer some kind of built-in dirty tracking or must I do this myself?

NHibernate has built-in dirty tracking.

How is this done?

In a nutshell, an EntityEntry keeps the state of the entity when it is loaded, then when the session is flushed this loaded state is compared to the actual state. Fields/properties with differing values are marked as dirty. It's actually much more complex than this, but as a user you don't need to know exactly how it works.

Is there not a huge overhead?

No. It's only a concern if you're managing a huge amount of entities in a single session (something you shouldn't do anyway)

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?

It seems that you're confusing dirty tracking, caching and concurrency. Caching and concurrency are highly configurable, these articles explain them very well:

Dirty tracking behavior can be overriden as well, but the default behavior is appropriate for 99% of the cases.

Mauricio Scheffer
What is a "huge" amount of entities?
snicker
@snicker: depends on entity size, but I'd say about 200+. It's very rare to have that many entities in a single session. See http://ayende.com/Blog/archive/2006/10/18/NHibernatePerformanceConcerns.aspx for more information on this.
Mauricio Scheffer
Ayende typically speaks from a webapp perspective, where those 200+ objects may be persisted for N users accessing the app. Do you think it makes a difference, in this case or other desktop applications, where you only have one user? Does it seem absurd for an application to have 5000+ entities loaded and tracked concurrently? for a "large" entity of 1kb, this is still only 5000kb of memory in entities alone..
snicker
I don't have much experience with desktop apps, but yes, 5000 entities in a *single* session sounds pretty absurd to me. A session in WinForms could span only a unit of work so 5000+ entities is **really a lot**. There is really no need to have that many entities in a session instance.
Mauricio Scheffer
A little aclaration: it's a lot to have that many entities in session *if they are intended to be updated* (and therefore flushed). If it's a readonly session there is no problem since there is no flush.
Mauricio Scheffer