SoftwareDevelopWeb

使用 HTTP 上下文的Wen场景,以及使用 桌面的客户端。 NHibernate 框架的使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using NHibernate;
 7 using NHibernate.Cfg;
 8 using System.Web;
 9 
10 namespace Agathas.Storefront.Repository.NHibernate.SessionStorage
11 {
12     /// <summary>
13     /// (在实现了保存会话的功能后)需要某种方式来创建会话。
14     /// 能够使用 NHibernate 来持久化和检索业务实体。
15     /// </summary>
16     public class SessionFactory
17     {
18         // NHibernate.ISessionFactory 由会话工厂来创建
19         private static ISessionFactory _sessionFactory;
20 
21         // 初始化
22         
23         public static void Init()
24         {
25             // 创建实例及配置组装
26             Configuration config = new Configuration();
27             config.AddAssembly("Agatha.Storefront.Repository.NHibernate");
28 
29             // 日志 log4net
30             log4net.Config.XmlConfigurator.Configure();
31 
32             // 实施配置
33             config.Configure();
34 
35             // 获取会话工厂
36             _sessionFactory = config.BuildSessionFactory();
37         }
38 
39         // 获取会话工厂
40         private static ISessionFactory GetSessionFactory()
41         {
42             if (_sessionFactory == null)
43                 Init();
44 
45             return _sessionFactory;
46         }
47 
48         private static ISession GetNewSession()
49         {
50             return GetSessionFactory().OpenSession();
51         }
52 
53         public static ISession GetCurrentSession()
54         {
55             ISessionStorageContainer sessionStorageContainer =
56                 SessionStorageFactory.GetSessionStorageContainer();
57 
58             ISession currenSession = sessionStorageContainer.GetCurrentSession();
59 
60             if (currenSession == null)
61             {
62                 currenSession = GetNewSession();
63                 sessionStorageContainer.Store(currenSession);
64             }
65 
66             return currenSession;
67         }
68 
69     }
70 }
View Code
原文地址:https://www.cnblogs.com/masterSoul/p/7604667.html