本文約定:
1. Nhibernate簡寫為NHB;
2. 本文例子的開發(fā)平臺為win2000pro+sp4, sql server2000, Nhibernate0.5;
3. 使用SQL Server自帶的羅斯文商貿(mào)數(shù)據(jù)庫(Northwind)滋早,是英文版的哦;
4. 本文例子是基于測試驅(qū)動開發(fā)(TDD)的覆积,因此建議使用NUnit和Log4Net
一 NHB簡介
NHB是基于ms.NET的O/R Mapping持久框架哲嘲,它從基于Java的hibernate項目移植而來星岗。O/R Mapping就是把對象到映射關(guān)系數(shù)據(jù)庫的記錄,簡單的說就是能實現(xiàn)把一個對象存儲為數(shù)據(jù)表中的一條記錄和由一條記錄創(chuàng)建一個相應(yīng)的對象,數(shù)據(jù)表中的數(shù)據(jù)就是對象的屬性。
那么為什么要使用O/R Mapping?它與傳統(tǒng)的DataSet/DataTable又有什么不同了油吭?
首先是設(shè)計上的不同,當(dāng)使用O/R Mapping時署拟,更多的是從對象的角度來設(shè)計程序婉宰,而把數(shù)據(jù)(對象的屬性)存儲的細(xì)節(jié)放在后面, 可以完全采用面向?qū)ο?OO)的方式來設(shè)計,而在使用DataSet/DataTable時推穷,它只是存放數(shù)據(jù)的對象心包,看起來更像一個數(shù)據(jù)表,不能直觀的表達(dá)業(yè)務(wù)概念馒铃。
二 NHB中主要接口的介紹
ISession
ISession是面向用戶的主要接口蟹腾,主要用于對象持久化,數(shù)據(jù)加載等操作区宇,支持?jǐn)?shù)據(jù)庫事務(wù)娃殖,它隱藏了NHB內(nèi)部復(fù)雜的實現(xiàn)細(xì)節(jié),ISession由ISessionFactory創(chuàng)建议谷。
ISessionFactory
ISessionFactory是NHB內(nèi)部的核心類炉爆,它維護(hù)到持久機(jī)制(數(shù)據(jù)庫)的連接并對它們進(jìn)行管理,同時還會保存所有持久對象的映射信息卧晓。
ISessionFactory由Configuration創(chuàng)建芬首,因為創(chuàng)建ISessionFactory的開銷非常大(需要加載映射信息),所以這個對象一般使用Singleton(單例)模式逼裆。
ITransaction
ITransaction是NHB的事務(wù)處理接口郁稍,它只是簡單的封裝了底層的數(shù)據(jù)庫事務(wù)。
事務(wù)必須由ISession來啟動胜宇。
ICriteria
ICriteria是Expression(表達(dá)式)數(shù)據(jù)加載接口耀怜,Expression是一個關(guān)系表達(dá)式組合恢着,通過它能產(chǎn)生SQL語句的Where部分, 用戶需要通過ISession來間接調(diào)用它。
IQuery
IQuery是HQL數(shù)據(jù)加載接口财破,HQL(Hibernate Query Language)是NHB專用的面向?qū)ο蟮臄?shù)據(jù)查詢語言然评,它與數(shù)據(jù)庫的SQL有些類似,但功能更強(qiáng)大狈究!同ICriteria一樣,也需要通過ISession來間接調(diào)用它盏求。
三 持久化操作
1. 會話和會話工廠
要進(jìn)行持久化操作抖锥,必須先取得ISession和ISessionFactory,我們用一個Sessions類來封裝它們碎罚, Sessions類的屬性和方法都是靜態(tài)的磅废,它有一個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!” );
}
}
現(xiàn)在還沒寫Sessions類憔购,將不能通過編譯! 下面我們來實現(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)在編譯可以通過了,啟動NUnit并選擇生成的文件NHibernateTest.exe,運行測試玫鸟。
我們得到了紅色的條导绷,出錯了!原來還沒有加入NHibernate的配置信息(當(dāng)使用NHibernate時屎飘,需要在項目的配置文件中加入NHibernate的配置信息妥曲。關(guān)于配置信息,在下面有說明)钦购。
在項目的配置文件App.Config(如沒有請自行創(chuàng)建一個)中加入以下內(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" />
再次運行測試檐盟,就可以看見綠色的條了。
在取得會話工廠的代碼中押桃,我使用了如下代碼:
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// build sessionfactory code;
}
} // end lock
}
這是一個典型的double lock方式葵萎,用來產(chǎn)生線程安全的Singletion(單例)對象。
2. 基本CRUD操作
在很多介紹NHB的文章怨规,包括NHB帶的測試用例中陌宿,業(yè)務(wù)對象只是做為一個數(shù)據(jù)實體存在的,它沒有任何操作波丰!這在java中是比較典型的作法壳坪。
而我希望我們的業(yè)務(wù)對象自身就能完成基本的Create/Retrieve/Update/Delete,即CRUD操作掰烟,
在羅斯文商貿(mào)應(yīng)用中爽蝴,存在客戶(customer)業(yè)務(wù)對象沐批,先來為它建立一個測試用例,
[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.
}
}
接下來創(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類中九孩,沒有實現(xiàn)CRUD操作,這些操作在業(yè)務(wù)對象基類BizObject中實現(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簡單的將數(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對ISession進(jìn)行了必要的封裝,通過ISession,就可以簡單的完成對象的CRUD操作了梅惯。
編譯并運行測試宪拥,CustomerFixture的TestCRUD操作還是不能通過! 異常信息為:
NHibernateTest.Test.CustomerFixture.TestCRUD : NHibernate.ADOException : Could not save object
----> NHibernate.MappingException : No persisters for: NHibernateTest.Business.Customer
顯然,是因為我們還沒有為Customer對象編寫映射文件铣减,而導(dǎo)致NHB不能對Customer對象進(jìn)行持久化操作她君。
Customer對象的映射文件(Customer.hbm.xml)內(nèi)容如下:
這個映射文件算是NHB中較為簡單的了。
class的name指定業(yè)務(wù)對象全名及其所在程序集葫哗,table指定數(shù)據(jù)表的名稱缔刹;
id用于指定一個對象標(biāo)識符(數(shù)據(jù)表中的主鍵)及其產(chǎn)生的方式, 常用的主健產(chǎn)生方式有自增型(identity)和賦值型(assigned),這里使用了assigned劣针,需要注意的是unsaved-value屬性校镐,它指定對象沒有持久化時的Id值,主要用于SaveOrUpdate操作捺典;
property用于指定其它映射的數(shù)據(jù)列灭翔;
在id和property中,name指定屬性名稱辣苏,column指定數(shù)據(jù)列的名稱肝箱,type指定屬性類型,注意這里的類型是NHB中的類型稀蟋,而不是.NET或數(shù)據(jù)庫中的數(shù)據(jù)類型煌张。
另外,對象映射文件名稱請按”對象名.hbm.xml”的規(guī)范來命名, 最后在映射文件的屬性中把操作改為“嵌入的資源“退客。
現(xiàn)在重新編譯程序并運行測試骏融,就能看到綠條了!
因為Product對象將在后面的案例中多次使用萌狂,在這里按與Customer相同的步驟創(chuàng)建它档玻。
// 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; // 應(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
}
// 映射文件
編譯并運行測試,檢查錯誤直到單元測試通過务傲。
注意:因為在數(shù)據(jù)庫中凉当,products表與categories表枣申、suppliers表有外鍵約束,必須先刪除這兩個約束看杭,product測試用例才能通過忠藤,后面我們再加上這兩個約束。
現(xiàn)在我們已經(jīng)掌握了NHB的基本CRUD操作了楼雹,整個過程應(yīng)該說是比較簡單吧模孩。呵呵,不再需要使用Connection贮缅、DataAdapter瓜贾、DataSet/DataReader之類的對象了,下面繼續(xù)學(xué)習(xí)NHB中更為復(fù)雜的映射關(guān)系携悯。
3. one-to-one
一對一是一種常見的數(shù)據(jù)模型,它有兩種情況:一種是主鍵(PrimaryKey)關(guān)聯(lián)筷笨;另一種是外健(ForeignKey)關(guān)聯(lián)憔鬼,在使用外健的時候要保證其唯一性。
在主鍵關(guān)聯(lián)的情況下, 必須有一個主鍵是根據(jù)別一個主鍵而來的胃夏。NHB是通過一種特殊的方式來處理這種情況的, 要注意兩個主健名稱必須同名轴或,而外健關(guān)聯(lián)需要在one-to-one配置中定義一個property-ref屬性, 這個屬性在當(dāng)前版本的NHB中還沒有實現(xiàn)。
在羅斯文商貿(mào)應(yīng)用仰禀,不需要使用one-to-one映射照雁,這里先不對其進(jìn)行講解,如欲了解one-to-one方面的應(yīng)用答恶,請參考我網(wǎng)站上的文章饺蚊。
4. many-to-one
many-to-one是描述多對一的一種數(shù)據(jù)模型,它指定many一方是不能獨立存在的悬嗓,我個人認(rèn)為many-to-one是NHB中保證數(shù)據(jù)有效性的最有用的一種映射污呼,通過使用many-to-one能有效的防治孤兒記錄被寫入到數(shù)據(jù)表中。
在羅斯文商貿(mào)數(shù)據(jù)中包竹,Product(產(chǎn)品)與Category(類別)是多對一的關(guān)系燕酷。下面我們來處理這一映射關(guān)系,
首先要讓Category能實現(xiàn)基本的CRUD操作衡便,步驟同上, 這里只列出測試用例郑趁,類和映射文件請參照上面的方式創(chuàng)建歧譬。
[TextFixture]
public class CategoryFixture {
public CategoryFixture() {
}
[Test] // 測試基本的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();
}
}
上面的測試用例通過后厌殉,接著修改Product的各部分殊橙。
Product測試用例修改如下:
[Test] // 測試Product對象的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" />
這里用到了一個many-to-one標(biāo)簽浴麻,用于指定與one的一方進(jìn)行關(guān)聯(lián)的對象信息得问。
name指定one一方在對象中的名稱;
column指定映射數(shù)據(jù)列的名稱;
unique指定one一方是唯一的;
class 指定one一方類的全名,包括程序集名稱;
重新編譯程序软免,運行測試用例, 看到綠條了嗎宫纬?沒有就根據(jù)異常去除錯吧!我已經(jīng)看到綠條了膏萧。:)
聲明: 因為過多的many-to-one使后面的測試代碼變得異常龐大(創(chuàng)建對象時要創(chuàng)建one一方的類)漓骚,所以在后面的代碼中,我假定Product對象是沒有實現(xiàn)任何many-to-one映射的榛泛。如果不怕麻煩蝌蹂,請自行修改后面測試用例。
5. one-to-many
一對多也是一種常見的數(shù)據(jù)模型曹锨,在按范式設(shè)計的數(shù)據(jù)庫中隨處可見孤个。在NHB中通過one-to-many可以非常方便的處理這種模型,同時NHB還提供了級聯(lián)更新和刪除的功能沛简,以保證數(shù)據(jù)完整性齐鲤。
在羅斯文商貿(mào)案例中,Customer與Order(訂單)椒楣、Order和OrderItem(訂單項目)就是一對多的關(guān)系给郊,值得注意的是,并不是所有一對多關(guān)系都應(yīng)該在NHB中實現(xiàn)捧灰,這取決于實際的需求情況淆九,無謂的使用one-to-many映射只會降低NHB的使用性能。
下面先讓Order和OrderItem對象能單獨的完成CRUD操作毛俏,按上面處理Customer的方法來創(chuàng)建測試用例和類炭庙,
[TestFixture]
public class OrderFixture {
public OrderFixture() {
}
[Test] // 測試Order對象的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] // 測試OrderItem對象的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映射文件
因為設(shè)計上的原因(業(yè)務(wù)對象必須為單主健)煤搜,我們向OrderDetails加入一個ItemId字段,這是一個自增型的主健唧席,以代替原來的聯(lián)合主健擦盾。
編譯并運行測試,檢查錯誤直到單元測試全部通過淌哟。
注意迹卢,在數(shù)據(jù)庫中要進(jìn)行如下修改,測試用例才能通過
1.orders表與customers表徒仓、employeess表和shippers表有外鍵約束腐碱,必須暫時將它們刪除,后面我們再加上這三個約束;
2.將Order Details改名為OrderDetails症见,表名中出現(xiàn)空格將導(dǎo)致NHB無法解析喂走;
3.orderDetails表與orders表、products表有外健約束谋作,必須暫時將它們刪除芋肠,下面我們將加上這些約束。
現(xiàn)在開始重構(gòu)OrderItem的測試用例和對象遵蚜,主要是加入many-to-one映射帖池。
先將TestItemCRUD修改為如下:
[Test] // 測試OrderItem對象的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();
}
}
接下來修改OrderItem對象吭净,改動如下:
// 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àn)錯誤囚巴,請檢查是否有拼寫錯誤。
最后修改OrderItem對象的映射文件友扰,改動如下:
class=”NHibernateTest.Business.Order, NHibernateTest” />
class=”NHibernateTest.Business.Product, NHibernateTest” />
按many-to-one一節(jié)中的說明對Order和Product對象設(shè)置many-to-one關(guān)聯(lián)彤叉。
重新生成項目,以使改動的資源編譯進(jìn)程序集中焕檬,運行TestItemCRUD測試用例,這時應(yīng)能得到一個綠條澳泵,如果是紅條实愚,請根據(jù)錯誤信息進(jìn)行檢查。
接下來重構(gòu)Order測試用例和對象兔辅。
先添加一個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();
}
}
在這個測試用例中腊敲,我們在Order對象中封裝了對OrderItem的添加和移除操作,提供一個ICollection類型的屬性Items用于遍歷OrderItem维苔。
接著修改Order對象碰辅,要添加兩個方法和一個屬性、一些輔助字段.
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;
在上面加入的代碼中介时,有個protected修飾的_Items屬性没宾,它是用于one-to-many映射的,由NHB使用沸柔。
最后修改Order對象的映射文件循衰,加入以下one-to-many代碼:
這里又用到了一個新的標(biāo)簽bag, bag用于集合映射,在NHB中還有set, list等褐澎,它們的元素大致相同会钝,但對應(yīng)的.NET集合對象卻是不一樣的,后面對它們進(jìn)行詳細(xì)的說明和比較工三。
bag屬性用于指定集合的名稱和級聯(lián)操作的類型;
key元素指定關(guān)聯(lián)的數(shù)據(jù)列名稱;
one-to-many指定many一方類的全名迁酸,包括程序集名稱先鱼。
再次編譯項目并運行測試用例,我得到了一個這樣的診斷錯誤信息:
NHibernateTest.Test.OrderFixture.TestOrderItem : remove item fail!
expected:<2>
but was:<1>
從源代碼可以得知奸鬓,當(dāng)執(zhí)行Update操作時焙畔,級聯(lián)操作并不會刪除我們移除的子對象,必須自行刪除全蝶!級聯(lián)刪除只是指刪除父對象的時候同時刪除子對象闹蒜。
修改TestOrderItem測試用例代碼如下:
o2.Update(); // 此行不變
item3.Delete(); // 加入此行代碼
6. element
集合element是一種處理多對多的映射,多對多在數(shù)據(jù)庫中也是常見的數(shù)據(jù)模型抑淫,像用戶與組绷落,用戶與權(quán)限等。多對多關(guān)系需要通過一個中間表實現(xiàn)始苇,element的就是讀取這個中間表中某列的值砌烁。
在羅斯文商貿(mào)應(yīng)用中,Employee和Territory之間是多對多的關(guān)系催式,它們通過EmployeeTerritories表進(jìn)行關(guān)聯(lián)函喉。 有關(guān)Employee和Territory對象的代碼、測試用例和映射文件請自行參照上面的方法創(chuàng)建荣月,這里就不列出代碼了管呵,下面只列出測試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添加兩個方法和一個屬性捐下,這和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對象的映射文件坷襟,加入以下內(nèi)容:
在bag標(biāo)簽中,加入了一個table屬性生年,它指定一個實現(xiàn)多對多的中間表婴程。在element元素中,指定要讀取的列名及其類型抱婉。
四 數(shù)據(jù)加載
1. Expression
Expression數(shù)據(jù)加載由ICriteria接口實現(xiàn), ICriteria在程序中是無法直接構(gòu)造的档叔,必須通過ISession.CreateCriteria(type)來獲得。ICriteria主要負(fù)責(zé)存儲一組Expression對象和一組Order對象蒸绩,當(dāng)調(diào)用List執(zhí)行查詢時蹲蒲,ICriteria對Expression對象和Order對象進(jìn)行組合以產(chǎn)生NHB內(nèi)部的查詢語句,然后交由DataLoader(數(shù)據(jù)加載器)來讀取滿足條件的記錄侵贵。
下面列出ICriteria接口中的一些常用方法:
Add:加入條件表達(dá)式(Expression對象)届搁,此方法可多次調(diào)用以組合多個條件;
AddOrder:加入排序的字段(Order對象);
List:執(zhí)行查詢, 返回滿足條件的對象集合卡睦。
SetMaxResults:設(shè)置返回的最大結(jié)果數(shù)宴胧,可用于分頁;
SetFirstResult:設(shè)置首個對象返回的位置表锻,可用于分頁恕齐;
通過SetMaxResults和SetFirstResult方法,就可以取得指定范圍段的記錄瞬逊,相當(dāng)于是分頁显歧,
!H纺鳌士骤! 要說明的是,對于SQL Server數(shù)據(jù)庫蕾域,它是使用將DataReader指針移到firstResult位置拷肌,再讀取maxResults記錄的方式來實現(xiàn)分頁的,在數(shù)據(jù)量非常大(10w以上)的情況下旨巷,性能很難保證巨缘。
所有表達(dá)式對象都繼承之Expression類,這是一個抽象(abstract)類, 同時也是一個類工廠(Factory Method模式), 用于創(chuàng)建派生的Expression對象采呐,這樣就隱藏了派生類的細(xì)節(jié)若锁。(又學(xué)到一招了吧!)
下面列出幾個常用的Expression對象:
EqExpression :相等判斷的表達(dá)式, 等同于 propertyName = value斧吐,由Expression.Eq取得;
GtExpression :大于判斷的表達(dá)式, 等同于 propertyName > value又固,由Expression.Gt取得;
LikeExpression :相似判斷的表達(dá)式, 等同于 propertyName like value,由Expression.Like取得会通;
AndExpression :對兩個表達(dá)式進(jìn)行And操作, 等同于 expr1 and expr2口予,由Expression.And取得;
OrExpression :對兩個表達(dá)式進(jìn)行Or操作, 等同于 expr1 or expr2娄周,由Expression.Or取得;
更多的Expression對象請參考相關(guān)文檔或源代碼涕侈。
Order對象用于向ICriteria接口提供排序信息,這個類提供了兩個靜態(tài)方法煤辨,分別是Asc和Desc裳涛,顧名思義就是創(chuàng)建升序和降序的Order對象,例如要取得一個按更新日期(Updated)降序的Order對象, 使用Order.Desc("Updated")就可以了众辨。
下面以加載Customer數(shù)據(jù)為例來說明Expression的使用:
測試代碼如下,
[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.
}
}
在上面的代碼中端三,給出了四個較簡單的表達(dá)式加載的測試用例,它們都通過調(diào)用ObjectLoader對象的Find方法來取得數(shù)據(jù)鹃彻,ObjectLoader是我們自己的數(shù)據(jù)加載器郊闯,它簡單的封裝了NHB中的數(shù)據(jù)加載功能。另外,我們還用一個PagerInfo類封裝了分頁數(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方法中,先通過會話取得ICriteria接口, 然后加入表達(dá)式欢摄,接著檢查是否需要設(shè)置分頁數(shù)據(jù)熬丧,最后返回列出的數(shù)據(jù)。
2. HQL
HQL(Hibernate Query Language)是NHB的專用查詢語言怀挠,它完全面向?qū)ο笪龊【褪钦f只需要知道對象名和屬性名就可以生成HQL了,這樣就再也不用去理會數(shù)據(jù)表和列了绿淋,前面說的Expression查詢最終也會轉(zhuǎn)換為HQL闷畸。
有兩種方式來執(zhí)行HQL,一種是直接使用ISession的Find方法躬它,另一種是使用IQuery接口腾啥。IQuery接口提供了一些額外的設(shè)置,最重要的就是分頁了冯吓,這個和ICriteria差不多倘待,另外一些就是設(shè)置參數(shù)的值了。
NHB中有一組類專門用于完成數(shù)據(jù)加載组贺,它們分別對應(yīng)不同的數(shù)據(jù)加載情況凸舵,如實體加載、Criteria加載失尖、OneToMany加載等啊奄。
下面同樣以加載Customer數(shù)據(jù)為例來說明HQL的使用:
在上面的CustomerSystemFixture類中加入以下幾個測試用例:
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.
}
}
在上面的測試用例中,我們同樣將數(shù)據(jù)加載交由ObjectLoader的Find方法來處理掀潮,F(xiàn)ind有很多重載的版本菇夸,都用于數(shù)據(jù)加載。另外還使用了一個ParameterInfo類來存儲HQL語句的參數(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方法中庄新,通過HQL語句創(chuàng)建一個IQuery, 然后加入?yún)?shù),接著設(shè)置分頁數(shù)據(jù)薯鼠,最后返回列出的數(shù)據(jù)择诈。
五 事務(wù)
既然而數(shù)據(jù)庫打交道,那么事務(wù)處理就是必需的出皇,事務(wù)能保整數(shù)據(jù)完整性羞芍。在NHB中,ITransaction對象只對.NET的事務(wù)對象(實現(xiàn)了IDbTransaction接口的對象)進(jìn)行了簡單的封裝郊艘。
使用NHB的典型事務(wù)處理看起來像下面這樣(見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ù)對象由ISession的BeginTransaction取得,同時事務(wù)開始荷科,如果執(zhí)行順利則提交事務(wù)唯咬,否則回滾事務(wù)。
當(dāng)實現(xiàn)一個業(yè)務(wù)規(guī)則時畏浆,而這一規(guī)則要改變多個業(yè)務(wù)對象狀態(tài)時副渴,這時就需要使用事務(wù)了,事務(wù)能保證所有改變要么全部保存全度,要么全部不保存煮剧!
在羅斯文商貿(mào)案例中,有這樣一個業(yè)務(wù)規(guī)則:
如果客戶對某一產(chǎn)品下了訂單将鸵,那么被訂購產(chǎn)品的已訂購數(shù)量(UnitsOnOrder)應(yīng)該加上客戶的產(chǎn)品訂單量勉盅,根據(jù)這一業(yè)務(wù)規(guī)則,創(chuàng)建一個測試用例如下:
[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!” );
}
}