using FluentNHibernate.Automapping; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Cfg; using Tanshu.Accounts.Conventions; using Tanshu.Accounts.Entities; using Tanshu.Accounts.SqlDAO; namespace Tanshu.Accounts.Repository { public sealed class SessionManager { private readonly Configuration cfg; private readonly ISessionFactory factory; private SessionManager() { cfg = GetConfiguration(); factory = cfg.BuildSessionFactory(); } //public static SessionManager Instance //{ // get { return Nested.sessionManager; } //} public static ISession Session { get { return Nested.sessionManager.GetSession(); } } public static IStatelessSession StatelessSession { get { return Nested.sessionManager.GetStatelessSession(); } } public static Configuration Configuration { get { return Nested.sessionManager.GetConfiguration(); } } private class Nested { static Nested() { } internal static readonly SessionManager sessionManager = new SessionManager(); } public Configuration GetConfiguration() { var storeConfiguration = new StoreConfiguration(); var persistenceModel = AutoMap.AssemblyOf(storeConfiguration) .Conventions.Setup(c => { c.Add(); c.Add(); c.Add(); c.Add(); //c.Add(); //c.Add(); //c.Add(DefaultLazy.Never()); //c.Add(); }); return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2005.ConnectionString(p => p.FromConnectionStringWithKey("FluentCon"))) .Mappings(m => m.AutoMappings.Add(persistenceModel)) .BuildConfiguration() .SetInterceptor(new NHSQLInterceptor()); } public ISession GetSession() { return factory.OpenSession(); } public IStatelessSession GetStatelessSession() { return factory.OpenStatelessSession(); } } }