惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
博客园_首页
G
Google Developers Blog
有赞技术团队
有赞技术团队
Vercel News
Vercel News
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
IT之家
IT之家
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
A
About on SuperTechFans
The Register - Security
The Register - Security
腾讯CDC
月光博客
月光博客
GbyAI
GbyAI
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
D
Docker
D
DataBreaches.Net
博客园 - 聂微东
C
Check Point Blog
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
F
Full Disclosure
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
小众软件
小众软件
量子位
L
LangChain Blog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
F
Fortinet All Blogs

博客园 - Ferry

ASP.NET网站权限设计实现(三)——套用JQuery EasyUI列表显示数据、分页、查询 ASP.NET网站权限设计实现(二)——角色权限绑定 ASP.NET读取网络图片并在页面上显示 ASP.NET网站权限设计实现(一)——使用PowerDesigner进行数据库设计 Visual Studio 2010 架构图之用例图(UML Use Case Diagram) ASP.NET MVC3 新的视图引擎(View Engine) 一个德国人图解中西文化差异(值得一看) 微软AJAX CDN(内容分发网络) 注册使用GAC—Global Assembly Cache(.NET) ASP.NET 递归将分类绑定到 TreeView - Ferry 新浪微博PC客户端(DotNet WinForm版)——功能实现分解介绍 新浪微博PC客户端(DotNet WinForm版)—— 初探 C#:30行数据插入到数据库中的效率测试-一行行执行、构造SQL一次执行、SqlBulkCopy C#使用GMAIL群发带附件邮件的例子 ASP.NET Web 应用开发实战快速上手系列 3—C#面向对象编程纲要-类 ASP.NET带进度条多文件上传 ASP.NET4.0中客户端ID的生成 在高优先级下运行应用程序 ASP.NET Web 应用开发实战快速上手系列 2——C#基础
学习使用NHibernate2.1.0Beta1(续七)— 单例模式
Ferry · 2010-09-29 · via 博客园 - Ferry

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

    }
}