NHibernate已经成为.net主流的ORM框架,当然,在开发中如果需要使用NHibernate的话,我们一般会对她进行一次封装,以便在项目中使用更方便,以及对NHibernate有一个全局的控制。而对NHibernate的封装也不是那么简单的,比如说NHibernate无法做到夸层的封装,意思就是在其他层使用封装的代码的时候,也是需要引用NHibernate的,比如一般的IQuery条件查询,如果需要排序或者动态条件的话,就需要传递ICriterion到调用层进行设置,当然如果是hql可以使用拼接的方法,但不推荐这种拼接的方法在UI层使用,这样跟sql没有什么差别。

下面代码是我在学习的项目中使用的一点封装,当然没有经过严格的测试,如有bug请告知。

首先是对NHibernate的SessionFactory的封装,这个就大同小异了

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web;
 6 using NHibernate;
 7 using NHibernate.Cfg;
 8 using System.Collections;
 9 
10 namespace LBC.Permission.DAL
11 {
12 
13     public class NHSessionFactory
14     {
15         private static readonly object lockObj = new object();
16         private static NHSessionFactory instance = null;
17         private static ISessionFactory factory;
18 
19         private NHSessionFactory() { }
20 
21         public static NHSessionFactory Instance
22         {
23             get
24             {
25                 if (instance == null)
26                 {
27                     lock (lockObj)
28                     {
29                         if (instance == null)
30                         {
31                             instance = new NHSessionFactory();
32                             Configuration cfg = new Configuration();
33                             factory = cfg.Configure().BuildSessionFactory();
34                         }
35                     }
36                 }
37                 return instance;
38             }
39         }
40 
41         public ISession GetCurrentSession()
42         {
43             return factory.GetCurrentSession();
44         }
45 
46         public ISession OpenSession()
47         {
48             return factory.OpenSession();
49         }
50 
51         public ISessionFactory SessionFactory
52         {
53             get
54             {
55                 return factory;
56             }
57         }
58 
59     }
60 }

然后是对常用方法的封装

1 public partial class DataAccess

2     {
 3         private ISession session;
 4 
 5         public DataAccess()
 6         {
 7             session = NHSessionFactory.Instance.GetCurrentSession();
 8         }
 9 
10         #region Transaction
11 
12         public void Transaction(Action action)
13         {
14             ITransaction transaction = session.BeginTransaction();
15             try
16             {
17                 action();
18                 transaction.Commit();
19             }
20             catch
21             {
22                 transaction.Rollback();
23                 throw;
24             }
25         }
26 
27         public void Transaction(Action action, System.Data.IsolationLevel isolationLevel)
28         {
29             ITransaction transaction = session.BeginTransaction(isolationLevel);
30             try
31             {
32                 action();
33                 transaction.Commit();
34             }
35             catch
36             {
37                 transaction.Rollback();
38                 throw;
39             }
40         }
41 
42         #endregion
43     }

转自:http://www.cnblogs.com/liubiaocai/archive/2011/09/17/2179350.html

最新文章

  1. Labview中局部变量和全局变量
  2. MyEclipse 2014 + JSP+ Servlet
  3. 数据库SQL Server与C#中数据类型的对应关系
  4. flex-linkbutton
  5. golang 并发之协程及通道
  6. Net 4.0 之 Dynamic 动态类型
  7. 蓝桥网试题 java 基础练习 十六进制转八进制
  8. java 学习笔记之 流、文件的操作
  9. [js高手之路] vue系列教程 - 事件专题(4)
  10. Netty 核心内容之 编解码器
  11. Gitlab使用Webhook实现Push代码后的jenkins自动构建
  12. H5 表单标签
  13. Subway POJ - 2502 最短路
  14. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习9
  15. PHP中递归的实现(附例子)
  16. Struts2+AJAX+JQuery 实现用户登入与注册功能。
  17. python 导出mongoDB数据中的数据
  18. 解决报错"Your security system have blocked an application with expired or not yet valid certificate from running"
  19. linux 安装jdk 二进制版本,非安装版
  20. 构建树形结构数据(全部构建,查找构建)C#版

热门文章

  1. mysql中的load命令使用方法
  2. 利用数据库链做DML操作时报ORA-02069: global_names parameter must be set to TRUE for this operation
  3. Linux命令总结(转载)
  4. Unity3d Shader开发(三)Pass(Alpha testing )
  5. 深入浅出JMS(二)——JMS的组成
  6. 30 个最棒的 jQuery 的拖放插件
  7. bzoj 2555: SubString 后缀自动机+LCT
  8. BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
  9. ***PHP各种编码的汉字字符串截取
  10. POJ 3592 Instantaneous Transference(强联通分量 Tarjan)