Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created November 28, 2012 21:02
Show Gist options
  • Save randyburden/4164457 to your computer and use it in GitHub Desktop.
Save randyburden/4164457 to your computer and use it in GitHub Desktop.
NHibernate Helper Class
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
/// <summary>
/// NHibernate Helper
/// </summary>
/// <remarks>
/// Because the SessionFactory creation is lazy-loaded, you technically never need to bootstrap NHibernate
/// and instead can just call OpenSession() as it will do it for you the first time you make the call.
/// </remarks>
public static class NHibernateHelper
{
#region Private Fields
private static ISessionFactory _sessionFactory;
#endregion Private Fields
#region Public Properties
/// <summary>
/// Creates <c>ISession</c>s.
/// </summary>
public static ISessionFactory SessionFactory
{
get { return _sessionFactory ?? ( _sessionFactory = CreateSessionFactory() ); }
}
/// <summary>
/// Allows the application to specify properties and mapping documents to be used when creating a <see cref="T:NHibernate.ISessionFactory"/>.
/// </summary>
public static NHibernate.Cfg.Configuration Configuration { get; set; }
#endregion Public Properties
#region Public Methods
/// <summary>
/// Open a new NHibenate Session
/// </summary>
/// <returns>A new ISession</returns>
public static ISession OpenSession()
{
var session = SessionFactory.OpenSession();
return session;
}
/// <summary>
/// Open a new stateless NHibernate Session
/// </summary>
/// <returns>Stateless NHibernate Session</returns>
public static IStatelessSession OpenStatelessSession()
{
var session = SessionFactory.OpenStatelessSession();
return session;
}
#endregion Public Methods
#region Private Methods
private static ISessionFactory CreateSessionFactory()
{
if ( Configuration == null )
{
Configuration = new Configuration();
Configuration.BeforeBindMapping += OnBeforeBindMapping;
// FluentNHibernate Configuration API for configuring NHibernate
Configuration = Fluently.Configure( Configuration )
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString( x => x.FromConnectionStringWithKey( "SomeConnectionStringName" ) )
.UseReflectionOptimizer()
.AdoNetBatchSize( 100 ) )
.ExposeConfiguration(
x =>
{
// Increase the timeout for long running queries
x.SetProperty( "command_timeout", "600" );
// Allows you to have non-virtual and non-public methods in your entities
x.SetProperty( "use_proxy_validator", "false" );
} )
.Mappings( m => m.FluentMappings.AddFromAssemblyOf<SomeFluentMappingClass>() )
.BuildConfiguration();
}
var sessionFactory = Configuration.BuildSessionFactory();
return sessionFactory;
}
private static void OnBeforeBindMapping( object sender, BindMappingEventArgs bindMappingEventArgs )
{
// Force using the fully qualified type name instead of just the class name.
// This will get rid of any duplicate mapping/class name issues.
bindMappingEventArgs.Mapping.autoimport = false;
}
#endregion Private Methods
}
@randyburden
Copy link
Author

This NHibernate Helper class is great for small projects and for use in Integration test projects as there is no bootstrapping necessary as the creation of the ISessionFactory is lazy-loaded. You simply need to call NHibernateHelper.OpenSession() and your off to the races. Simple, short, and to the point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment