I've spent two full days on this, reading the documentation, message boards and blogs. I still can't figure this out, so I'm begging for help, before I have to give up and resort to my old O/R mapper (LLBLGenPro).
The problem I'm having is with sessions and transactions. I just don't understand NHibernate's session/transaction paradigm and how to make
it work with our service facade.
We're using a distributed 2-tier architecture with a service facade. We have an application server with our service instance initialized (WCF). This app server can be console or web; we use the console version for debugging and local testing.
The client is a WinForms app. It has a serviceproxy that calls the real service through WCF bindings.
Now, my limited understanding of NHibernate's sessions is that in a Console app situation, it connects to the app thread. And in our situation where we have a central server handling data access that's not a good thing, as Ayende points out here:
http://www.ayende.com/Blog/archive/2007/02/03/iBatis-better-than-Hibernate-for-GUI-applications.aspx
The key part I drew from this was:
Quote:
Working with Hibernate or NHibernate in a desktop envrionment can be a little more challanging than on the web. The issue is that you need to make sure that your session doesn't live for the duration of the application. The reason is, of course, the session cache. Using a single session for the application would basically be a memory leak, since the objects would never be released.
Because we're using a service facade, we don't do any Lazy Loading of objects. We load everything on the request and shoot a fully baked object graph back to the client. When the client wants to save/update the object, it makes another request to the facade. At that point we expect the object to be saved, unless a Timestamp indicates the object has been changed by another user (optimistic concurrency for us, in this situation).
My reading of NHibernate suggests this sort of behavior is difficult to achieve primarily because of the way NHibernate handles sessions and
maintains object state with its session state. It is basically trying to do all sorts of work to maintain knowledge of object state, when really all we want it to do is act stupid and fetch objects when we ask for them and update objects when we ask for it (and what I'm reading so far says this is a "detached object" scenario. We could do this with regular ADO.Net (and maybe we should) but I like allowing an O/R mapper to map all
the fields so I don't have to iterate through a datareader (I especially
like how NHibernate uses reflection to fill fields; this works well for some
objects we have that provide a lot of readonly data to the client).
Anyway, I guess what I'm asking for is two things:
(A) Can we do what we want to do with NHibernate, or are we better off
using a different technology (we don't have any problems with LLBL and
it's transactions - we don't get tons of errors and exceptions just trying
to do regular fetch and update, like we do with NHibernate, but maybe that's because we don't pass their business objects up to the client).
(B) If so, what would typical syntax look like for this scenario? Because I've tried all sorts of combinations of Flush, Transactions, Disconnecting
and Reconnecting sessions, etc., and I can't get anything to work.
I'm dying for some clear-cut examples and advice here. Everything I've seen so far seems to be centered around asp.net web pages or non-distributed use. I can't find anything on how anyone would use NHibernate in a distributed scenario for something like a Smart Client where data is expected to be disconnected from the database frequently.
Thanks to anyone who responds - appreciate it.