代码改变世界

NHibernate in Action 2.2 Understanding the architecture

2011-07-30 16:05  一一九九  阅读(210)  评论(0编辑  收藏  举报

NH API in a layered architecture

image

包含那么几个部分:

  • 被Application调用执行CRUD的接口。包含 ISession, ITransaction, IQuery, ICriteria.
  • 配置NH的结构。 包含Configuration
  • 允许Application回调响应NHibernate内部事件的部分,比如IInterceptor, ILifecycle, iValidateble.  这一部分是对Entity中的Field或者Property的一些变化进行的响应,猜应该有 NewObject, DeleteObject, UpdateObject , Update Field等事件的响应,是实现业务规则的重要地方。
  • 允许扩展NH的Mapping功能的部分。比如IUserType,  ICompositeUserType, IIdentifierGenerator。

另外NH的基础还是ADO.net, 作者也提到:

    NHibernate makes use of existing .net apis, including ADO.net and its ITransaction API. ADO.net provides a rudimentary level of abstraction of functionality common to relational databases, letting NHibernate support almost any database with ADO.net driver.

The core interfaces

ISession interface

  • 负责CRUD操作, exposes NH’methods for finding, saving , updating,  and deleting objects.
  • lightweight and inexpensive. 可以任意的Create 和 destroy的。
  • not thread safe.
  • the nh notion of a session is something between connection and transaction.  it may be easier to think of a session as a cache or collection of loaded objects relating to a single unit of work. we sometimes call the ISession a  persistence manager. ISession的作用应该就是一个Persistence manager,  应该说有点类似于DataSet,可以连接到数据库进行操作,也可以进行事务管理。

ISessionfactory interface

  • isn’t lightweight!
  • one sessionfactory for one database.
  • caches generated SQL statements and other mapping metadata that NHibernate uses at runtime.
  • secod-level cache.  it can also hold cached data that has been read in one unit of work and which may be reused in a futrue unit of work or session . this is possible if you configure class and collection mappings to use the scond-level cache.

Configuration interface

  • specify the location of mapping documents
  • set NHibernate-specifc properties before creating the ISesionfactory

ITransaction interface

  • optional API.
  • 其他的都没有说。

IQuery and ICriteria interface

  • IQuery uses queries written in HQL.
  • ICriteria lets you create and execute object-oriented criteria queries.

Callback interface

  • callback interface allow the application to receive a notification when somthing interesting happens to an object- for example, when an object is loaded, saved, or deleted.
  • the Ilifecycle and ivalidateble interface let a persistent object react to events relating to its own persistence lifecyle. the persistence lifecycle is encompassed by an object’s curd operation.
  • the iinterceptor interface was introduced to let the application process callbacks without forcing the persistent classes to implement nhibernate-specific APIs.

Types

  • 一句话,进行NH和Database 之间的映射。

Extension inteface

  • 主要是Cache,Tranaction等功能