NHibernate version: 1.2.1GA
Hello,
I do apologize for asking what is a pretty general O-O concept question, but I have a huge amount of code that depends on this being right and don't want to run into threading issues along the lines of where Fred logs in and sees Bob's account.
Here's my architecture:
DAO -> Processor (Plain old .NET Class) -> ASP.NET (with codebehind)
The DAO calls to a static helper class:
Code:
public static class NHibSupport
{
private static ISessionFactory sessionFactory;
public static ISessionFactory SessionFactory
{
get { return sessionFactory; }
set { sessionFactory = value; }
}
}
The SessionFactory is built once as part of Application_Start.
Every DAO method follows the pattern:
Code:
ISession session = NHibSupport.SessionFactory.OpenSession();
// Method-specific code here
session.Close();
The Processors have methods that perform business tasks (such as LoginUser, CreateProfile, etc). Their only properties are DAOs and other Processors (in the event of interdependencies).
Right now, I am constructing all Processors that an ASP page uses in its constructor. I learned through painful trial and error that the Page object is reconstructed across postbacks, so I construct the Processors in the ASP page's constructor to avoid NullReferenceExceptions (thinking there's got to be a better way to do it, but haven't found one yet).
Code:
public partial class LoginPage : BasePage
{
private ILoginProcessor loginProcessor;
public ILoginProcessor LoginProcessor
{
get { return loginProcessor; }
set { loginProcessor = value; }
}
public LoginPage() : base()
{
// Get the ProcessorFactory from cached AppObjectStore.
ProcessorFactory processorFactory = CacheUtil.GetAppObjectStore(Context.Application).ProcessorFactory;
// Construct all required Processors.
LoginProcessor = processorFactory.GetLoginProcessor();
....
}
}
Based on this current setup, and what I've read about NHibernate, I think this is thread-safe. My understanding is that the ISessionFactory is thread-safe, so that I can get away with only having one static instance that is used by all DAOs, as long as each DAO method opens and closes a new ISession for its execution.
However, I would be extremely grateful if someone with more experience with NHibernate could eyeball this setup and let me know if it appears to be thread-safe.
Thank you very much in advance,
Philip Tenn