代码改变世界

NHibernate in Action 2.2

2009-12-10 08:04  jiva  阅读(283)  评论(0编辑  收藏  举报

2.2 Understanding the architecture

作者先给了一个典型图:阐释了NH提供给Business和Persistence 层的主要接口。如下:

clip_image002

NH在图2.1中提供的接口大体上可以归为如下几类:

  1. Application调用以便来执行基本的CRUD操作和查询操作(Create, Retrieve, Update, Delete)。这是Application 在业务和控制逻辑(business/control logic )对NH依赖最多的以点。包含ISession, ITransaction, IQuery , ICriteria。
  2. Application用来配置NH的代码。主要是Configuration Class。
  3. 允许Application对在NH内部发生的事件(events)作出反应的回调(callback)。比如IInterceptor, ILifecycle, IValidateble。
  4. 对NH进行扩展的部分。比如IUserType, ICompositeUserType, IIdentifierGenerator。

作者说"NHibernate makes use of existing .Net APIs, including ADO.net and its ITransaction API"。

2.2.1 The Core Interfaces

(一)ISESSION INTERFACE

ISession是NH Application中主要使用的接口,提供了finding, saving, updating, deleting Object的方法。是轻量级的,线程不安全的。

在NH中Session的概念介于connection 和transaction之间,可以把它看作是和一个工作单元(a single unit of work)相关的已经加载对象的集合或者缓存。

这句话很好的解释了在2.1节为什么一个Session中获取对象A,在另一个Session中也获取对象A,两者不会同步的问题。看来这个NH的缓存真是Session级别的。比如像下面的代码,这两个Session都是同一个SessionFactory创建的,但是在第一个Session中,获取e3的时候不会去数据库取数据,在第二个Session中,获取e1的时候是会到数据库中获取数据的。

作者说Session对象的创建和释放是inexpensive,所以可以任意的创建释放,因此一个Session生命周期应该是比较短的,在这么短的时间内,是可以忽略更新不及时造成的问题的。Session有点像GSP中的SubModel的概念。像缓存这种东西,是应该放到SessionFactory这种生命周期长的地方。

 

代码
using (ISession sesion = OpenSession()) 



   Employee e1 
= sesion.Get<Employee>(1); 

   Employee e3 
= sesion.Get<Employee>(1); 



Console.WriteLine(
"New session start"); 

using (ISession sesion = OpenSession()) 



   Employee e1 
= sesion.Get<Employee>(1); 

   Employee e3 
= sesion.Get<Employee>(1); 


 

(二) ISESSIONFACORY INTERFACE

下面这句话还是很好的阐述了ISessionFactory和ISession的区别的:

The SessionFactory caches generated SQL statements and other mapping meta-data that NHibernate uses at runtime. It can also hold cached data that has been read in one unit of work and which may be reused in a future unit of work or session. This is possible if you configure class and collection mappings to use the second-level cache.

(三)CONFIGURATION INTERFACE

(四)ITRANSACTION INTERFACE

(五)IQUERY AND ICRITERIA INTERFACES

2.2.2 Callback interfaces

The ILifecycle and IValidatable interfaces let a persistent object react to events relating to its own persistence lifecycle. 这东西非常类似于GSP中的触发器。比如说当修改某个字段的时候触发某些任务。估计会比较好用和实用。应该包含了记录级别和字段级别的增删改等事件。

2.2.3 Types

2.2.4 Extension interfaces

  • a. Primary-key generation (IIdentifierGenerator interface)
  • b. SQL dialect support (Dialect abstract class)
  • c. Caching strategies (ICache and ICacheProvider interfaces)
  • d. ADO.NET connection management (IConnectionProvider interface)
  • e. Transaction management (ITransactionFactory and ITransaction interfaces)
  • f. ORM strategies (IClassPersister interface hierarchy)
  • g. Property-access strategies (IPropertyAccessor interface)
  • h. Proxy creation (IProxyFactory interface)

 

    简要看,NH还是很强大的。对外提供了对象持久化和对象生命周期的管理,对内提供了对NH的配置和扩展能力。这些应该能够解决掉几乎所有的数据库类的应用程序的需要。和GSP相比,GSP提供了一套比较完整的数据敏感控件。