现在的位置: 首页 > 综合 > 正文

NHibernate 博客园专题 (学习篇)

2012年09月20日 ⁄ 综合 ⁄ 共 10078字 ⁄ 字号 评论关闭

本文约定:
1. Nhibernate简写为NHB;
2. 本文例子的开发平台为win2000pro+sp4, sql server2000, Nhibernate0.5;
3. 使用SQL Server自带的罗斯文商贸数据库(Northwind),是英文版的哦;
4. 本文例子是基于测试驱动开发(TDD)的,因此建议使用NUnit和Log4Net

一 NHB简介
NHB是基于ms.net的O/R Mapping持久框架,它从基于Java的Hibernate项目移植而来。O/R Mapping就是把对象到映射关系数据库的记录,简单的说就是能实现把一个对象存储为数据表中的一条记录和由一条记录创建一个相应的对象,数据表中的数据就是对象的属性。
那么为什么要使用O/R Mapping?它与传统的DataSet/DataTable又有什么不同了?
首先是设计上的不同,当使用O/R Mapping时,更多的是从对象的角度来设计程序,而把数据(对象的属性)存储的细节放在后面, 可以完全采用面向对象(OO)的方式来设计,而在使用DataSet/DataTable时,它只是存放数据的对象,看起来更像一个数据表,不能直观的表达业务概念。

二 NHB中主要接口的介绍

ISession
ISession是面向用户的主要接口,主要用于对象持久化,数据加载等操作,支持数据库事务,它隐藏了NHB内部复杂的实现细节,ISession由ISessionFactory创建。

ISessionFactory
ISessionFactory是NHB内部的核心类,它维护到持久机制(数据库)的连接并对它们进行管理,同时还会保存所有持久对象的映射信息。
ISessionFactory由Configuration创建,因为创建ISessionFactory的开销非常大(需要加载映射信息),所以这个对象一般使用Singleton(单例)模式。

ITransaction
ITransaction是NHB的事务处理接口,它只是简单的封装了底层的数据库事务。
事务必须由ISession来启动。

ICriteria
ICriteria是Expression(表达式)数据加载接口,Expression是一个关系表达式组合,通过它能产生SQL语句的Where部分, 用户需要通过ISession来间接调用它。

IQuery
IQuery是HQL数据加载接口,HQL(Hibernate Query Language)是NHB专用的面向对象的数据查询语言,它与数据库的SQL有些类似,但功能更强大!同ICriteria一样,也需要通过ISession来间接调用它。

三 持久化操作

1. 会话和会话工厂
要进行持久化操作,必须先取得ISession和ISessionFactory,我们用一个Sessions类来封装它们, Sessions类的属性和方法都是静态的,它有一个Factory属性, 用于返回ISessionFactory, 有一个GetSession方法,用于取得一个新的ISession。

测试类代码如下:

[TestFixture]
Public
class SessionsFixture {
Public
void SessionsFixture() {
}
[Test]
// 测试能否取得NHB会话工厂。
public void FactoryTest() {
ISessionFactory sf
= Sessions.Factory;
Assert.IsNotNull( sf, “
get sessionfactory fail!” );
}
[Test]
// 测试能否取得NHB会话。
public void GetSessionTest() {
ISession s
= Sessions.GetSession();
Assert.IsNotNull( s, “
get session fail!” );
}
}


 


现在还没写Sessions类,将不能通过编译! 下面我们来实现Sessions类.

 

public class Sessions {
private static readonly object lockObj = new object();
private static ISessionFactory _factory;

public static Sessions() {` }

Public static ISessionFactory Factory {
get {
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
Cfg.Configuration cfg
= new Cfg.Configuration ();
cfg.AddAssembly( Assembly.GetExecutingAssembly() );
_factory
= cfg.BuildSessionFactory();
}
}
// end lock
}
return _factory;
}
}
public static ISession GetSession() {
return Factory.OpenSession();
}
}


 


OK,现在编译可以通过了,启动NUnit并选择生成的文件NHibernateTest.exe,运行测试。
我们得到了红色的条,出错了!原来还没有加入NHibernate的配置信息(当使用NHibernate时,需要在项目的配置文件中加入NHibernate的配置信息。关于配置信息,在下面有说明)。
在项目的配置文件App.Config(如没有请自行创建一个)中加入以下内容.

<configSections>
<!—定义NHibernate配置节的处理类 -->
<section name="nhibernate"


type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<!— 指定在log中是否显示sql语句, 用于调试 -->
<add key="hibernate.show_sql" value="true" />
<!— 数据库连接提供 -->
<add key="hibernate.connection.provider"
value
="NHibernate.Connection.DriverConnectionProvider" />
<!—指定数据库方言, NHB可以针对数据库方言进行优化 -->
<add key="hibernate.dialect"
value
="NHibernate.Dialect.MsSql2000Dialect" />
<!—数据驱动对象 -->
<add key="hibernate.connection.driver_class"
value
="NHibernate.Driver.SqlClientDriver" />
<!—连接字符串, userid和password改成你自己的哦。 -->
<add key="hibernate.connection.connection_string"
value
="Server=localhost;initial catalog=northwind;user id=northwind;password=123456;Min Pool Size=2" />
</nhibernate>



再次运行测试,就可以看见绿色的条了。


在取得会话工厂的代码中,我使用了如下代码:


if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// build sessionfactory code;
}
}
// end lock
}






这是一个典型的double lock方式,用来产生线程安全的Singletion(单例)对象。


2. 基本CRUD操作
在很多介绍NHB的文章,包括NHB带的测试用例中,业务对象只是做为一个数据实体存在的,它没有任何操作!


这在java中是比较典型的作法。
而我希望我们的业务对象自身就能完成基本的Create/Retrieve/Update/Delete,即CRUD操作,
在罗斯文商贸应用中,存在客户(customer)业务对象,先来为它建立一个测试用例,


这是一个典型的double lock方式,用来产生线程安全的Singletion(单例)对象。


[TestFixture]
public class CustomerFixture {
   public CustomerFixture() {
   }


   [Test] // 测试Customer对象的CRUD操作。
   public void TestCRUD() {
      Customer c = new Customer();
      c.CustomerId = "test";
      c.CompanyName = "company name";
      c.ContactName = "contact name";
      c.Address = "address";
      c.Create(); // test create.


      Customer c2 = new Customer( c.CustomerId ); // test retrieve.
      Assert.AreEqual( c2.CompanyName, "company name", "save companyname fail! " );


      c2.CompanyName = "update name";
      c2.Update(); // test update.


      Customer c3 = new Customer( c.CustomerId )
      Assert.AreEqual( c3.CompanyName, "update name", "update companyname fail! " );


      c3.Delete(); // test delete.
   }
}


接下来创建Customer业务类,



public class Customer : BizObject {
public Customer() { }
public Customer( string existingId ) : base( existingId ) { }

#region persistent properties.

private string _customerId = string.Empty;
private string _companyName = string.Empty;
private string _contactName = string.Empty;
private string _contactTitle = string.Empty;
private string _address = string.Empty;
private string _city = string.Empty;
private string _region = string.Empty;
private string _postalCode = string.Empty;
private string _country = string.Empty;
private string _phone = string.Empty;
private string _fax = string.Empty;

public string CustomerId {
get { return _customerId; }
set { _customerId = value; }
}
public string CompanyName {
get { return _companyName; }
set { _companyName = value; }
}
public string ContactName {
get { return _contactName; }
set { _contactName = value; }
}
public string ContactTitle {
get { return _contactTitle; }
set { _contactTitle = value; }
}
public string Address {
get { return _address; }
set { _address = value; }
}
public string City {
get { return _city; }
set { _city = value; }
}
public string Region {
get { return _region; }
set { _region = value; }
}
public string PostalCode {
get { return _postalCode; }
set { _postalCode = value; }
}
public string Country {
get { return _country; }
set { _country = value; }
}
public string Phone {
get { return _phone; }
set { _phone = value; }
}
public string Fax {
get { return _fax; }
set { _fax = value; }
}

#endregion
}



在Customer类中,没有实现CRUD操作,这些操作在业务对象基类BizObject中实现,代码如下:



public class BizObject {
public BizObject() { }

public BizObject( object existingId ) {
ObjectBroker.Load(
this, existingId );
}
public virtual void Create() {
ObjectBroker.Create(
this );
}
public virtual void Update() {
ObjectBroker.Update(
this );
}
public virtual void Delete() {
ObjectBroker.Delete(
this );
}
}






BizObject简单的将数据操作转发至ObjectBroker类, 目的是为了降低业务层和NHB之间的耦合,


以利于持久层间的移植。


public class ObjectBroker {
private ObjectBroker() { }

public static void Load( object obj, object id ){
ISession s
= Sessions.GetSession();
try {
s.Load( obj, id );
}
finally {
s.Close();
}
}

public static void Create( object obj ) {
ISession s
= Sessions.GetSession();
ITransaction trans
= null;
try {
trans
= s.BeginTransaction();
s.Save( obj );
trans.Commit();
}
finally {
s.Close();
}
}

public static void Update( object obj ) {
ISession s
= Sessions.GetSession();
ITransaction trans
= null;
try {
trans
= s.BeginTransaction();
s.Update( obj );
trans.Commit();
}
finally {
s.Close();
}
}

public static void Delete( object obj ) {
ISession s
= Sessions.GetSession();
ITransaction trans
= null;
try {
trans
= s.BeginTransaction();
s.Delete( obj );
trans.Commit();
}
finally {
s.Close();
}
}
}





ObjectBroker对ISession进行了必要的封装,通过ISession,就可以简单的完成对象的CRUD操作了。


编译并运行测试,CustomerFixture的TestCRUD操作还是不能通过! 异常信息为:
NHibernateTest.Test.CustomerFixture.TestCRUD : NHibernate.ADOException : Could not save object
----> NHibernate.MappingException : No persisters for: NHibernateTest.Business.Customer
显然,是因为我们还没有为Customer对象编写映射文件,而导致NHB不能对Customer对象进行持久化操作。


Customer对象的映射文件(Customer.hbm.xml)内容如下:


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Business.Customer, NHibernateTest" table="Customers">
<id name="CustomerId" column="customerId" type="String" unsaved-value="">
<generator class="assigned"/>
</id>
<property name="CompanyName" column="companyName" type="String" />
<property name="ContactName" column="contactName" type="String" />
<property name="ContactTitle" column="contactTitle" type="String" />
<property name="Address” column=”address” type="String" />
<property name="City” column=”city” type=”String” />
<property name="Region” column=”region” type=”String” />
<property name="PostalCode” column=”postalCode” type=”String” />
<property name="Country” column=”country” type=”String” />
<property name="Phone” column=”phone” type=”String” />
<property name="Fax” column=”fax” type=”String” />
</class>
</hibernate-mapping>






这个映射文件算是NHB中较为简单的了。
class的name指定业务对象全名及其所在程序集,table指定数据表的名称;
id用于指定一个对象标识符(数据表中的主键)及其产生的方式, 常用的主健产生方式有自增型(identity)和赋值型(assigned),


这里使用了assigned,需要注意的是unsaved-value属性,它指定对象没有持久化时的Id值,主要用于SaveOrUpdate操作;
property用于指定其它映射的数据列;
在id和property中,name指定属性名称,column指定数据列的名称,type指定属性类型,注意这里的类型是NHB中的类型,


而不是.NET或数据库中的数据类型。


另外,对象映射文件名称请按”对象名.hbm.xml”的规范来命名, 最后在映射文件的属性中把操作改为“嵌入的资源“。
现在重新编译程序并运行测试,就能看到绿条了!


因为Product对象将在后面的案例中多次使用,在这里按与Customer相同的步骤创建它。



// Product单元测试
[TestFixture]
public class ProductFixture {
public ProductFixture() { }

[Test] // 测试Product对象的CRUD操作。
public void TestCRUD() {
Product p
= new Product();
p.ProductName
= "test";
p.QuantityPerUnit
= "1箱10只";
p.UnitPrice
= 10.5M;
p.Create();

Product p2 = new Product( p.ProductId );
p2.UnitPrice
= 15.8M;
p2.Update();

Product p3 = new Product( p.ProductId );
Assert.AreEqual( p3.UnitPrice,
15.8M, "update fail! " );

p3.Delete();
}
}

// Product对象
public class Product : BizObject {
public Product() : base() { }
public Product( int existingId ) : base( existingId ) { }

#region persistent properties

private int _productId = 0;
private string _productName = string.Empty;
private int _supplierId = 0; // 应使用many-to-one, 需要重构。
private int _categoryId = 0; // 应使用many-to-one, 需要重构。
private string _quantityPerUnit = string.Empty;
private decimal _unitPrice = 0;
private int _unitsInStock = 0;
private int _unitsOnOrder = 0;
private int _reorderLevel = 0;
private bool _discontinued = false;

public int ProductId {
get { return _productId; }
set { _productId = value; }
}
public string ProductName {
get { return _productName; }
set { _productName = value; }
}
public int SupplierId {
get { return _supplierId; }
set { _supplierId = value; }
}
public int CategoryId {
get { return _categoryId; }
set { _categoryId = value; }
}
public string QuantityPerUnit {
get { return _quantityPerUnit; }
set { _quantityPerUnit = value; }
}
public decimal UnitPrice {
get { return _unitPrice; }
set { _unitPrice = value; }
}
public int UnitsInStock {
get { return _unitsInStock; }
set { _unitsInStock = value; }
}
public int UnitsOnOrder {
get { return _unitsOnOrder; }
set { _unitsOnOrder = value; }
}
public int ReorderLevel {
get { return _reorderLevel; }
set { _reorderLevel = value; }
}
public bool Discontinued {
get { return _discontinued; }
set { _discontinued = value; }
}

#endregion
}

// 映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Business.Product, NHibernateTest" table="Products">
<id name="ProductId" column="productId" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="ProductName" column="ProductName" type="String" />
<property name="QuantityPerUnit" column="QuantityPerUnit" type="String" />
<property name="UnitPrice" column="unitPrice" type="Decimal" />

【上篇】
【下篇】

抱歉!评论已关闭.