using Sleis.Models; using FluentNHibernate.Automapping; using FluentNHibernate.Cfg; using FluentNHibernate.Conventions; using FluentNHibernate.Conventions.Helpers; using log4net; using NHibernate.Tool.hbm2ddl; using NHibernate.Event; using hbn = NHibernate.Cfg; using Spring.Data.NHibernate; using NHibernate.Cfg; using System.Collections; using NHibernate; using System.Web; using System.IO; namespace Sleis.Infrastructure { public class SessionFactoryProvider : LocalSessionFactoryObject { private static readonly ILog _log = LogManager.GetLogger(typeof(SessionFactoryProvider)); private static ISessionFactory _SessionFactory; SessionFactoryProvider() { } public static ISessionFactory SessionFactory { /* 11/3/11 - Refactored to be handled via Spring get { if (null == _fnhSessionFactory) { _log.Info("Configuring Fluently..."); hbn.Configuration conf = new hbn.Configuration().Configure(); _log.Info("Building Session Factory..."); _fnhSessionFactory = Fluently.Configure(conf) .Mappings(m => m.FluentMappings.AddFromAssemblyOf()) .BuildSessionFactory(); } return _fnhSessionFactory; } * */ get { if (_SessionFactory != null) { return _SessionFactory; } else { _SessionFactory = SpringServiceProvider.GetService("NHibernateSessionFactory"); return _SessionFactory; } } } protected override void PostProcessConfiguration(Configuration config) { base.PostProcessConfiguration(config); LoadHbmOverrides(config, CreateDirIfNotExists(HttpContext.Current.Server.MapPath("~/App_Data/Hbm/Overrides"))); Fluently.Configure(config) .Mappings(m => { m.HbmMappings.AddFromAssemblyOf(); m.FluentMappings .AddFromAssemblyOf() .ExportTo(CreateDirIfNotExists(HttpContext.Current.Server.MapPath("~/App_Data/Hbm/Export"))); }) /*.ExposeConfiguration(c=> LoadHbmOverrides(c, CreateDirIfNotExists(HttpContext.Current.Server.MapPath("~/App_Data/Hbm/Overrides")))) * */ .BuildConfiguration(); } protected string CreateDirIfNotExists(string path) { if (!Directory.Exists(path)) Directory.CreateDirectory(path); return path; } protected static void LoadHbmOverrides(Configuration config, string sourceDirectoryPath) { foreach (var f in new DirectoryInfo(sourceDirectoryPath).GetFiles()) { config.AddXmlFile(f.FullName); } } } }