using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

using DataAccess.NHDal.Maps;

using NHibernate;

namespace DataAccess.NHDal {
	public class NHibernateSessionProvider {
		private static ISessionFactory sessionFactory;

		public static ISessionFactory SessionFactory {
			get {
				if (sessionFactory == null) {
#if DEBUG
					System.Diagnostics.Trace.WriteLine("NHibernateSessionProvider create session instance");
#endif
					sessionFactory = createSessionFactory();
				}
				return sessionFactory;
			}
		}

		private static ISessionFactory createSessionFactory() {
			return Fluently.Configure()
				.Database(MsSqlConfiguration.MsSql2008
#if DEBUG
					.ShowSql()
#endif
					.ConnectionString(c => c.FromConnectionStringWithKey("HomeDB"))
					)
				.Mappings(m => m.FluentMappings.AddFromAssemblyOf<CategoryMap>())
				.BuildSessionFactory();
		}
	}
}
