using System;
using NHibernate;
using NHibernate.Cfg;
using System.Web;
using NetPuan.BDO;
namespace NetPuan.DAO
{
public class NHibernateHelper {
public static readonly ISessionFactory SessionFactory;
static NHibernateHelper() {
try
{
log4net.Config.XmlConfigurator.Configure();
Configuration cfg = new Configuration();
NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true; // Enable validation (optional)
SessionFactory = cfg.Configure().BuildSessionFactory();
using( System.IO.MemoryStream stream =
NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.Load("NetPuan.BDO")) )
cfg.AddInputStream(stream); // Send the mapping information to NHibernate configuration
// Create the table in the database for the entity Message
System.Console.Out.WriteLine("Creating the table in the database for the entity Message...");
new NHibernate.Tool.hbm2ddl.SchemaExport(cfg).Create(true, true);
} catch (Exception ex) {
Console.Error.WriteLine(ex);
throw new Exception("NHibernate initialization failed", ex);
}
}
public static ISession OpenSession() {
return SessionFactory.OpenSession();
}
public static ISession GetCurrentSession()
{
return (ISession) HttpContext.Current.Items["NHIBERNATE_SESSION"];
}
}
}