What is the best way to manage sessions in a DDD application?
Most of the sample code that I have seen shows an NHibernate ISession object being created by code in a business layer and then passed to a data provider, which executes a query. The business layer disposes of the ISession object when it's done with the object graph that the ISession supports.
I don't like this approach, because it pollutes the business layer with data access concerns. If I ever need to change my data access approach, I'll have to go back and do a major rewrite of my business layer. I'd prefer to encapsulate all of my data access code in a separate layer, and isolate the business layer from any knowledge of NHibernate.
That creates some pretty obvious problems for session management, when using the 'session per conversation' approach. An ISession will need a lifespan longer than the life of a particular data access method. That is easily accomplished by creating the ISession as a member-level variable of my data access project. But how do I dispose of the session when I no longer need it?
I have been searching for an article or a blog post on this subject, but I am not having any luck so far. Any thoughts or suggestions greatly appreciated.
David Veeneman
Foresight Systems
|