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

83 lines
2.7 KiB
C#
Raw Normal View History

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 = BuildConfiguration();
factory = cfg.BuildSessionFactory();
}
public static void Initialize()
{
Nested.sessionManager.Init();
}
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();
}
private Configuration GetConfiguration()
{
return cfg;
}
private Configuration BuildConfiguration()
{
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<UniqueConvention>();
c.Add<NotNullConvention>();
c.Add<FormulaConvention>();
c.Add<InverseConvention>();
//c.Add<AllowNullConvention>();
c.Add<EnumConvention>();
});
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(p => p.FromConnectionStringWithKey("FluentCon")))
.Mappings(m => m.AutoMappings.Add(persistenceModel))
.BuildConfiguration()
.SetInterceptor(new NHSQLInterceptor());
}
private void Init()
{ }
private ISession GetSession()
{
return factory.OpenSession();
}
private IStatelessSession GetStatelessSession()
{
return factory.OpenStatelessSession();
}
}
}