在ASP.NET中使用NHibernate

博客主要讨论了会话工厂建立和NHibernate的Session管理问题。会话工厂用singleton模式建立,在Web Form中,使用ThreadStaticAttribute和TLS保存session不合适,建议用HttpContext.Current.Items共享session,并通过HttpModule处理,还提及相关工具类和页面使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键还是会话工厂的建立和NHibernate的Session的管理问题。
会话工厂仍然是使用singleton模式建立。而session管理则和Wndows Form不同:Wndows Form可以保持长连接,以获得比较好的用户体验,因而可以使用ThreadStaticAttribute或者TLS来保存session;在Web Form中使用ThreadStaticAttribute则不会如期望的那样工作,当有多个线程的时候,ThreadStaticAttribute的变量被第一个线程初始化后,其它的线程访问到的都是null,而每个HttpRequest则可能有多个线程为其服务,因而有人称ThreadStatic is evil,对于此,可以参考这个Blog。考虑到上述原因,在Web Form中使用TLS和ThreadStatic属性是不合适的,好的做法是使用HttpContext.Current.Items来共享session。我们使用HttpModule来处理之,下面是一个例子:

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  DAL;
None.gif
using  NHibernate;
None.gif
None.gif
namespace  NHTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// NHSessionModule 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NHSessionModule : IHttpModule
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Default constructor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public NHSessionModule()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{}
InBlock.gif
InBlock.gif        
private IEntityManager manager = null;
InBlock.gif        
public void Init(HttpApplication context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            context.BeginRequest 
+= new EventHandler(Context_BeginRequest);
InBlock.gif            context.EndRequest 
+= new EventHandler(Context_EndRequest);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Context_BeginRequest(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            manager 
= EntityManager.CreateEntityManager();
InBlock.gif            ISession session 
= manager.Session as ISession;
InBlock.gif            HttpContext.Current.Items.Add(
"NHSession", session);
InBlock.gif            HttpContext.Current.Items.Add(
"Manager",manager);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Context_EndRequest(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Close the NHibernate session.
InBlock.gif
            if ( HttpContext.Current.Items["NHSession"!= null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ISession session 
= HttpContext.Current.Items["NHSession"as ISession;
InBlock.gif                
if ( session != null && session.IsOpen )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

XML配置文件:

None.gif    < system .web >
None.gif    
< httpModules >
None.gif      
< add  type ="NHTest.NHSessionModule,NHTest"  name ="NHSessionModule"   />
None.gif    
</ httpModules >
None.gif   
</ system.web >


上面代码中的IEntityManager 是用来执行持久化以及查询的工具类(调用NHibernate中ISession提供的方法),然后在页面中先写一个页面基类来进行Session的获取:

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  DAL;
None.gif
using  NHibernate;
None.gif
None.gif
namespace  NHTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// BasePage 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class BasePage : Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private ISession _activeSession = null;
InBlock.gif        
private IEntityManager _manager = null;
InBlock.gif
InBlock.gif        
public BasePage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public ISession ActiveSession
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._activeSession; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IEntityManager Manager
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._manager; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._activeSession = HttpContext.Current.Items["NHSession"as ISession;
InBlock.gif            
this._manager = HttpContext.Current.Items["Manager"as IEntityManager;
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


 在页面中可以这么用:

None.gif private  IEntityManager manager  =   null ;
None.gif
private  ISession session  =   null ;
None.gif
private   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   session 
= base.ActiveSession;
InBlock.gif   manager 
= base.Manager;
InBlock.gif   IList list 
= manager.GetList(session, typeof ( MyBusinessType));
InBlock.gif   DataGrid1.DataSource 
= list;
InBlock.gif   DataGrid1.DataBind();
ExpandedBlockEnd.gif}


另外请参考NHibernate的官方说明,还可以看Tobin的论述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值