温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:逐迹内容管理系统AspxNuke v2.0源码
当前文件路径:AspxNuke/Common/NHibernate/NHibernateHelper.cs

1using System.Web; 2
using NHibernate; 3
using NHibernate.Cfg; 4
5
namespace AspxNuke.NH 6
{ 7
/// <summary> 8
/// NHibernate帮助类 9
/// </summary> 10
public sealed class NHibernateHelper 11
{ 12
private const string CurrentSessionKey = "hibernate-configuration"; 13
private static readonly ISessionFactory sessionFactory; 14
15
static NHibernateHelper() 16
{ 17
sessionFactory = new Configuration().Configure().BuildSessionFactory(); 18
} 19
20
/// <summary> 21
/// 打开当前Session 22
/// </summary> 23
/// <returns>Session</returns> 24
public static ISession GetCurrentSession() 25
{ 26
HttpContext context = HttpContext.Current; 27
ISession currentSession; 28
if (context != null) 29
{ 30
currentSession = context.Items[CurrentSessionKey] as ISession; 31
if (currentSession == null) 32
{ 33
currentSession = sessionFactory.OpenSession(); 34
context.Items[CurrentSessionKey] = currentSession; 35
} 36
} 37
else 38
{ 39
currentSession = sessionFactory.OpenSession(); 40
} 41
42
return currentSession; 43
} 44
45
/// <summary> 46
/// 关闭Session 47
/// </summary> 48
public static void CloseSession() 49
{ 50
HttpContext context = HttpContext.Current; 51
if (context != null) 52
{ 53
ISession currentSession = context.Items[CurrentSessionKey] as ISession; 54
55
if (currentSession == null) 56
{ 57
return; 58
} 59
60
currentSession.Close(); 61
context.Items.Remove(CurrentSessionKey); 62
} 63
64
} 65
66
/// <summary> 67
/// 关闭Session工厂 68
/// </summary> 69
public static void CloseSessionFactory() 70
{ 71
if (sessionFactory != null) 72
{ 73
sessionFactory.Close(); 74
} 75
} 76
} 77
} 78





}