Yet another way to manage your NHibernate ISessionFactory
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的更多相关文章
- [NHibernate]ISessionFactory配置
系列文章 [Nhibernate]体系结构 引言 在上篇文章学习了orm和nhibernate相关概念,这篇文章主要学习ISessionFactory如何配置. 因为NHibernate被设计为可以在 ...
- 关于NHibernate的一些代码
SessionManager using System; using System.IO; using System.Runtime.Serialization; using System.Runti ...
- MVC Nhibernate 示例
首先,非常感谢提出问题的朋友们,使得本人又去深入研究了NHibernate的<Session-Per-Request 模式>. 前言: 谈到NHibernate大伙并不陌生,搞Java ...
- 耗时两月,NHibernate系列出炉
写在前面 这篇总结本来是昨天要写的,可昨天大学班长来视察工作,多喝了点,回来就倒头就睡了,也就把这篇总结的文章拖到了今天. nhibernate系列从开始着手写,到现在前后耗费大概两个月的时间,通过总 ...
- 【翻译】Fluent NHibernate介绍和入门指南
英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...
- [Nhibernate]体系结构
引言 在项目中也有用到过nhibernate但对nhibernate的认识,也存留在会用的阶段,从没深入的学习过,决定对nhibernate做一个系统的学习. ORM 对象-关系映射(OBJECT/R ...
- [NHibernate]持久化类(Persistent Classes)
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 引言 持久化类是应用程序用来解决商业问题的类(比如,在电子交易程序中的Customer和Orde ...
- [NHibernate]O/R Mapping基础
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) 引言 对象和关系数据库 ...
- [NHibernate]集合类(Collections)映射
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...
随机推荐
- linux nginx配置新项目加域名
找到nginx的配置文件 nginx/nginx.conf 第一种方,法直接在nginx.com里面配置 user www www; worker_processes auto; error_log ...
- thikphp5.0 ip地址库 解决卡顿问题 curl_init
使用淘宝新浪的地址库非常的使用,但是调用有时候会出现很慢.会导致卡在当前网页. 要想不影响当前速度,因此要使用 curl_init功能. 项目案例:会员登陆日志 user_log 字段:id,user ...
- JSTL时间格式化项目小试
我在之前的博客中虽然详尽的介绍了JSTL在各个载体中的用法,也介绍了它和EL的共同使用的好处,但是只是顺便提了一下JSTL的格式化. 今天在项目中遇到了一个小问题,也就想到这,马上就开始实践了一下,效 ...
- 如何获取模拟器安装的app的位置
你可以死记下地址格式, 但是一旦不同的xcode和模拟器版本改变变了地址, 又得记, 从活动管理器里其实是可以直接查看的: Launch the app in the simulator Open A ...
- python获取数组中最多的元素
获取数组中数量最多的元素,也就是最频繁的那个元素,方法有很多,下面是3种最简单的: 用max函数 sample = [1,2,3,3,3,4,5,5] max(set(sample), key=sam ...
- Java多线程-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
Java多线程编程-(1)-线程安全和锁Synchronized概念 Java多线程编程-(2)-可重入锁以及Synchronized的其他基本特性 Java多线程编程-(3)-从一个错误的双重校验锁 ...
- 基于jQuery的tooltips插件--poshytip
摘要: 分享一款在项目中使用的tooltips插件--poshytip,这是一款基于jQuery的插件,其特点是简单易用,支持浏览器:IE6+, FF 2+, Opera 9+, Safari 3+, ...
- git同时提交到两个仓库
有时候一个项目,希望既提交到oschina又提交到公司内网的gitlab,或者是github什么的. 使用git remote -v 查看当前git的远程仓库. 添加一个远程仓库 git remote ...
- NHibernate 数据查询之Linq to NHibernate
刚学NHibernate的时候觉得,HQL挺好用的,但是终归没有与其他技术相关联,只有NHibernate用到,一来容易忘记,二来没有智能提示,排除错误什么的都不给力,直到看到一个同事用Linq to ...
- 项目管理PMP输入输出ITTO联系记忆
综述九大领域 项目管理的输入输出非常难记,原因在于理解起来也经不去推敲,故整理一个联想记忆版本,通过联想把输入输出都串起来达到记忆的目的,既然是联想,里面的内容逻辑只是为了好记,并无正确与否,请大家原 ...