Basically what I'm trying to accomplish is to configure the NHibernate session as a disconnected unit-of-work in a two-tier smart client architecture.
The session should only temporarily connect to the database when data is needed from the database and any changes (i.e. the insert/update/delete statements) should only be committed to the database at the end of the unit-of-work.
Now, if I put the session's flushmode to Commit, I can see that the modifications to the persistent objects and even the creation of child objects of a certain persistent object are nicely cached within the session until the session.Commit() is called. I guess what I am experiencing here is the persistence-by-reachability pattern.
But if I want to create a new root object (one that I cannot just add to the collection of a persistent object) I explicitly have to call the session.Save() method for it. This will either generate an insert statement (if the id generator is "identity") or retrieve the sequence's nextval (if the id generator is "sequence"). Even though the latter doesn't actually modify the database, it requires to connect to the database.
I also noticed that removing objects by calling the session.Delete() method also require that the session is connected in some scenarios.
Unfortunately, I have not been able yet to force the session.Save() and session.Delete() methods to defer their sql statement until the commit is called. Therefore I am thinking of subclassing the NHibernate provided session to defer the calls to these methods until the session.Commit() is called.
Does anyone know how to configure NHibernate to provide a custom session?
Or maybe there is an easier way to accomplish what I'm trying to do?
You can find my code here:
http://www.paarden.be/NHibernate/Sol_PaardenConfigurator.zip
Thanks in advance,
Tolomaüs.