本文約定:
1. Nhibernate簡(jiǎn)寫為NHB;
2. 本文例子的開發(fā)平臺(tái)為win2000pro+sp4, sql server2000, Nhibernate0.5;
3. 使用SQL Server自帶的羅斯文商貿(mào)數(shù)據(jù)庫(kù)(Northwind),是英文版的哦;
4. 本文例子是基于測(cè)試驅(qū)動(dòng)開發(fā)(TDD)的,因此建議使用NUnit和Log4Net
一 NHB簡(jiǎn)介
NHB是基于ms.NET的O/R Mapping持久框架,它從基于Java的hibernate項(xiàng)目移植而來(lái)。O/R Mapping就是把對(duì)象到映射關(guān)系數(shù)據(jù)庫(kù)的記錄,簡(jiǎn)單的說(shuō)就是能實(shí)現(xiàn)把一個(gè)對(duì)象存儲(chǔ)為數(shù)據(jù)表中的一條記錄和由一條記錄創(chuàng)建一個(gè)相應(yīng)的對(duì)象,數(shù)據(jù)表中的數(shù)據(jù)就是對(duì)象的屬性。
那么為什么要使用O/R Mapping?它與傳統(tǒng)的DataSet/DataTable又有什么不同了?
首先是設(shè)計(jì)上的不同,當(dāng)使用O/R Mapping時(shí),更多的是從對(duì)象的角度來(lái)設(shè)計(jì)程序,而把數(shù)據(jù)(對(duì)象的屬性)存儲(chǔ)的細(xì)節(jié)放在后面, 可以完全采用面向?qū)ο?OO)的方式來(lái)設(shè)計(jì),而在使用DataSet/DataTable時(shí),它只是存放數(shù)據(jù)的對(duì)象,看起來(lái)更像一個(gè)數(shù)據(jù)表,不能直觀的表達(dá)業(yè)務(wù)概念。
二 NHB中主要接口的介紹
ISession
ISession是面向用戶的主要接口,主要用于對(duì)象持久化,數(shù)據(jù)加載等操作,支持?jǐn)?shù)據(jù)庫(kù)事務(wù),它隱藏了NHB內(nèi)部復(fù)雜的實(shí)現(xiàn)細(xì)節(jié),ISession由ISessionFactory創(chuàng)建。
ISessionFactory
ISessionFactory是NHB內(nèi)部的核心類,它維護(hù)到持久機(jī)制(數(shù)據(jù)庫(kù))的連接并對(duì)它們進(jìn)行管理,同時(shí)還會(huì)保存所有持久對(duì)象的映射信息。
ISessionFactory由Configuration創(chuàng)建,因?yàn)閯?chuàng)建ISessionFactory的開銷非常大(需要加載映射信息),所以這個(gè)對(duì)象一般使用Singleton(單例)模式。
ITransaction
ITransaction是NHB的事務(wù)處理接口,它只是簡(jiǎn)單的封裝了底層的數(shù)據(jù)庫(kù)事務(wù)。
事務(wù)必須由ISession來(lái)啟動(dòng)。
ICriteria
ICriteria是Expression(表達(dá)式)數(shù)據(jù)加載接口,Expression是一個(gè)關(guān)系表達(dá)式組合,通過(guò)它能產(chǎn)生SQL語(yǔ)句的Where部分, 用戶需要通過(guò)ISession來(lái)間接調(diào)用它。
IQuery
IQuery是HQL數(shù)據(jù)加載接口,HQL(Hibernate Query Language)是NHB專用的面向?qū)ο蟮臄?shù)據(jù)查詢語(yǔ)言,它與數(shù)據(jù)庫(kù)的SQL有些類似,但功能更強(qiáng)大!同ICriteria一樣,也需要通過(guò)ISession來(lái)間接調(diào)用它。
三 持久化操作
1. 會(huì)話和會(huì)話工廠
要進(jìn)行持久化操作,必須先取得ISession和ISessionFactory,我們用一個(gè)Sessions類來(lái)封裝它們, Sessions類的屬性和方法都是靜態(tài)的,它有一個(gè)Factory屬性, 用于返回ISessionFactory, 有一個(gè)GetSession方法,用于取得一個(gè)新的ISession。
測(cè)試類代碼如下:
[TestFixture]
Public class SessionsFixture {
Public void SessionsFixture() {
}
[Test] // 測(cè)試能否取得NHB會(huì)話工廠。
public void FactoryTest() {
ISessionFactory sf = Sessions.Factory;
Assert.IsNotNull( sf, “get sessionfactory fail!” );
}
[Test] // 測(cè)試能否取得NHB會(huì)話。
public void GetSessionTest() {
ISession s = Sessions.GetSession();
Assert.IsNotNull( s, “get session fail!” );
}
}
現(xiàn)在還沒(méi)寫Sessions類,將不能通過(guò)編譯! 下面我們來(lái)實(shí)現(xiàn)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,現(xiàn)在編譯可以通過(guò)了,啟動(dòng)NUnit并選擇生成的文件NHibernateTest.exe,運(yùn)行測(cè)試。
我們得到了紅色的條,出錯(cuò)了!原來(lái)還沒(méi)有加入NHibernate的配置信息(當(dāng)使用NHibernate時(shí),需要在項(xiàng)目的配置文件中加入NHibernate的配置信息。關(guān)于配置信息,在下面有說(shuō)明)。
在項(xiàng)目的配置文件App.Config(如沒(méi)有請(qǐng)自行創(chuàng)建一個(gè))中加入以下內(nèi)容.
value="NHibernate.Connection.DriverConnectionProvider" />
value="NHibernate.Dialect.MsSql2000Dialect" />
value="NHibernate.Driver.SqlClientDriver" />
value="Server=localhost;initial catalog=northwind;user id=northwind;password=123456;Min Pool Size=2" />
再次運(yùn)行測(cè)試,就可以看見綠色的條了。
在取得會(huì)話工廠的代碼中,我使用了如下代碼:
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// build sessionfactory code;
}
} // end lock
}
這是一個(gè)典型的double lock方式,用來(lái)產(chǎn)生線程安全的Singletion(單例)對(duì)象。
2. 基本CRUD操作
在很多介紹NHB的文章,包括NHB帶的測(cè)試用例中,業(yè)務(wù)對(duì)象只是做為一個(gè)數(shù)據(jù)實(shí)體存在的,它沒(méi)有任何操作!這在java中是比較典型的作法。
而我希望我們的業(yè)務(wù)對(duì)象自身就能完成基本的Create/Retrieve/Update/Delete,即CRUD操作,
在羅斯文商貿(mào)應(yīng)用中,存在客戶(customer)業(yè)務(wù)對(duì)象,先來(lái)為它建立一個(gè)測(cè)試用例,
[TestFixture]
public class CustomerFixture {
public CustomerFixture() {
}
[Test] // 測(cè)試Customer對(duì)象的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.
}
}
接下來(lái)創(chuàng)建Customer業(yè)務(wù)類,
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類中,沒(méi)有實(shí)現(xiàn)CRUD操作,這些操作在業(yè)務(wù)對(duì)象基類BizObject中實(shí)現(xiàn),代碼如下:
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簡(jiǎn)單的將數(shù)據(jù)操作轉(zhuǎn)發(fā)至ObjectBroker類, 目的是為了降低業(yè)務(wù)層和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對(duì)ISession進(jìn)行了必要的封裝,通過(guò)ISession,就可以簡(jiǎn)單的完成對(duì)象的CRUD操作了。
編譯并運(yùn)行測(cè)試,CustomerFixture的TestCRUD操作還是不能通過(guò)! 異常信息為:
NHibernateTest.Test.CustomerFixture.TestCRUD : NHibernate.ADOException : Could not save object
----> NHibernate.MappingException : No persisters for: NHibernateTest.Business.Customer
顯然,是因?yàn)槲覀冞€沒(méi)有為Customer對(duì)象編寫映射文件,而導(dǎo)致NHB不能對(duì)Customer對(duì)象進(jìn)行持久化操作。
Customer對(duì)象的映射文件(Customer.hbm.xml)內(nèi)容如下:
這個(gè)映射文件算是NHB中較為簡(jiǎn)單的了。
class的name指定業(yè)務(wù)對(duì)象全名及其所在程序集,table指定數(shù)據(jù)表的名稱;
id用于指定一個(gè)對(duì)象標(biāo)識(shí)符(數(shù)據(jù)表中的主鍵)及其產(chǎn)生的方式, 常用的主健產(chǎn)生方式有自增型(identity)和賦值型(assigned),這里使用了assigned,需要注意的是unsaved-value屬性,它指定對(duì)象沒(méi)有持久化時(shí)的Id值,主要用于SaveOrUpdate操作;
property用于指定其它映射的數(shù)據(jù)列;
在id和property中,name指定屬性名稱,column指定數(shù)據(jù)列的名稱,type指定屬性類型,注意這里的類型是NHB中的類型,而不是.NET或數(shù)據(jù)庫(kù)中的數(shù)據(jù)類型。
另外,對(duì)象映射文件名稱請(qǐng)按”對(duì)象名.hbm.xml”的規(guī)范來(lái)命名, 最后在映射文件的屬性中把操作改為“嵌入的資源“。
現(xiàn)在重新編譯程序并運(yùn)行測(cè)試,就能看到綠條了!
因?yàn)镻roduct對(duì)象將在后面的案例中多次使用,在這里按與Customer相同的步驟創(chuàng)建它。
// Product單元測(cè)試
[TestFixture]
public class ProductFixture {
public ProductFixture() { }
[Test] // 測(cè)試Product對(duì)象的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對(duì)象
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; // 應(yīng)使用many-to-one, 需要重構(gòu)。
private int _categoryId = 0; // 應(yīng)使用many-to-one, 需要重構(gòu)。
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
}
// 映射文件
編譯并運(yùn)行測(cè)試,檢查錯(cuò)誤直到單元測(cè)試通過(guò)。
注意:因?yàn)樵跀?shù)據(jù)庫(kù)中,products表與categories表、suppliers表有外鍵約束,必須先刪除這兩個(gè)約束,product測(cè)試用例才能通過(guò),后面我們?cè)偌由线@兩個(gè)約束。
現(xiàn)在我們已經(jīng)掌握了NHB的基本CRUD操作了,整個(gè)過(guò)程應(yīng)該說(shuō)是比較簡(jiǎn)單吧。呵呵,不再需要使用Connection、DataAdapter、DataSet/DataReader之類的對(duì)象了,下面繼續(xù)學(xué)習(xí)NHB中更為復(fù)雜的映射關(guān)系。
3. one-to-one
一對(duì)一是一種常見的數(shù)據(jù)模型,它有兩種情況:一種是主鍵(PrimaryKey)關(guān)聯(lián);另一種是外健(ForeignKey)關(guān)聯(lián),在使用外健的時(shí)候要保證其唯一性。
在主鍵關(guān)聯(lián)的情況下, 必須有一個(gè)主鍵是根據(jù)別一個(gè)主鍵而來(lái)的。NHB是通過(guò)一種特殊的方式來(lái)處理這種情況的, 要注意兩個(gè)主健名稱必須同名,而外健關(guān)聯(lián)需要在one-to-one配置中定義一個(gè)property-ref屬性, 這個(gè)屬性在當(dāng)前版本的NHB中還沒(méi)有實(shí)現(xiàn)。
在羅斯文商貿(mào)應(yīng)用,不需要使用one-to-one映射,這里先不對(duì)其進(jìn)行講解,如欲了解one-to-one方面的應(yīng)用,請(qǐng)參考我網(wǎng)站上的文章。
4. many-to-one
many-to-one是描述多對(duì)一的一種數(shù)據(jù)模型,它指定many一方是不能獨(dú)立存在的,我個(gè)人認(rèn)為many-to-one是NHB中保證數(shù)據(jù)有效性的最有用的一種映射,通過(guò)使用many-to-one能有效的防治孤兒記錄被寫入到數(shù)據(jù)表中。
在羅斯文商貿(mào)數(shù)據(jù)中,Product(產(chǎn)品)與Category(類別)是多對(duì)一的關(guān)系。下面我們來(lái)處理這一映射關(guān)系,
首先要讓Category能實(shí)現(xiàn)基本的CRUD操作,步驟同上, 這里只列出測(cè)試用例,類和映射文件請(qǐng)參照上面的方式創(chuàng)建。
[TextFixture]
public class CategoryFixture {
public CategoryFixture() {
}
[Test] // 測(cè)試基本的CRUD操作。
public void TextCRUD() {
Category c = new Category();
c.CategoryName = “category1”;
c.Description = “category1”;
c.Create();
Category c2 = new Category(c.CategoryId);
c2.CategoryName = "test update";
c2.Update();
Category c3 = new Category( c.CategoryId);
Assert.AreEqual( c3.CategoryName, "test updated", "update fail! " );
c3.Delete();
}
}
上面的測(cè)試用例通過(guò)后,接著修改Product的各部分。
Product測(cè)試用例修改如下:
[Test] // 測(cè)試Product對(duì)象的CRUD操作。
public void TestCRUD() {
Category c = null;
try {
c.CategoryName = "test";
c.Create();
Product p = new Product();
p.ProductName = "test";
p.QuantityPerUnit = "1箱10只";
p.UnitPrice = 10.5M;
p.Category = c;
p.Create();
// 為省篇幅,下略...(不是刪除掉哦!)
}
finally {
if ( c != null && c.CategoryId > 0 ) c.Delete();
}
}
Product類做如下修改:
1. 刪除categoryId 字段和CategoryId屬性;
2. 加入以下代碼:
public Category Category {
get { return _category; }
set { _category = value; }
}
private Category _category;
Product映射文件做如下修改:
class="NHibernateTest.Business.Category, NHibernateTest" />
這里用到了一個(gè)many-to-one標(biāo)簽,用于指定與one的一方進(jìn)行關(guān)聯(lián)的對(duì)象信息。
name指定one一方在對(duì)象中的名稱;
column指定映射數(shù)據(jù)列的名稱;
unique指定one一方是唯一的;
class 指定one一方類的全名,包括程序集名稱;
重新編譯程序,運(yùn)行測(cè)試用例, 看到綠條了嗎?沒(méi)有就根據(jù)異常去除錯(cuò)吧!我已經(jīng)看到綠條了。:)
聲明: 因?yàn)檫^(guò)多的many-to-one使后面的測(cè)試代碼變得異常龐大(創(chuàng)建對(duì)象時(shí)要?jiǎng)?chuàng)建one一方的類),所以在后面的代碼中,我假定Product對(duì)象是沒(méi)有實(shí)現(xiàn)任何many-to-one映射的。如果不怕麻煩,請(qǐng)自行修改后面測(cè)試用例。
5. one-to-many
一對(duì)多也是一種常見的數(shù)據(jù)模型,在按范式設(shè)計(jì)的數(shù)據(jù)庫(kù)中隨處可見。在NHB中通過(guò)one-to-many可以非常方便的處理這種模型,同時(shí)NHB還提供了級(jí)聯(lián)更新和刪除的功能,以保證數(shù)據(jù)完整性。
在羅斯文商貿(mào)案例中,Customer與Order(訂單)、Order和OrderItem(訂單項(xiàng)目)就是一對(duì)多的關(guān)系,值得注意的是,并不是所有一對(duì)多關(guān)系都應(yīng)該在NHB中實(shí)現(xiàn),這取決于實(shí)際的需求情況,無(wú)謂的使用one-to-many映射只會(huì)降低NHB的使用性能。
下面先讓Order和OrderItem對(duì)象能單獨(dú)的完成CRUD操作,按上面處理Customer的方法來(lái)創(chuàng)建測(cè)試用例和類,
[TestFixture]
public class OrderFixture {
public OrderFixture() {
}
[Test] // 測(cè)試Order對(duì)象的CRUD操作。
public void TestCRUD() {
Order o = new Order();
o.CustomerId = 1;
o.EmployeeId = 1;
o.ShippedDate = new DateTime( 2005, 3, 5 );
o.ShipVia = 1;
o.Freight = 20.5M;
o.ShipName = "test name";
o.ShipAddress = "test address";
o.Create();
Order o2 = new Order( o.OrderId );
o2.Freight = 21.5M;
o2.ShipAddress = "update address";
o2.Update();
Order o3 = new Order( o.OrderId );
Assert.AreEqual( o3.Freight, 21.5M, "update order fail! " );
Assert.AreEqual( o3.ShipAddress, "update address", "update order fail! " );
o3.Delete();
}
[Test] // 測(cè)試OrderItem對(duì)象的CRUD操作。
public void TestItemCRUD() {
OrderItem item = new OrderItem();
item.OrderId = 1;
item.ProductId = 1;
item.UnitPrice = 10.5M;
item.Quantity = 12;
item.Discount = 1;
item.Create();
OrderItem item2 = new OrderItem( item.ItemId );
item2.Quantity = 13;
item2.Update();
OrderItem item3 = new OrderItem( item.ItemId );
Assert.AreEqual( item3.Quantity, 13, "update orderitem fail! " );
item3.Delete();
}
}
// Order
public class Order : BizObject {
public Order() : base() { }
public Order( int existingId ) : base( existingId ) { }
#region persistent properties
private int _orderId = 0;
private string _customerId = string.Empty; // 應(yīng)使用many-to-one, 需要重構(gòu)
private int _employeeId = 0; // 應(yīng)使用many-to-one, 需要重構(gòu)
private DateTime _orderDate = DateTime.Now;
private DateTime _requiredDate;
private DateTime _shippedDate;
private int _shipVia = 0; // 應(yīng)使用many-to-one, 需要重構(gòu)
private Decimal _freight = 0;
private string _shipName = string.Empty;
private string _shipAddress = string.Empty;
private string _shipCity = string.Empty;
private string _shipRegion = string.Empty;
private string _shipPostalCode = string.Empty;
private string _shipCountry = string.Empty;
public int OrderId {
get { return _orderId; }
set { _orderId = value; }
}
public string CustomerId {
get { return _customerId; }
set { _customerId = value; }
}
public int EmployeeId {
get { return _employeeId; }
set { _employeeId = value; }
}
public DateTime OrderDate {
get { return _orderDate; }
set { _orderDate = value; }
}
public DateTime RequiredDate {
get { return _requiredDate; }
set { _requiredDate = value; }
}
public DateTime ShippedDate {
get { return _shippedDate; }
set { _shippedDate = value; }
}
public int ShipVia {
get { return _shipVia; }
set { _shipVia = value; }
}
public Decimal Freight {
get { return _freight; }
set { _freight = value; }
}
public string ShipName {
get { return _shipName; }
set { _shipName = value; }
}
public string ShipAddress {
get { return _shipAddress; }
set { _shipAddress = value; }
}
public string ShipCity {
get { return _shipCity; }
set { _shipCity = value; }
}
public string ShipRegion {
get { return _shipRegion; }
set { _shipRegion = value; }
}
public string ShipPostalCode {
get { return _shipPostalCode; }
set { _shipPostalCode = value; }
}
public string ShipCountry {
get { return _shipCountry; }
set { _shipCountry = value; }
}
#endregion
}
// Order映射文件
// OrderItem
public class OrderItem : BizObject {
public OrderItem() : base() { }
public OrderItem( int existingId ) : base( existingId ) { }
#region persistent properties
private int _itemId = 0;
private int _orderId = 0; // many-to-one, 需要重構(gòu).
private int _productId = 0; // many-to-one, 需要重構(gòu).
private decimal _unitPrice = 0;
private int _quantity = 0;
private double _discount = 0.0;
public int ItemId {
get { return _itemId; }
set { _itemId = value; }
}
public int OrderId {
get { return _orderId; }
set { _orderId = value; }
}
public int ProductId {
get { return _productId; }
set { _productId = value; }
}
public decimal UnitPrice {
get { return _unitPrice; }
set { _unitPrice = value; }
}
public int Quantity {
get { return _quantity; }
set { _quantity = value; }
}
public double Discount {
get { return _discount; }
set { _discount = value; }
}
#endregion
}
// OrderItem映射文件
因?yàn)樵O(shè)計(jì)上的原因(業(yè)務(wù)對(duì)象必須為單主健),我們向OrderDetails加入一個(gè)ItemId字段,這是一個(gè)自增型的主健,以代替原來(lái)的聯(lián)合主健。
編譯并運(yùn)行測(cè)試,檢查錯(cuò)誤直到單元測(cè)試全部通過(guò)。
注意,在數(shù)據(jù)庫(kù)中要進(jìn)行如下修改,測(cè)試用例才能通過(guò)
1.orders表與customers表、employeess表和shippers表有外鍵約束,必須暫時(shí)將它們刪除,后面我們?cè)偌由线@三個(gè)約束;
2.將Order Details改名為OrderDetails,表名中出現(xiàn)空格將導(dǎo)致NHB無(wú)法解析;
3.orderDetails表與orders表、products表有外健約束,必須暫時(shí)將它們刪除,下面我們將加上這些約束。
現(xiàn)在開始重構(gòu)OrderItem的測(cè)試用例和對(duì)象,主要是加入many-to-one映射。
先將TestItemCRUD修改為如下:
[Test] // 測(cè)試OrderItem對(duì)象的CRUD操作。
public void TestItemCRUD() {
Order o = null;
Product p = null;
try {
Order o = new Order();
o.CustomerId = "AA001";
o.EmployeeId = 1;
o.Create();
Product p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.Create();
OrderItem item = new OrderItem();
// item.OrderId = 1;
item.Order = o;
// item.ProductId = 1;
item.Product = p;
item.UnitPrice = 10.5M;
item.Quantity = 12;
item.Discount = 1;
item.Create();
OrderItem item2 = new OrderItem( item.ItemId );
item2.Quantity = 13;
item2.Update();
OrderItem item3 = new OrderItem( item.ItemId );
Assert.AreEqual( item3.Quantity, 13, "update orderitem fail! " );
item3.Delete();
}
finally {
if ( o != null && o.OrderId > 0 ) o.Delete();
if ( p != null && p.ProductId > 0 ) p.Delete();
}
}
接下來(lái)修改OrderItem對(duì)象,改動(dòng)如下:
// private int _orderId = 0; // many-to-one, 需要重構(gòu).
private Order Order;
// private int _productId = 0; // many-to-one, 需要重構(gòu).
private Product Product;
刪除OrderId和ProductId屬性,并加入以下屬性:
public Order Order {
get { return _order; }
set { _order = value; }
}
public Product Product {
get { return _product; }
set { _product = value; }
}
編譯項(xiàng)目,確保其能通過(guò),如出現(xiàn)錯(cuò)誤,請(qǐng)檢查是否有拼寫錯(cuò)誤。
最后修改OrderItem對(duì)象的映射文件,改動(dòng)如下:
class=”NHibernateTest.Business.Order, NHibernateTest” />
class=”NHibernateTest.Business.Product, NHibernateTest” />
按many-to-one一節(jié)中的說(shuō)明對(duì)Order和Product對(duì)象設(shè)置many-to-one關(guān)聯(lián)。
重新生成項(xiàng)目,以使改動(dòng)的資源編譯進(jìn)程序集中,運(yùn)行TestItemCRUD測(cè)試用例,這時(shí)應(yīng)能得到一個(gè)綠條,如果是紅條,請(qǐng)根據(jù)錯(cuò)誤信息進(jìn)行檢查。
接下來(lái)重構(gòu)Order測(cè)試用例和對(duì)象。
先添加一個(gè)TestOrderItem用例如下:
[Test]
public void TestOrderItem() {
Product p = null;
Product p2 = null;
try {
p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.Create();
p2 = new Product();
p2.ProductName = "test2";
p2.UnitPrice = 12.2M;
p2.Create();
Order o = new Order();
o.CustomerId = "AA001";
o.EmployeeId = 1;
o.Create();
OrderItem item = new OrderItem();
item.Product = p;
item.UnitPrice = 10;
item.Quantity = 5;
OrderItem item2 = new OrderItem();
item2.Product = p2;
item2.UnitPrice = 11;
item2.Quantity = 4;
o.AddItem( item );
o.AddItem( item2 );
o.Create();
Order o2 = new Order( o.OrderId );
Assert.IsNotNull( o2.Items, "add item fail! " );
Assert.AreEqual( o2.Items.Count, 2, "add item fail! " );
IEnumerator e = o2.Items.GetEnumerator();
e.MoveNext();
OrderItem item3 = e.Current as OrderItem;
o2.RemoveItem( item3 );
o2.Update();
Order o3 = new Order( o.OrderId );
Assert.AreEqual( o3.Items.Count, 1, "remove item fail! " );
o3.Delete();
}
finally {
if (p!=null && p.ProductId > 0) p.Delete();
if (p2!=null && p2.ProductId >0) p2.Delete();
}
}
在這個(gè)測(cè)試用例中,我們?cè)贠rder對(duì)象中封裝了對(duì)OrderItem的添加和移除操作,提供一個(gè)ICollection類型的屬性Items用于遍歷OrderItem。
接著修改Order對(duì)象,要添加兩個(gè)方法和一個(gè)屬性、一些輔助字段.
public void AddItem( OrderItem item ) {
if ( _items == null ) _items = new ArrayList();
item.Order = this;
_items.Add( item );
}
public void RemoveItem( OrderItem item ) {
_items.Remove( item );
}
public ICollection Items {
return _items;
}
protected IList _Items {
get { return _items; }
set { _items = value; }
}
private IList _items;
在上面加入的代碼中,有個(gè)protected修飾的_Items屬性,它是用于one-to-many映射的,由NHB使用。
最后修改Order對(duì)象的映射文件,加入以下one-to-many代碼:
這里又用到了一個(gè)新的標(biāo)簽bag, bag用于集合映射,在NHB中還有set, list等,它們的元素大致相同,但對(duì)應(yīng)的.NET集合對(duì)象卻是不一樣的,后面對(duì)它們進(jìn)行詳細(xì)的說(shuō)明和比較。
bag屬性用于指定集合的名稱和級(jí)聯(lián)操作的類型;
key元素指定關(guān)聯(lián)的數(shù)據(jù)列名稱;
one-to-many指定many一方類的全名,包括程序集名稱。
再次編譯項(xiàng)目并運(yùn)行測(cè)試用例,我得到了一個(gè)這樣的診斷錯(cuò)誤信息:
NHibernateTest.Test.OrderFixture.TestOrderItem : remove item fail!
expected:<2>
but was:<1>
從源代碼可以得知,當(dāng)執(zhí)行Update操作時(shí),級(jí)聯(lián)操作并不會(huì)刪除我們移除的子對(duì)象,必須自行刪除!級(jí)聯(lián)刪除只是指刪除父對(duì)象的時(shí)候同時(shí)刪除子對(duì)象。
修改TestOrderItem測(cè)試用例代碼如下:
o2.Update(); // 此行不變
item3.Delete(); // 加入此行代碼
6. element
集合element是一種處理多對(duì)多的映射,多對(duì)多在數(shù)據(jù)庫(kù)中也是常見的數(shù)據(jù)模型,像用戶與組,用戶與權(quán)限等。多對(duì)多關(guān)系需要通過(guò)一個(gè)中間表實(shí)現(xiàn),element的就是讀取這個(gè)中間表中某列的值。
在羅斯文商貿(mào)應(yīng)用中,Employee和Territory之間是多對(duì)多的關(guān)系,它們通過(guò)EmployeeTerritories表進(jìn)行關(guān)聯(lián)。 有關(guān)Employee和Territory對(duì)象的代碼、測(cè)試用例和映射文件請(qǐng)自行參照上面的方法創(chuàng)建,這里就不列出代碼了,下面只列出測(cè)試element映射的部分。
[TestFixture]
public class EmployeeFixture() {
// other test....
[test]
public TerritoryElementTest() {
Employee e = new Employee();
e.FirstName = “first”;
e.LastName = “l(fā)ast”;
e.AddTerritory( 1000 );
e.AddTerritory( 1001 );
e.Create();
Employee e2 = new Employee( e.EmployeeId );
Assert.IsNotNull( e2.Territories, “add territory fail!” );
Assert.AreEqual( e2.Territories.Count, 2, “add territory fail!” );
e2.RemoveTerritory( 1000 );
e2.Update();
Employee e3 = new Employee( e.EmployeeId );
Assert.AreEqual( e3.Territories.Count, 1, “remove territory fail!” );
e3.Delete();
}
}
在上面的代碼中,我們給Employee添加兩個(gè)方法和一個(gè)屬性,這和one-to-many一節(jié)中介紹的處理方法是相似的。
在Employee類中,要添加如下代碼:
public class Employee {
// other fields , properties, method...
public void AddTerritory( int territoryId ) {
if ( _territory == null ) _territory = new ArrayList();
_territory.Add( territoryId );
}
public void RemoveTerritory( int territoryId ) {
_territory.Remove( teritoryId );
}
public ICollection Territories {
get { return _territory; }
}
protected IList _Territories {
get { return _territories; }
set { _territories = value; }
}
}
最后修改Employee對(duì)象的映射文件,加入以下內(nèi)容:
在bag標(biāo)簽中,加入了一個(gè)table屬性,它指定一個(gè)實(shí)現(xiàn)多對(duì)多的中間表。在element元素中,指定要讀取的列名及其類型。
四 數(shù)據(jù)加載
1. Expression
Expression數(shù)據(jù)加載由ICriteria接口實(shí)現(xiàn), ICriteria在程序中是無(wú)法直接構(gòu)造的,必須通過(guò)ISession.CreateCriteria(type)來(lái)獲得。ICriteria主要負(fù)責(zé)存儲(chǔ)一組Expression對(duì)象和一組Order對(duì)象,當(dāng)調(diào)用List執(zhí)行查詢時(shí),ICriteria對(duì)Expression對(duì)象和Order對(duì)象進(jìn)行組合以產(chǎn)生NHB內(nèi)部的查詢語(yǔ)句,然后交由DataLoader(數(shù)據(jù)加載器)來(lái)讀取滿足條件的記錄。
下面列出ICriteria接口中的一些常用方法:
Add:加入條件表達(dá)式(Expression對(duì)象),此方法可多次調(diào)用以組合多個(gè)條件;
AddOrder:加入排序的字段(Order對(duì)象);
List:執(zhí)行查詢, 返回滿足條件的對(duì)象集合。
SetMaxResults:設(shè)置返回的最大結(jié)果數(shù),可用于分頁(yè);
SetFirstResult:設(shè)置首個(gè)對(duì)象返回的位置,可用于分頁(yè);
通過(guò)SetMaxResults和SetFirstResult方法,就可以取得指定范圍段的記錄,相當(dāng)于是分頁(yè),
!!! 要說(shuō)明的是,對(duì)于SQL Server數(shù)據(jù)庫(kù),它是使用將DataReader指針移到firstResult位置,再讀取maxResults記錄的方式來(lái)實(shí)現(xiàn)分頁(yè)的,在數(shù)據(jù)量非常大(10w以上)的情況下,性能很難保證。
所有表達(dá)式對(duì)象都繼承之Expression類,這是一個(gè)抽象(abstract)類, 同時(shí)也是一個(gè)類工廠(Factory Method模式), 用于創(chuàng)建派生的Expression對(duì)象,這樣就隱藏了派生類的細(xì)節(jié)。(又學(xué)到一招了吧!)
下面列出幾個(gè)常用的Expression對(duì)象:
EqExpression :相等判斷的表達(dá)式, 等同于 propertyName = value,由Expression.Eq取得;
GtExpression :大于判斷的表達(dá)式, 等同于 propertyName > value,由Expression.Gt取得;
LikeExpression :相似判斷的表達(dá)式, 等同于 propertyName like value,由Expression.Like取得;
AndExpression :對(duì)兩個(gè)表達(dá)式進(jìn)行And操作, 等同于 expr1 and expr2,由Expression.And取得;
OrExpression :對(duì)兩個(gè)表達(dá)式進(jìn)行Or操作, 等同于 expr1 or expr2,由Expression.Or取得;
更多的Expression對(duì)象請(qǐng)參考相關(guān)文檔或源代碼。
Order對(duì)象用于向ICriteria接口提供排序信息,這個(gè)類提供了兩個(gè)靜態(tài)方法,分別是Asc和Desc,顧名思義就是創(chuàng)建升序和降序的Order對(duì)象,例如要取得一個(gè)按更新日期(Updated)降序的Order對(duì)象, 使用Order.Desc("Updated")就可以了。
下面以加載Customer數(shù)據(jù)為例來(lái)說(shuō)明Expression的使用:
測(cè)試代碼如下,
[TestFixture]
public class CustomerSystemFixture {
public CustomerSystemFixture() {
}
[Test]
public void LoadByNameTest() {
Expression expr = Expression.Eq( “CompanyName”, “company name” );
IList custs = ObjectLoader.Find( expr, typeof(Customer) );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNamePagerTest() {
Expression expr = Expression.Eq( “CompanyName”, “company name” );
PagerInfo pi = new PagerInfo( 0, 5 );
IList custs = ObjectLoader.Find( expr, typeof(Customer) , pi );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameAndAddressTest() {
Expression expr = Expression.Eq( “CompanyName”, “company name” );
Expression expr2 = Expression.Eq( “Address”, “address” );
IList custs = ObjectLoader.Find( Expression.And(expr, expr2), typeof(Customer) );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameOrAddressTest() {
Expression expr = Expression.Eq( “CompanyName”, “company name” );
Expression expr2 = Expression.Eq( “Address”, “address” );
IList custs = ObjectLoader.Find( Expression.Or(expr, expr2), typeof(Customer) );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
}
在上面的代碼中,給出了四個(gè)較簡(jiǎn)單的表達(dá)式加載的測(cè)試用例,它們都通過(guò)調(diào)用ObjectLoader對(duì)象的Find方法來(lái)取得數(shù)據(jù),ObjectLoader是我們自己的數(shù)據(jù)加載器,它簡(jiǎn)單的封裝了NHB中的數(shù)據(jù)加載功能。另外,我們還用一個(gè)PagerInfo類封裝了分頁(yè)數(shù)據(jù),以方便數(shù)據(jù)傳遞。
// PagerInfo
public class PagerInfo {
private int firstResult; // 起始位置
private int maxReuslts; // 返回最大記錄數(shù)
public PagerInfo( int firstResult, int maxResults ) {
this.firstResult = firstReuslt;
this.maxResults = maxResults;
}
public int FirstResult {
get { return firstResult; }
}
public int maxResults {
get { return maxResults; }
}
}
// ObjectLoader
public class ObjectLoader {
private ObjectLoader() {
}
public static IList Find( Expression expr, Type type ) {
return Find( expr, type, null );
}
public static IList Find( Expression expr, Type type, PagerInfo pi ) {
ISession s = Sessions.GetSession();
try {
ICriteria c = s.CreateCriteria( type );
if ( ex != null ) c.Add( ex );
if ( pi != null ) {
c.SetFirstResult( pi.FirstResult );
c.SetMaxResults( pi.MaxResults );
}
return c.List();
}
finally {
s.Close();
}
}
}
在Find方法中,先通過(guò)會(huì)話取得ICriteria接口, 然后加入表達(dá)式,接著檢查是否需要設(shè)置分頁(yè)數(shù)據(jù),最后返回列出的數(shù)據(jù)。
2. HQL
HQL(Hibernate Query Language)是NHB的專用查詢語(yǔ)言,它完全面向?qū)ο螅【褪钦f(shuō)只需要知道對(duì)象名和屬性名就可以生成HQL了,這樣就再也不用去理會(huì)數(shù)據(jù)表和列了,前面說(shuō)的Expression查詢最終也會(huì)轉(zhuǎn)換為HQL。
有兩種方式來(lái)執(zhí)行HQL,一種是直接使用ISession的Find方法,另一種是使用IQuery接口。IQuery接口提供了一些額外的設(shè)置,最重要的就是分頁(yè)了,這個(gè)和ICriteria差不多,另外一些就是設(shè)置參數(shù)的值了。
NHB中有一組類專門用于完成數(shù)據(jù)加載,它們分別對(duì)應(yīng)不同的數(shù)據(jù)加載情況,如實(shí)體加載、Criteria加載、OneToMany加載等。
下面同樣以加載Customer數(shù)據(jù)為例來(lái)說(shuō)明HQL的使用:
在上面的CustomerSystemFixture類中加入以下幾個(gè)測(cè)試用例:
public class CustomerSystemFixture {
[Test]
public void LoadAllTest () {
string query = “ from Customer “;
IList custs = ObjectLoader.Find( query, null );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadPagerDataTest() {
string query = “ from Customer “;
PagerInfo pi = new PagerInfo( 0, 5 );
IList custs = ObjectLoader.Find( query, null, pi );
}
[Test]
public void LoadByName2Test() {
string query = “ from Customer c where c.CompanyName = :CompanyName “;
paramInfos.Add( new ParameterInfo(“CompanyName”, “test name”, ??????TypeFactory.GetStringType()) );
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameAndAddress2Test() {
string query = “ from Customer c where c.CompanyName = :CompanyName and c.Address = :Address“;
paramInfos.Add( new ParameterInfo(“CompanyName”, “test name”, ??????TypeFactory.GetStringType()) );
paramInfos.Add( new ParameterInfo(“Address”, “test address”, TypeFactory.GetStringType()) );
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
}
在上面的測(cè)試用例中,我們同樣將數(shù)據(jù)加載交由ObjectLoader的Find方法來(lái)處理,F(xiàn)ind有很多重載的版本,都用于數(shù)據(jù)加載。另外還使用了一個(gè)ParameterInfo類來(lái)存儲(chǔ)HQL語(yǔ)句的參數(shù)信息。
// ParamInfo
public class ParamInfo {
private string name; // 參數(shù)名稱
private object value; // 參數(shù)值
private IType type; // 參數(shù)類型
public ParamInfo( string name, object value, IType type ) {
this.name = name;
this.value = value;
this.type = type;
}
public string Name {
get { return name; }
}
public object Value {
get { return value; }
}
public IType Type {
get { return type; }
}
} //class ParamInfo
向ObjectLoader類加入以下方法:
public class ObjectLoader {
// ....
public static IList Find( string query, ICollection paramInfos ) {
return Find( query, paramInfos, null );
}
public static IList Find( string query, ICollection paramInfos, PagerInfo pi ) {
ISession s = Sessions.GetSession();
try {
IQuery q = s.CreateQuery( query );
if ( paramInfos != null ) {
foreach ( ParamInfo info in paramInfos ) {
if ( info.Value is ICollection )
q.SetParameterList( info.Name, (ICollection)info.Value, info.Type );
else
q.SetParameter( info.Name, info.Value, info.Type );
}
}
if ( pi != null ) {
q.SetFirstResult( pi.FirstResult );
q.SetMaxResults( pi.MaxResults );
}
return q.List();
}
finally {
s.Close();
}
}
}
在上面的Find方法中,通過(guò)HQL語(yǔ)句創(chuàng)建一個(gè)IQuery, 然后加入?yún)?shù),接著設(shè)置分頁(yè)數(shù)據(jù),最后返回列出的數(shù)據(jù)。
五 事務(wù)
既然而數(shù)據(jù)庫(kù)打交道,那么事務(wù)處理就是必需的,事務(wù)能保整數(shù)據(jù)完整性。在NHB中,ITransaction對(duì)象只對(duì).NET的事務(wù)對(duì)象(實(shí)現(xiàn)了IDbTransaction接口的對(duì)象)進(jìn)行了簡(jiǎn)單的封裝。
使用NHB的典型事務(wù)處理看起來(lái)像下面這樣(見ISession.cs的注釋)
ISession sess = factory.OpenSession();
Transaction tx;
try {
tx = sess.BeginTransaction();
//do some work
//...
tx.Commit();
}
catch (Exception e) {
if (tx != null) tx.Rollback();
throw e;
}
finally {
sess.Close();
}
事務(wù)對(duì)象由ISession的BeginTransaction取得,同時(shí)事務(wù)開始,如果執(zhí)行順利則提交事務(wù),否則回滾事務(wù)。
當(dāng)實(shí)現(xiàn)一個(gè)業(yè)務(wù)規(guī)則時(shí),而這一規(guī)則要改變多個(gè)業(yè)務(wù)對(duì)象狀態(tài)時(shí),這時(shí)就需要使用事務(wù)了,事務(wù)能保證所有改變要么全部保存,要么全部不保存!
在羅斯文商貿(mào)案例中,有這樣一個(gè)業(yè)務(wù)規(guī)則:
如果客戶對(duì)某一產(chǎn)品下了訂單,那么被訂購(gòu)產(chǎn)品的已訂購(gòu)數(shù)量(UnitsOnOrder)應(yīng)該加上客戶的產(chǎn)品訂單量,根據(jù)這一業(yè)務(wù)規(guī)則,創(chuàng)建一個(gè)測(cè)試用例如下:
[TestFixture]
public class OrderFixture {
[Test]
public void OrderTest() {
Product p = new Product();
p.UnitsOnOdrer = 20;
p.Create();
Order o = new Order();
OrderItem item = new OrderItem();
item.Order = o;
item.Product = p;
item.OrderNum = 10;
OrderCO.Create( o );
Product p2 = new Product( p.ProductId );
Assert.AreEqual( p2.UnitsOnOrder, 30, “add to unitsonorder fail!” );
}
}