I am trying to come up with a general pattern I can use to provide ORP Framework services to my business logic layer as well as trying to implement thread-safe custom NHibernate powered Providers for ASP.NET 2.0's new provider model (Membership Provider, Profile Provider etc)
As I am not a high end super coder I decided to do the following and was wondering what you guys think of this and if you could give me some advice.
Code:
namespace FusionRecruit.OrpLayer
{
public interface IOrpFrameworkProvider
{
ISessionFactory GetSessionFactory();
}
public class NHibernateOrpProvider : IOrpFrameworkProvider
{
private static ISessionFactory sessionFactory = null;
#region IOrpFrameworkProvider Members
public ISessionFactory GetSessionFactory()
{
// Running an ASP.NET Application?
if (System.Web.HttpContext.Current != null)
{
if (System.Web.HttpContext.Current.Application["HibernateSessionFactory"] == null)
{
System.Web.HttpContext.Current.Application["HibernateSessionFactory"] = BuildHibernateSessionFactory();
}
return (ISessionFactory)System.Web.HttpContext.Current.Application["HibernateSessionFactory"];
}
else
{
if (sessionFactory == null)
{
sessionFactory = BuildHibernateSessionFactory();
}
return sessionFactory;
}
}
#endregion
private static ISessionFactory BuildHibernateSessionFactory()
{
// Tell the configuration where our assembly is
Configuration cfg = new Configuration();
cfg.AddAssembly("FusionRecruit.Pocos");
ISessionFactory factory = cfg.BuildSessionFactory();
return factory;
}
}
public abstract class OrpProviderFactory
{
public static IOrpFrameworkProvider GetOrpProvider()
{
return new NHibernateOrpProvider();
}
}
}
I am trying to encapsulate NHibernate through an interface IOrpFrameworlProvider which will provide object relational persistence services to my business logic layer and the ASP.NET custom Providers I am writing.
With the custom providers MS insist that they must be thread safe, and I was wondering what an impact this would have on my design, in a typical method of my custom MembershipProvider class I have something like this:
Code:
IOrpFrameworkProvider orpProvider = OrpProviderFactory.GetOrpProvider();
ISessionFactory orpSessionFactory = orpProvider.GetSessionFactory();
ISession orpSession = orpSessionFactory.OpenSession();
IQuery membershipQuery = orpSession.CreateQuery("from FusionRecruit.AspNetProviders.Pocos.AspNetMembership as aspNetMembership where aspNetMembership.User.UserId ='" + uid + "'");
AspNetMembership membershipCheck = (AspNetMembership)membershipQuery.UniqueResult();
if (membershipCheck != null && ((int)membershipCheck.User.UserId) != 0)
{
status = MembershipCreateStatus.DuplicateUserName;
return null;
}
orpSession.Close();
I ussually use a session across a few queries and operations, not sure if this is advisable but I thought it might be best to keep a session open for a series of business logic where I might request a few lists from the db, single objects or just values, rather than openeing a session and closing sessions for every operation.
Anyway, if anyone can point out where my code should be thread safe I would much appreciate it, I am very new to threading and thread-safety so its a big learning curve for me!
regards
Fluxtah