学习使用NHibernate2.1.0Beta1(续七)— 单例模式

用类SingletonSession创建ISession对象

using System;
using NHibernate;
using System.Reflection;
using NHibernate.Cfg;

namespace CmsModels
{
    
/// 
    
/// Sessions 的摘要说明。
    
/// 
    public class SingletonSession
    {
        
private static readonly object lockObj = new object();
        
private static ISessionFactory _factory;

        
public SingletonSession()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }
        
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();
        }
    }
}

数据访问对象示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.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 NHibernate.Criterion.Order("NID"true));
            
return cri.List<GTNews>();
        }

        
public IList<GTNews> GetAllNews()
        {
            ICriteria cri 
= SingletonSession.GetSession().CreateCriteria(typeof(GTNews));
            cri.AddOrder(
new NHibernate.Criterion.Order("IsTop",false));
            cri.AddOrder(
new NHibernate.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();
                
//注意在修改和删除时需要加下面一行代码
                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;
            
//}
        }

    }
}


 

 

posted on 2010-09-29 17:08  Ferry  阅读(352)  评论(1编辑  收藏  举报

导航