Skip to content

Instantly share code, notes, and snippets.

@mdnmdn
Created November 30, 2011 14:07
Show Gist options
  • Save mdnmdn/1409182 to your computer and use it in GitHub Desktop.
Save mdnmdn/1409182 to your computer and use it in GitHub Desktop.
NHibernate Base Context class
public class NHContext :IDisposable
{
#region init
protected static ISessionFactory _sessionFactory;
public ISessionFactory SessionFactory {
get {
if (_sessionFactory == null) Init();
return _sessionFactory;
}
}
public void Init()
{
if (_session != null) return;
var configuration = new NHibernate.Cfg.Configuration();
configuration.AddAssembly(this.GetType().Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
#endregion
protected ISession _session;
#region session
public ISession CurrenSession {
get {
if (_session == null) {
_session = SessionFactory.OpenSession();
}
return _session;
}
}
#endregion
public IQueryable<Company> Companies {
get { return CurrenSession.Query<Company>(); }
}
#region IDisposable
public void Dispose()
{
if (_session != null) {
_session.Clear();
_session.Dispose();
_session = null;
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment