narsil/Tanshu.Accounts.Repository/SetupStore.cs
tanshu 3ca8b29e04 Regression: BillItemKey added the compare methods back
Regression: PrintLocation added the compare methods back
Breaking: Kot.Code is now integers
Breaking: Kot Update is now via Stored Procedure to get DB Values
Breaking: Reprints Insert is now via Stored Procedure to get DV Values
Breaking: Voucher.BillID and KotID are now integers
Breaking: Voucher Insert/Update is now via Stored Procedures to get DV Values also Dirty Checking for Voucher has been overwritten to set dirty for LastEditDate update
Fix: Login forms simplified
Feature: PrintLocation and Products are cached application wide.
2014-11-02 13:33:31 +05:30

142 lines
4.1 KiB
C#

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");
//SchemaMetadataUpdater.QuoteTableAndColumns(configure);
//configure.SetInterceptor(new NHSQLInterceptor());
configure.SetInterceptor(new VoucherDirty());
return configure;
}
private static HbmMapping GetMappings()
{
var mapper = new ModelMapper();
var entities = new Type[] {
typeof(VoucherMap),
typeof(UserMap),
typeof(WaiterMap),
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),
typeof(FoodTableMap)
};
mapper.AddMappings(entities);
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}
private ISession GetSession()
{
return factory.OpenSession();
}
private IStatelessSession GetStatelessSession()
{
return factory.OpenStatelessSession();
}
}
}