I recently learned about nhib.pdf and found there are two ways to implement it.
1. sessionmanager. CS
View code
Public sealed class sessionmanager
{
Private const string currentsessionkey = "Nhibernate. current_session ";
Private Static readonly isessionfactory sessionfactory;
Public static configuration currentconfig {Get; set ;}
Static sessionmanager ()
{
If (sessionfactory = NULL)
{
VaR CFG = new atmsconfiguration ();
VaR model = automap. assemblyof <users> (CFG );
Currentconfig = fluently. Configure ()
. Database (mssqlconfiguration
. Mssql2005
. Connectionstring (C => C. fromconnectionstringwithkey ("dbstring "))
)
. Mappings (M => M. automappings. Add (model ))
. Buildconfiguration ();
Sessionfactory = currentconfig. buildsessionfactory ();
}
}
Public static isession getcurrentsession ()
{
Httpcontext context = httpcontext. Current;
Isession currentsession = context. items [currentsessionkey] As isession;
If (currentsession = NULL)
{
Currentsession = sessionfactory. opensession ();
Context. items [currentsessionkey] = currentsession;
}
Return currentsession;
}
Public static void closesession ()
{
Httpcontext context = httpcontext. Current;
Isession currentsession = context. items [currentsessionkey] As isession;
If (currentsession = NULL)
{
// No current session
Return;
}
Currentsession. Close ();
Context. Items. Remove (currentsessionkey );
}
Public static void closesessionfactory ()
{
If (sessionfactory! = NULL)
{
Sessionfactory. Close ();
}
}
}
2. sessionmodule. CS
View code
Public class sessionmodule: ihttpmodule
{
Private isession _ Session;
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (begintransaction );
Context. endrequest + = new eventhandler (closesession );
}
Private void closesession (Object sender, eventargs E)
{
Sessionmanager. closesession ();
}
// Create our session
Private void begintransaction (Object sender, eventargs E)
{
_ Session = sessionmanager. getcurrentsession ();
}
Public void dispose ()
{
_ Session = NULL;
}
}
Repository. CS
View code
Public class nhibernaterepository <t>: irepository <t> where T: Class
{
Protected isession session = sessionmanager. getcurrentsession ();
Public t get (Object ID)
{
Using (VAR transaction = session. begintransaction ())
{
T returnval = session. Get <t> (ID );
Transaction. Commit ();
Return returnval;
}
}
Public void save (T value)
{
Using (VAR transaction = session. begintransaction ())
{
Session. Save (value );
Transaction. Commit ();
}
}
Public void Update (T value)
{
Using (VAR transaction = session. begintransaction ())
{
Session. Update (value );
Transaction. Commit ();
}
}
Public void Delete (T value)
{
Using (VAR transaction = session. begintransaction ())
{
Session. Delete (value );
Transaction. Commit ();
}
}
Public ilist <t> getall ()
{
Using (VAR transaction = session. begintransaction ())
{
Ilist <t> returnval = session. createcriteria <t> (). List <t> ();
Transaction. Commit ();
Return returnval;
}
}
/// <Summary>
/// Generate databaseschemal
/// </Summary>
/// <Param name = "areyousure"> </param>
Public void GENERATESCHEMA (dbcheck areyousure)
{
If (areyousure = dbcheck. thiswilldropmydatabase)
New schemaexport (sessionmanager. currentconfig). Execute (true, true, false );
}
}
This httpmodule method will open session/close session for each httprequest (such as JS and IMG loading.
Another method is to use the di (dependency injection) method ninject.
Sessioninjecthelper. CS
View code
Public class sessioninjecthelper {
Public static string session_key = "nhib.pdf. isession ";
Public static Nhibernate. isession getrequestsession (icontext CTX)
{
Idictionary dict = httpcontext. Current. items;
Nhibsion. isession session;
If (! Dict. Contains (session_key ))
{
// Create an nhib1_session for this request
Session = (CTX. kernel. getservice (typeof (sharedconfiguration) as sharedconfiguration). opensession ();
Dict. Add (session_key, session );
}
Else
{
// Re-use the nhib1_session for this request
Session = (Nhibernate. isession) dict [session_key];
}
Return session;
}
}
Inject Service
View code
Private Static void registerservices (ikernel kernel)
{
Kernel. Bind <iusersrepository> (). To <userrepository> ();
VaR CFG = new sharedconfiguration ();
Kernel. Bind <sharedconfiguration> (). toconstant (CFG );
Kernel. Bind <nhibsion. isession> (). tomethod (x => sessioninjecthelper. getrequestsession (x ));
}
Finally, in application_end
View code
Public class mvcapplication: system. Web. httpapplication
{
Public mvcapplication ()
{
This. endrequest + = mvcapplication_endrequest;
}
Private void mvcapplication_endrequest (Object sender, system. eventargs E)
{
If (context. Items. Contains (sessioninjecthelper. session_key ))
{
Nhibernate. isession session = (Nhibernate. isession) Context. items [sessioninjecthelper. session_key];
Session. Close ();
Session. Dispose ();
Context. items [sessioninjecthelper. session_key] = NULL;
}
}