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. 软件设计模式之适配器模式(JAVA)

    什么是适配器模式? 在计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的.适配器能将因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存 ...

  2. 使用intellij idea打包并部署到外部的tomcat

    1.使用intellij idea创建项目demotest File -> New -> Project-> Spring Initializr,根据提示一步步操作 会生成一个带有 ...

  3. php命令

    今天因为psi无法在5.3版本上运行,正好看了下这些php版本的特性. 无意中,5.4版本 Buid-in web server内置了一个简单的Web服务器 $ php -S localhost: 于 ...

  4. [RN] 01 - Init: Try a little bit of React Native

    Ref: React Native跨平台移动应用开发 后记:这本书博客味有点浓. 本篇涉及新建工程的若干套路,以及一点语法知识. 创建新工程 (1) 解决的一大核心问题: (2) 使用Javascri ...

  5. Linux oracle数据库创建表空间、用户并赋予权限

    管理员用户登录oracle数据库 1.创建临时表空间 select name from v$tempfile;查出当前数据库临时表空间,主要是使用里面的存放路径: 得到其中一条记录/opt/oracl ...

  6. php扩展AMQP,安装报错解决

    接下来来安装php扩展AMQP,安装了它以后,才能用PHP操作rabbitmq.wget https://pecl.php.net/get/amqp-1.4.0.tgztar -zxvf amqp-1 ...

  7. MongoDB(四)-- 主从配置

    一.前言 虽然MongoDB官方已经不建议使用主从模式了,但是 熟悉下 也是有用的,替代方案是采用副本集的模式.slave默认情况下是不支持读写的,但是master会把数据同步到slave,不支持客户 ...

  8. apt-get/dpkg常用指令备查

    apt-get install <package> Downloads <package> and all of its dependencies, and installs ...

  9. Elasticsearch 配置同义词

    配置近义词 近义词组件已经是elasticsearch自带的了,所以不需要额外安装插件,但是想要让近义词和IK一起使用,就需要配置自己的分析器了. 首先创建近义词文档 在config目录下 mkdir ...

  10. 在css中 父元素不固定高度,怎样实现子元素的高度100%

    父元素使用 position: relative; 子元素使用 position: absolute; height: 100%;