narsil/Tanshu.Accounts.SqlDAO/Fluent/SetupStore.cs

75 lines
2.5 KiB
C#

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<Voucher>(storeConfiguration)
.Conventions.Setup(c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<ClassConvention>();
c.Add<CascadeConvention>();
//c.Add<AllowNullConvention>();
//c.Add<NotNullConvention>();
//c.Add(DefaultLazy.Never());
//c.Add<EnumConvention>();
});
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();
}
}
}