First of all, hello to all. I'm a newcomer to the forum and a relative newcomer to NHibernate.
I've been working on a project lately which is using NHibernate in its default DAL implementation. The DAL is composed of a set of DAOs which encapsulate high-level data access, but these DAOs (Which we call persistence managers) are independant of NHibernate. To give an example, I've got a base interface which all other persistence managers implement. Each of my entities has a persistence manager, which is described by another interface that inherits from the base interface. The business layer is coded around these interfaces, and a factory is used to get a certain implementation. As I said before, the default implementation uses NHibernate. Here's a simplified example (The real DAOs implement high-level data access, not just CRUD operations):
My base interface is IPersistenceManager<T, ID> where T is the entity type, ID is the identifier type
Code:
IUserPersistenceManager : IPersistenceManager<User, string>
{
User GetById(string username);
IList<User> GetAll();
void Persist(User user);
void Delete(User user);
}
I also have an NHibernateUserPersistenceManager class which implements the IUserPersistenceManager interface and is the default implementation.
Right now I'm working on an ASP.Net REST service, which receives certain parameters and returns the details of an entity in Xml. I'm stuck between two options and here is where I could use some help. The way my code is structured, here's the necessary code to retrieve a user:
Code:
IPersistenceManagerFactory pmf = new NHibernatePersistenceManagerFactory(SessionManager.Instance.GetSession());
IUserPersistenceManager upm = pmf.CreateUserPersistenceManager();
User user = upm.GetById("whatever");
My first option is to use a simple .aspx page that uses a session per request and constructs the persistence manager factory each time a request is made.
My other option is to use an Http Handler capable of servicing several requests. I'd make the persistence manager factory a member of the http handler, so that it is only instantiated once (and therefore reducing overhead) and make some modifications to properly close and open the sessions. The code for this second option would be something like (The instantiation of the factory takes place in the http handler's constructor):
Code:
IUserPersistenceManager upm = pmf.CreateUserPersistenceManager();
upm.Begin();
User user = upm.GetById("whatever");
upm.End();
Here I use only one session to process all requests. The Begin() method is responsible for reconnecting the session and the End() method is responsible for disconnecting the session. Also, synchronization to the session object would be required.
I'm not sure which would be the better solution. The first one forces me to create a factory for each request I want to service, but the second one needs additional synchronization and two additional methods. It also opens up additional possibilities for other implementations which could require startup and cleanup code controlled by the developer. I also don't know if handling the sessions this way could expose the application to stale data or similar risks.
So, if anyone has any comments, advice, pointers, I'd really appreciate it.
Thanks!