ASP.NET中NHibernate的應用

關鍵還是會話工廠的建立和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.gifusing System;
None.gif
using System.Web;
None.gif
using DAL;
None.gif
using NHibernate;
None.gif
None.gif
namespace NHTest
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// NHSessionModule 的摘要說明。
ExpandedSubBlockEnd.gif    
/// </summary>

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

InBlock.gif        public NHSessionModule()
ExpandedSubBlockStart.gif        
{}
InBlock.gif
InBlock.gif        
private IEntityManager manager = null;
InBlock.gif        
public void Init(HttpApplication context)
ExpandedSubBlockStart.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.gif        
{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Context_BeginRequest(object sender, EventArgs e)
ExpandedSubBlockStart.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.gif        
{
InBlock.gif            
// Close the NHibernate session.
InBlock.gif
            if ( HttpContext.Current.Items["NHSession"!= null )
ExpandedSubBlockStart.gif            
{
InBlock.gif                ISession session 
= HttpContext.Current.Items["NHSession"as ISession;
InBlock.gif                
if ( session != null && session.IsOpen )
ExpandedSubBlockStart.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.gifusing 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.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// BasePage 的摘要說明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class BasePage : Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
private ISession _activeSession = null;
InBlock.gif        
private IEntityManager _manager = null;
InBlock.gif
InBlock.gif        
public BasePage()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此處添加構造函數邏輯
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public ISession ActiveSession
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return this._activeSession; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IEntityManager Manager
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return this._manager; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.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.gifprivate IEntityManager manager = null;
None.gif
private ISession session = null;
None.gif
private void Button1_Click(object sender, EventArgs e)
ExpandedBlockStart.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的論述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章