Skip to content

Instantly share code, notes, and snippets.

@tucaz
Created February 10, 2010 21:41
Show Gist options
  • Save tucaz/300882 to your computer and use it in GitHub Desktop.
Save tucaz/300882 to your computer and use it in GitHub Desktop.
public class NHibernateHelper
{
private static string _databaseConnectionString;
private static string DatabaseConnectionString
{
get
{
if (String.IsNullOrEmpty(_databaseConnectionString))
{
_databaseConnectionString = ConfigurationUtil.GetDeploymentValue("ConnectionStringProfile");
}
return _databaseConnectionString;
}
}
private static string _databaseProvider;
private static string DatabaseProvider
{
get
{
if (String.IsNullOrEmpty(_databaseProvider))
{
_databaseProvider = ConfigurationUtil.GetDeploymentValue("databaseProvider");
}
return _databaseProvider;
}
}
private static Configuration Config;
#region NHibernate Session Factory
private static ISessionFactory _factorySession;
private static ISessionFactory FactorySession
{
get
{
if (_factorySession == null)
{
_factorySession = CreateSessionFactory();
}
return _factorySession;
}
}
public static ISessionFactory CreateSessionFactory()
{
if(DatabaseProvider.ToUpper() == "SQLSERVER")
{
Assembly mappingsAssemly = Assembly.GetExecutingAssembly();
return Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(DatabaseConnectionString)
.ProxyFactoryFactory(typeof(ProxyFactoryFactory))
)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>())
.ExposeConfiguration(cfg =>
{
cfg.SetProperty("default-cascade", "none");
})
.BuildSessionFactory();
}
else if (DatabaseProvider.ToUpper() == "SQLITE")
{
log4net.Config.XmlConfigurator.Configure();
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard.InMemory()
.ShowSql()
.ProxyFactoryFactory(typeof(ProxyFactoryFactory))
)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>()
.ExportTo(@"C:\"))
.ExposeConfiguration(cfg =>
{
cfg.SetProperty("default-cascade", "none");
Config = cfg;
})
.BuildSessionFactory();
}
else{
throw new ArgumentException("Database provider invalid. SqlServer or SqLite");
}
}
#endregion
#region NHibernate Session
private static ISession _session;
public static ISession CreateSession()
{
//When running tests I don´t have the HttpContext
//but I need to return de ISession anyway
if (HttpContext.Current == null)
{
_session = FactorySession.OpenSession();
//If using Sqlite then I need to create the schema
if( DatabaseProvider.ToUpper().Equals("SQLITE"))
{
var schema = new SchemaExport(Config);
schema.Execute(false, true, false, _session.Connection, null);
}
}
else
{
if (HttpContext.Current.Items["Session"] != null)
{
_session = (ISession)HttpContext.Current.Items["Session"];
}
else
{
_session = FactorySession.OpenSession();
HttpContext.Current.Items["Session"] = _session;
}
}
return _session;
}
public static void BeginTransaction()
{
_session.BeginTransaction();
}
public static void CommitTransaction()
{
_session.Transaction.Commit();
}
public static void RollBackTransaction()
{
_session.Transaction.Rollback();
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment