Learn to use NHibernate2.1.0Beta1 (Continued 7)-singleton Mode

Source: Internet
Author: User

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;
//}
}

}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.