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

70 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping;
using Tanshu.Accounts.Contracts;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Tanshu.Accounts.Conventions;
using Tanshu.Accounts.Entities;
using NHibernate.Tool.hbm2ddl;
using NHibernate;
using NHibernate.Cfg;
using FluentNHibernate.Conventions.Helpers;
namespace Tanshu.Accounts.SqlDAO
{
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 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(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();
}
}
}