总体概览

一个非常高层次的NHibernate架构:

这个图展示了NHibernate使用数据库和配置信息来为应用程序提供持久化服务(和持久化对象)。

我们想展示一个更加详细的运行时架构。但是NHibernate 很灵活并且支持多种架构方式。我们将会展示两个极端。对于“轻量级”架构,应用程序管理自身的ADO.NET连接和事务。这个方式仅仅使用了很小的一部分NHibernate API:

对于“重量级”架构,它将应用程序的ADO.NET抽象出来,然后使用NHibernate 管理其中的细节。

以下是图中一些对象的定义:

ISessionFactory (NHibernate.ISessionFactory)
 

一个线程安全(不变的)的单数据库mapping缓存。ISession 的制造工厂和IConnectionProvider 的消费者。可能拥有一个自定义的(二级)在处理器集群水平下的(processor cluster-level)事务之间可重用数据缓存。

 
ISession (NHibernate.ISession)
 

生命周期短暂的单线程的对象,表示应用程序和数据库之间的会话。包含一个ADO.NET连接,是ITransaction的工厂。

拥有一个强制的(一级)持久化对象缓存,在查询对象图谱或者通过标识符查询对象的时候提供缓存。

 
Persistent Objects and Collections
 

生命周期短暂的单线程的对象,包含持久化状态和业务方法。他们可能是普通的POCO,唯一不同之处在于他们和(一个)ISession关联。只要这个Session 关闭了,他们就会变成detached 状态,并且可以在任何应用层无障碍使用(例如,直接作为展示层的DTO)。

Transient Objects and Collections

持久化类的实例,不和ISession 关联。他们可能已经被应用程序实例化,并且(还)没有持久化或者是已经被一个关闭的ISession实例化。

 
ITransaction (NHibernate.ITransaction)
 

(可选的)生命周期短暂的单线程的对象,作为原子的工作单元(atomic units of work)被用在应用程序中。将应用程序从相应的ADO.NET 事务抽象出来。在某些情况下,一个ISession 可能横穿多个ITransactions 。

IConnectionProvider (NHibernate.Connection.IConnectionProvider)
 
(可选的)一个ADO.NET 连接和命令工厂。将应用程序特定(数据库)供应商实现的IDbConnection 和IDbCommand 抽象出来。不暴露给应用程序,但是可以被开发者扩展或者实现。
 
IDriver (NHibernate.Driver.IDriver)
(可选的)一个包含各个ADO.NET提供者的不同配置的接口,例如各自的参数命名惯例和支持的ADO.NET 特征。
 
ITransactionFactory (NHibernate.Transaction.ITransactionFactory)
(可选的)一个ITransaction 实例的工厂。不暴露给应用程序,但是可以被开发者扩展或者实现。

在“轻量级”的架构中,应用程序绕过所有ITransaction/ITransactionFactoryIConnectionProvider ,直接与ADO.NET 交互。

实例状态

根据持久化上下文(persistence context)的定义,一个持久化类可能有三个状态中的一个。NHibernate ISession 是持久化上下文:

瞬时态(transient)

这个实例没有,并且永远从来没有和持久化上下文有关联。它没有持久化标识符(主键)。

持久态(persistent)

这个实例和一个持久化上下文关联。它有一个持久化标识符(主键),也许,在数据库中也有对应的记录。对于一个特定的持久化上下文,NHibernate 能保证持久化对象identity和CLR identity一致(对象在内存中的位置)。

游离态(detached)

这个实例曾经和一个持久化上下文关联,但是上下文已经关闭或者实例已经被序列化到其他进程中。它有一个持久化标识符,也许,在数据库中也有对应的记录。对于游离态的实例,NHibernate 不能保证持久化对象identity和CLR identity一致。

上下文会话

Most applications using NHibernate need some form of "contextual" sessions, where a given session is in effect throughout the scope of a given context. However, across applications the definition of what constitutes a context is typically different; and different contexts define different scopes to the notion of current.

大多数使用NHibernate的应用程序需要一些“上下文的”会话形式,在这个上下文中会话是有效的。然而,从整个应用程序来看,上下文的组成是不同的;并且不同的上下文定义了不同的scopes to the notion of current.

从1.2版本起,NHibernate添加了 ISessionFactory.GetCurrentSession() 方法。ISessionFactory.GetCurrentSession() 背后的处理方式是可拔插的。添加了一个扩展的接口(NHibernate.Context.ICurrentSessionContext) 和一个新的配置参数(hibernate.current_session_context_class) 以支持可拔插性。

通过查看API文档来获得NHibernate.Context.ICurrentSessionContext 接口的相关详情。它定义了一个方法,CurrentSession(),这个实现通过这个方法来负责追踪当前上下文会话。NHibernate提供了一些立即可用的接口的实现:

Starting with version 1.2, NHibernate added the ISessionFactory.GetCurrentSession() method. The processing behind ISessionFactory.GetCurrentSession() is pluggable. An extension interface (NHibernate.Context.ICurrentSessionContext) and a new configuration parameter (hibernate.current_session_context_class) have been added to allow pluggability of the scope and context of defining current sessions.

See the API documentation for the NHibernate.Context.ICurrentSessionContext interface for a detailed discussion of its contract. It defines a single method, CurrentSession(), by which the implementation is responsible for tracking the current contextual session. Out-of-the-box, NHibernate comes with several implementations of this interface:

  • NHibernate.Context.CallSessionContext - current sessions are tracked by CallContext. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext .

  • NHibernate.Context.ThreadStaticSessionContext - current session is stored in a thread-static variable. This context only supports one session factory. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext.

  • NHibernate.Context.WebSessionContext - stores the current session in HttpContext. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext.

  • NHibernate.Context.WcfOperationSessionContext - current sessions are tracked by WCF OperationContext. You need to register the WcfStateExtension extension in WCF. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext.

  • NHibernate.Context.ManagedWebSessionContext - current sessions are tracked by HttpContext. Removed in NHibernate 4.0 - NHibernate.Context.WebSessionContext should be used instead. You are responsible to bind and unbind an ISession instance with static methods on this class, it never opens, flushes, or closes an ISession itself.

    The hibernate.current_session_context_class configuration parameter defines which NHibernate.Context.ICurrentSessionContext implementation should be used. Typically, the value of this parameter would just name the implementation class to use (including the assembly name); for the out-of-the-box implementations, however, there are corresponding short names: "call", "thread_static", "web" and "wcf_operation", respectively.

NHibernate官方文档中文版-框架架构(Architecture)的更多相关文章

  1. NHibernate官方文档中文版--基础ORM(Basic O/R Mapping)

    映射声明 对象/关系映射在XML文件中配置.mapping文件这样设计是为了使它可读性强并且可修改.mapping语言是以对象为中心,意味着mapping是围绕着持久化类声明来建立的,而不是围绕数据表 ...

  2. NHibernate官方文档中文版--拦截器和事件(Interceptors and events)

    对于应用程序来说,能够对NHibernate内部发生的事件做出响应式很有用的.这能够有助于实现一些类的功能或者扩展NHibernate的功能. 拦截器 IInterceptor接口提供了应用程序ses ...

  3. NHibernate官方文档中文版--ISessionFactory的配置(ISessionFactory Configuration)

    由于NHibernate是被设计应用在许多不同环境中的,因此它存在很多配置参数.幸运的是,这些参数大多都有合理的默认值,而且NHibernate发布的时候伴随着一个App.config 例子(可在sr ...

  4. NHibernate官方文档中文版--只读实体类型(Read-only entities)

    重点 NHIbernate处理只读对象的方式可能和你在别处看到的不同.不正确的使用方式可能造成不可预料的结果. 当一个实体是只读的时候: NHIbernate不会对实体的简单属性和单向关联数据检查数据 ...

  5. NHibernate官方文档中文版——事务和并发(Transactions And Concurrency)

    NHibernate本身并不是一个数据库.它是一个轻量级的对象-关系映射工具.因此,它的事务管理代理给对应的数据库连接.如果这个连接代理了一个分布式的事务,ISession管理的操作就会自动成为整个分 ...

  6. NHibernate官方文档中文版——批量插入(Batch inserts)

    A naive approach t7o inserting 100 000 rows in the database using NHibernate might look like this: 一 ...

  7. NHibernate官方文档中文版——持久化类(Persistent Classes)

    持久化类是一个应用程序中的类,主要用来实现业务逻辑(例如,在电商应用中的客户和订单类).持久化类,就像它的名字一样,生命周期短暂并且用来持久化的据库对象实例. 如果这些类的构造能够依照一些简单的原则, ...

  8. 人工智能系统Google开源的TensorFlow官方文档中文版

    人工智能系统Google开源的TensorFlow官方文档中文版 2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,机器学习作为人工智能的一种类型,可以让软件根据大量的 ...

  9. TestNG官方文档中文版(2)-annotation(转)

    1. 介绍    TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器). 编写一个测试的 ...

随机推荐

  1. Linux Platform驱动模型(二) _驱动方法【转】

    转自:http://www.cnblogs.com/xiaojiang1025/archive/2017/02/06/6367910.html 在Linux设备树语法详解和Linux Platform ...

  2. V4L2(二)虚拟摄像头驱动vivi深入分析【转】

    转自:http://www.cnblogs.com/tureno/articles/6694463.html 转载于: http://blog.csdn.net/lizuobin2/article/d ...

  3. python实战===2017年30个惊艳的Python开源项目 (转)

    本文转自:http://www.sohu.com/a/216723120_115128 摘要:本文来自Mybridge,介绍了过去一年里30个惊艳的Python开源项目.点击每一个都可以在GitHub ...

  4. libsensor

    https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...

  5. MVC自定义路由实现URL重写,SEO优化

    //App_Start-RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollect ...

  6. JS面试题第一弹

    1.javascript的typeof返回哪些数据类型  alert(typeof [1, 2]); //object     alert(typeof 'leipeng'); //string   ...

  7. [LabVIEW架构]ActorFramework(一)

    前言 小黑结婚回来第二周了,每天忙于程序设计,时间比较紧张,所以文章一直没出来,也算憋大招了. 近期小黑将与大家一起认识一下ActorFramework,既是对自己一段时间写AF程序的总结,也是梳理, ...

  8. [New learn]GCD的基本使用

    https://github.com/xufeng79x/GCDDemo 1.简介 介绍GCD的使用,介绍多种队列与同步异步多种情况下的组合运行情况. 2.基本使用步骤 如果使用GCD则一般也就两个步 ...

  9. js判断文件格式及大小

      //判断照片大小 function getPhotoSize(obj){     photoExt=obj.value.substr(obj.value.lastIndexOf(".&q ...

  10. Excel Sheet Column Title&&Excel Sheet Column Number

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...