2014-10-12 09:41:45 +00:00
|
|
|
|
using System;
|
|
|
|
|
using NHibernate;
|
|
|
|
|
using NHibernate.Cfg;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
|
|
|
|
using NHibernate.Mapping.ByCode;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using NHibernate.Tool.hbm2ddl;
|
|
|
|
|
using NHibernate.Cfg.MappingSchema;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
|
|
|
|
public sealed class SessionManager
|
|
|
|
|
{
|
|
|
|
|
private readonly Configuration cfg;
|
|
|
|
|
private readonly ISessionFactory factory;
|
|
|
|
|
|
|
|
|
|
private static SessionManager instance;
|
|
|
|
|
private SessionManager()
|
|
|
|
|
{
|
|
|
|
|
cfg = ConfigureNHibernate();
|
|
|
|
|
factory = cfg.BuildSessionFactory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new SessionManager();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static ISession Session
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new SessionManager();
|
|
|
|
|
}
|
|
|
|
|
return instance.GetSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static IStatelessSession StatelessSession
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new SessionManager();
|
|
|
|
|
}
|
|
|
|
|
return instance.GetStatelessSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static Configuration Configuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new SessionManager();
|
|
|
|
|
}
|
|
|
|
|
return instance.GetConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private Configuration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
return cfg;
|
|
|
|
|
}
|
|
|
|
|
private Configuration ConfigureNHibernate()
|
|
|
|
|
{
|
|
|
|
|
var configure = new Configuration();
|
|
|
|
|
configure.SessionFactoryName("BuildIt");
|
|
|
|
|
|
|
|
|
|
configure.DataBaseIntegration(db =>
|
|
|
|
|
{
|
|
|
|
|
db.Dialect<NHibernate.Dialect.MsSql2005Dialect>();
|
|
|
|
|
db.Driver<NHibernate.Driver.SqlClientDriver>();
|
|
|
|
|
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
|
|
|
|
|
db.IsolationLevel = IsolationLevel.ReadCommitted;
|
|
|
|
|
db.ConnectionStringName = "Con";
|
|
|
|
|
db.Timeout = 10;
|
|
|
|
|
// enabled for testing
|
|
|
|
|
//db.LogFormattedSql = true;
|
|
|
|
|
//db.LogSqlInConsole = true;
|
|
|
|
|
//db.AutoCommentSql = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var mapping = GetMappings();
|
|
|
|
|
configure.AddDeserializedMapping(mapping, "NHSchemaTest");
|
2014-11-02 08:03:31 +00:00
|
|
|
|
//SchemaMetadataUpdater.QuoteTableAndColumns(configure);
|
|
|
|
|
configure.SetInterceptor(new VoucherDirty());
|
2014-11-06 10:39:11 +00:00
|
|
|
|
|
|
|
|
|
var timeout = TimeSpan.FromMinutes(10).TotalSeconds;
|
|
|
|
|
configure.SetProperty("command_timeout", timeout.ToString());
|
2014-10-12 09:41:45 +00:00
|
|
|
|
return configure;
|
|
|
|
|
}
|
|
|
|
|
private static HbmMapping GetMappings()
|
|
|
|
|
{
|
|
|
|
|
var mapper = new ModelMapper();
|
|
|
|
|
var entities = new Type[] {
|
|
|
|
|
typeof(VoucherMap),
|
|
|
|
|
typeof(UserMap),
|
|
|
|
|
typeof(CustomerMap),
|
|
|
|
|
typeof(UserGroupMap),
|
|
|
|
|
typeof(KotMap),
|
|
|
|
|
typeof(ReprintMap),
|
|
|
|
|
typeof(InventoryMap),
|
|
|
|
|
typeof(InventoryModifierMap),
|
|
|
|
|
typeof(GroupMap),
|
|
|
|
|
typeof(RoleGroupMap),
|
|
|
|
|
typeof(ProductMap),
|
|
|
|
|
typeof(ModifierMap),
|
|
|
|
|
typeof(ProductGroupModifierMap),
|
|
|
|
|
typeof(RoleMap),
|
|
|
|
|
typeof(ProductGroupMap),
|
|
|
|
|
typeof(PrintLocationMap),
|
|
|
|
|
typeof(TaxMap),
|
|
|
|
|
typeof(VoucherSettlementMap),
|
2016-01-04 05:22:01 +00:00
|
|
|
|
typeof(FoodTableMap),
|
|
|
|
|
typeof(MachineLocationMap),
|
|
|
|
|
typeof(SettingMap)
|
2014-10-12 09:41:45 +00:00
|
|
|
|
};
|
|
|
|
|
mapper.AddMappings(entities);
|
|
|
|
|
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
|
|
|
|
|
return mapping;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ISession GetSession()
|
|
|
|
|
{
|
|
|
|
|
return factory.OpenSession();
|
|
|
|
|
}
|
|
|
|
|
private IStatelessSession GetStatelessSession()
|
|
|
|
|
{
|
|
|
|
|
return factory.OpenStatelessSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|