2011-01-31 20:33:22 +00:00
|
|
|
|
using FluentNHibernate.Automapping;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
using FluentNHibernate.Cfg;
|
|
|
|
|
using FluentNHibernate.Cfg.Db;
|
|
|
|
|
using NHibernate;
|
|
|
|
|
using NHibernate.Cfg;
|
2011-01-31 20:33:22 +00:00
|
|
|
|
using Tanshu.Accounts.Conventions;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
|
|
|
|
using Tanshu.Accounts.SqlDAO;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
|
2011-01-31 20:33:22 +00:00
|
|
|
|
namespace Tanshu.Accounts.Repository
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
public sealed class SessionManager
|
|
|
|
|
{
|
|
|
|
|
private readonly Configuration cfg;
|
|
|
|
|
private readonly ISessionFactory factory;
|
|
|
|
|
private SessionManager()
|
|
|
|
|
{
|
2011-02-18 16:54:48 +00:00
|
|
|
|
cfg = BuildConfiguration();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
factory = cfg.BuildSessionFactory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//public static SessionManager Instance
|
|
|
|
|
//{
|
|
|
|
|
// get { return Nested.sessionManager; }
|
|
|
|
|
//}
|
|
|
|
|
public static ISession Session
|
|
|
|
|
{
|
|
|
|
|
get { return Nested.sessionManager.GetSession(); }
|
|
|
|
|
}
|
2011-01-31 20:33:22 +00:00
|
|
|
|
public static IStatelessSession StatelessSession
|
|
|
|
|
{
|
|
|
|
|
get { return Nested.sessionManager.GetStatelessSession(); }
|
|
|
|
|
}
|
2011-01-30 07:14:05 +00:00
|
|
|
|
public static Configuration Configuration
|
|
|
|
|
{
|
|
|
|
|
get { return Nested.sessionManager.GetConfiguration(); }
|
|
|
|
|
}
|
|
|
|
|
private class Nested
|
|
|
|
|
{
|
|
|
|
|
static Nested() { }
|
|
|
|
|
internal static readonly SessionManager sessionManager = new SessionManager();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 16:54:48 +00:00
|
|
|
|
private Configuration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
return cfg;
|
|
|
|
|
}
|
|
|
|
|
private Configuration BuildConfiguration()
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
var storeConfiguration = new StoreConfiguration();
|
|
|
|
|
var persistenceModel = AutoMap.AssemblyOf<Voucher>(storeConfiguration)
|
|
|
|
|
.Conventions.Setup(c =>
|
|
|
|
|
{
|
|
|
|
|
c.Add<PrimaryKeyConvention>();
|
|
|
|
|
c.Add<CustomForeignKeyConvention>();
|
|
|
|
|
c.Add<ClassConvention>();
|
2011-01-31 20:33:22 +00:00
|
|
|
|
c.Add<CascadeConvention>();
|
2011-02-18 16:54:48 +00:00
|
|
|
|
c.Add<UniqueConvention>();
|
|
|
|
|
c.Add<NotNullConvention>();
|
|
|
|
|
c.Add<FormulaConvention>();
|
2011-02-04 19:30:55 +00:00
|
|
|
|
//c.Add<AllowNullConvention>();
|
2011-02-18 16:54:48 +00:00
|
|
|
|
c.Add<EnumConvention>();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Fluently.Configure()
|
|
|
|
|
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(p => p.FromConnectionStringWithKey("FluentCon")))
|
|
|
|
|
.Mappings(m => m.AutoMappings.Add(persistenceModel))
|
|
|
|
|
.BuildConfiguration()
|
|
|
|
|
.SetInterceptor(new NHSQLInterceptor());
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
private ISession GetSession()
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
return factory.OpenSession();
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
private IStatelessSession GetStatelessSession()
|
2011-01-31 20:33:22 +00:00
|
|
|
|
{
|
|
|
|
|
return factory.OpenStatelessSession();
|
|
|
|
|
}
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|