Use SingletonSession to create an ISession object
Using System;
Using nhib.pdf;
Using System. Reflection;
Using nhib.pdf. Cfg;
Namespace CmsModels
{
///
/// Summary of Sessions.
///
Public class SingletonSession
{
Private static readonly object lockObj = new object ();
Private static ISessionFactory _ factory;
Public SingletonSession ()
{
//
// TODO: add the constructor logic here
//
}
Public static ISessionFactory Factory
{
Get
{
If (_ factory = null)
{
Lock (lockObj)
{
If (_ factory = null)
{
_ Factory = new Configuration (). Configure (). BuildSessionFactory ();
}
}
}
Return _ factory;
}
}
Public static ISession GetSession ()
{
Return Factory. OpenSession ();
}
}
}
Data Access Object example:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using nhib.pdf;
Using nhib.pdf. Cfg;
Using nhib.pdf. Criterion;
Namespace CmsModels
{
Public class DAONews
{
Public DAONews ()
{
}
/// <Summary>
/// Gets Menus by NID
/// </Summary>
/// <Param name = "ID"> News ID </param>
/// <Returns> IList <GTNews> </returns>
Public IList <GTNews> GetNewsByNid (Int32 ID)
{
ICriteria cri = SingletonSession. GetSession (). CreateCriteria (typeof (GTNews ));
Cri. Add (Restrictions. Eq ("NID", ID ));
Cri. AddOrder (new nhib.pdf. Criterion. Order ("NID", true ));
Return cri. List <GTNews> ();
}
Public IList <GTNews> GetAllNews ()
{
ICriteria cri = SingletonSession. GetSession (). CreateCriteria (typeof (GTNews ));
Cri. AddOrder (new nhib.pdf. Criterion. Order ("IsTop", false ));
Cri. AddOrder (new nhib.pdf. Criterion. Order ("NID", false ));
Return cri. List <GTNews> ();
}
Public bool Delete (String newIDs)
{
Try
{
SingletonSession. GetSession (). CreateSQLQuery ("delete from gtnews where nid in (" + newIDs + ")"). ExecuteUpdate ();
// Note that the following line of code must be added during modification and deletion.
SingletonSession. GetSession (). Flush ();
Return true;
}
Catch
{
Return false;
}
}
Public bool Save (GTNews news)
{
// Try
//{
SingletonSession. GetSession (). Save (news );
Return true;
//}
// Catch
//{
// Return false;
//}
}
Public bool Update (GTNews news)
{
// Try
//{
ISession _ session = SingletonSession. GetSession ();
_ Session. Update (news );
_ Session. Flush ();
Return true;
//}
// Catch
//{
// Return false;
//}
}
}
}