So here is my current UnitOfWork implementation.  This one makes use of the somewhat new current_session_context_class feature. I think this is quite simple compared to some of the others you will find.

public interface IUnitOfWork : IDisposable
{
IUnitOfWork Start();
void BeginTransaction();
void CommitTransaction();
void RollbackTransaction();
}
public class UnitOfWork : IUnitOfWork
{
#region Dependencies public ISessionFactory SessionFactory
{
get;
set;
} #endregion #region IUnitOfWork Members public virtual void BeginTransaction()
{
var session = SessionFactory.GetCurrentSession();
if ( !session.Transaction.IsActive )
{
session.BeginTransaction();
}
} public virtual void CommitTransaction()
{
var session = SessionFactory.GetCurrentSession();
if ( session.Transaction.IsActive )
{
session.Transaction.Commit();
}
} public void RollbackTransaction()
{
var session = SessionFactory.GetCurrentSession();
if ( session.Transaction.IsActive )
{
session.Transaction.Rollback();
}
} public IUnitOfWork Start()
{
if ( !CurrentSessionContext.HasBind(SessionFactory) )
{
var session = SessionFactory.OpenSession();
session.FlushMode = FlushMode.Commit;
CurrentSessionContext.Bind(session);
}
return this;
} #endregion #region IDisposable Members public void Dispose()
{
var session = CurrentSessionContext.Unbind(SessionFactory);
var transaction = session.Transaction;
if ( transaction.IsActive )
{
transaction.Dispose();
}
session.Dispose();
} #endregion
}

All that’s required is this in the hibernate config section:

(for web apps):

<property name="current_session_context_class">web</property>

(for pretty much anything else):

<property name="current_session_context_class">thread_static</property>

and just the barebones bootstrapping:

var sessionFactory = new Configuration().Configure().BuildSessionFactory();
            

Of course, to get this to work you need to register that sessionFactory instance into your IoC container, and register the UnitOfWork type with the container with a transient lifecycle.

From there you can either explicitly create and dispose IUnitOfWork instances for each operation, or set up a HttpModule to create one at the start of each request and dispose of it in the end_request event.

Yet another way to manage your NHibernate ISessionFactory的更多相关文章

  1. [NHibernate]ISessionFactory配置

    系列文章 [Nhibernate]体系结构 引言 在上篇文章学习了orm和nhibernate相关概念,这篇文章主要学习ISessionFactory如何配置. 因为NHibernate被设计为可以在 ...

  2. 关于NHibernate的一些代码

    SessionManager using System; using System.IO; using System.Runtime.Serialization; using System.Runti ...

  3. MVC Nhibernate 示例

    首先,非常感谢提出问题的朋友们,使得本人又去深入研究了NHibernate的<Session-Per-Request 模式>.   前言: 谈到NHibernate大伙并不陌生,搞Java ...

  4. 耗时两月,NHibernate系列出炉

    写在前面 这篇总结本来是昨天要写的,可昨天大学班长来视察工作,多喝了点,回来就倒头就睡了,也就把这篇总结的文章拖到了今天. nhibernate系列从开始着手写,到现在前后耗费大概两个月的时间,通过总 ...

  5. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  6. [Nhibernate]体系结构

    引言 在项目中也有用到过nhibernate但对nhibernate的认识,也存留在会用的阶段,从没深入的学习过,决定对nhibernate做一个系统的学习. ORM 对象-关系映射(OBJECT/R ...

  7. [NHibernate]持久化类(Persistent Classes)

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 引言 持久化类是应用程序用来解决商业问题的类(比如,在电子交易程序中的Customer和Orde ...

  8. [NHibernate]O/R Mapping基础

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) 引言 对象和关系数据库 ...

  9. [NHibernate]集合类(Collections)映射

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

随机推荐

  1. 手机程序的app包名查找,可以在手机上查到

    获取pkgname(安卓软件包名) 1. 先下载pkgName安装文件(pkgName.apk )并在手机上安装2. 打开刚刚安装的pkgName软件,软件会自动生成你手机上软件的包名列表,同时会在手 ...

  2. winform c#中子窗体关闭刷新父窗体

    父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...

  3. 关于Struts2的多文件上传

    之前写过一篇文章,关于Struts2文件上传:http://www.cnblogs.com/lichenwei/p/3927964.html 现在来说下多文件上传,其实就把上传文件当成是一个数组去处理 ...

  4. Linux top和负载的解释(转载)

    转载自://www.blogjava.net/freeman1984/archive/2011/12/08/365853.html op命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程 ...

  5. 表达式树(Expression Tree)

    你每创建一个表示表达式的实例时,都可以将该类型实例看成是一棵表达式树.每种表示表达式的类型都有一个具体的类型,如Expression的Variable()方法创建的是ParameterExpressi ...

  6. ubuntu 4.10~5.10 :古老的ubuntu上安装oracle10g的情况

    64位的: 不用想了,安装不上,因为ubuntu库里没有提供编译环境:安装不上gcc-multilib库.也没有libc6-dev-i386库! 自己这点水平,真搞不定! 32位的: 很顺利.很顺利! ...

  7. zabbix-agents客户端安装

    Windows下zabbix客户端的安装 记得关windows上的防火墙 1.首先需要下载zabbix_agents.rar文件 点击这里进行下载 当然也可以在www.zabbix.com进行下载 2 ...

  8. ios学习之UIWebView网页视图调整

    //先来一个可行的小Demo程序:结合searchBar的google搜索 #import <UIKit/UIKit.h> @interface ViewController : UIVi ...

  9. Android测试跑单个包脚本文件

    脚本: adb shell monkey -p 应用包名 --throttle 随机事件间隔 -v -v -v -s 1 --ignore-security-exceptions --kill-pro ...

  10. 【Oracle】BLOB

    1.データベースからの読み込み Dim strSql As String = "select IMG from TBL where ID=XX" Dim cmd As New Or ...