Created
November 30, 2011 14:07
-
-
Save mdnmdn/1409182 to your computer and use it in GitHub Desktop.
NHibernate Base Context class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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