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. Linq 实现两个对象实例List之间的赋值

    public class UserCopy { public class LoginEntity { public string UserName { get; set; } public strin ...

  2. Android 混淆代码有关问题总结

    Android 混淆代码问题总结 Android 混淆代码: 最快的方式: 1. 首先更新Android的SDK至最新版本,重新建立1个工程,把源码和资源及其他文件拷到新的工程里面. 2. 工程目录底 ...

  3. 修改ftp服务的监听端口

    需求说明: 今天在做一个项目的时候,低端的端口都给屏蔽掉了,需要进行修改为高端端口才能访问,在此记录下. 操作过程: 1.修改ftp的配置文件vsftpd.conf,增加以下的配置 listen_po ...

  4. tooltips插件

    摘要: 继‘带箭头提示框’,本文将分享几款带箭头提示框. qtipqTip是一种先进的提示插件,基于jQuery框架.以用户友好,而且功能丰富,qTip为您提供不一般的功能,如圆角和语音气泡提示,并且 ...

  5. js 或 且 非

    给定 x=6 以及 y=3,下表解释了逻辑运算符: 运算符 描述 例子 && and (x < 10 && y > 1) 为 true || or (x== ...

  6. Tomcat------如何配置域名和80端口

    1.打开Tomcat的默认安装路径下的Service.xml文件 路径:C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf\Serv ...

  7. Linux+Redis实战教程_day01_Linux介绍与安装

    1.Linux介绍(了解) 1.1.Linux和Windows的区别 Linux是一款操作系统.正规开发 服务器项目部署都是放在Linux操作系统上. Windows一款操作系统,民用操作系统.娱乐. ...

  8. logback -- 配置详解 -- 二 -- <appender>

    附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...

  9. Python easyGUI 猜数字

    import easygui as g import random d=random.randint(0,10) while 1: g.msgbox("现在开始猜数字小游戏:") ...

  10. springJdbc in 查询,Spring namedParameterJdbcTemplate in查询

    springJdbc in 查询,Spring namedParameterJdbcTemplate in查询, SpringJdbc命名参数in查询,namedParameterJdbcTempla ...