Skip to content

Instantly share code, notes, and snippets.

@nicbell
Last active December 14, 2015 12:58
Show Gist options
  • Save nicbell/5089888 to your computer and use it in GitHub Desktop.
Save nicbell/5089888 to your computer and use it in GitHub Desktop.
NHibernate Session Manager with SysCache
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Caches.SysCache2;
using NHibernate.Context;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Web;
namespace Ixoxo.NHib
{
/// <summary>
/// Manages the NHibernate session, please note SysCache2 has been added as the L2 cache provider.
/// </summary>
public class NHibernateSessionManager
{
private static ISessionFactory Factory { get; set; }
public static string ConnectionString { get; set; }
static NHibernateSessionManager()
{
ConnectionString = String.Empty;
}
private static ISessionFactory GetFactory<T>() where T : ICurrentSessionContext
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
#if DEBUG
.ShowSql()
#endif
.ConnectionString(c => c.Is(ConnectionString)))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateSessionManager>())
.Cache(c => c.ProviderClass<SysCacheProvider>().UseQueryCache())
.CurrentSessionContext<T>().BuildSessionFactory();
}
/// <summary>
/// Gets the current session.
/// </summary>
public static ISession GetCurrentSession()
{
if (Factory == null)
Factory = HttpContext.Current != null ? GetFactory<WebSessionContext>() : GetFactory<ThreadStaticSessionContext>();
if (CurrentSessionContext.HasBind(Factory))
return Factory.GetCurrentSession();
var session = Factory.OpenSession();
CurrentSessionContext.Bind(session);
return session;
}
/// <summary>
/// Closes the session.
/// </summary>
public static void CloseSession()
{
if (Factory != null && CurrentSessionContext.HasBind(Factory))
{
var session = CurrentSessionContext.Unbind(Factory);
session.Close();
}
}
/// <summary>
/// Commits the session.
/// </summary>
/// <param name="session">The session.</param>
public static void CommitSession(ISession session)
{
try
{
session.Transaction.Commit();
}
catch (Exception)
{
session.Transaction.Rollback();
throw;
}
}
/// <summary>
/// Creates the database from mapping in this assembly
/// </summary>
public static void CreateSchemaFromMappings()
{
var config = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ShowSql()
.ConnectionString(c => c.Is(ConnectionString)))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateSessionManager>());
new SchemaExport(config.BuildConfiguration()).Create(false, true);
}
}
}
using System.Configuration;
using System.Web;
namespace Ixoxo.NHib
{
public class NHibernateWebSessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
NHibernateSessionManager.ConnectionString = ConfigurationManager.ConnectionStrings["defaultDb"].ConnectionString;
context.EndRequest += (sender, e) => NHibernateSessionManager.CloseSession();
}
public void Dispose()
{
}
}
}
using NHibernate;
using System;
using System.Collections.Generic;
namespace Ixoxo.NHib.Repository
{
/// <summary>
/// Base NHibernate repository.
/// </summary>
/// <typeparam name="T">Type of entity the repository is for.</typeparam>
/// <typeparam name="TID">Type of the ID for the entity.</typeparam>
public abstract class Repository<T, TID> : IRepository<T, TID> where T : class
{
protected Repository()
{
}
protected ISession Session
{
get { return NHibernateSessionManager.GetCurrentSession(); }
}
public virtual T Get(TID id)
{
return Session.Get<T>(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment