NHibernate學習

寫的非常好的NHibernate教學,轉自http://sifang2004.cnblogs.com/archive/2005/09/05/230713.aspx

最近準備學NHibernate,於是網上狂找,看來有不少文章,但仔細看就會明白,搞來搞去,其實就那麼幾篇大同小異的文章,但還是終於在我們的博客上找到篇好點的,就是下面那篇了,我也不明白是哪爲高手寫的了,因爲轉載的太多了,有點糊塗了,也許是張老三的作品吧,但不知道是沒有把相關源碼共享出來,還是我沒有找到,對一個新手來說,就一些文字真的有點困難啊,何況下面的文章可能由於手誤,還是某些原因,我是調試很久才調得出來,也許是我的NHibernate,和NUIT的版本跟原文的不同,我用的NHibernate 0.9.1.0,NUnit2.2。順便把NUnit也學了,還是蠻不錯的了。下面的文章也許是我有些修改的了,望給想學NHibernate的稍有補益。

http://www.cnblogs.com/Files/sifang2004/NHibernateTest.rar
NHibernate 博客園專題之一

(原文標題,本人尊重原作,故此保留,可以點擊看到原文)

http://www.cnblogs.com/wxx/archive/2005/07/17/194337.html


(因爲太麻煩,我下面對原文所做的修改,不會再有說明,也再次感謝本文原作的努力,隨着我學習的深入,我也會不斷修改下文)
本文約定:
1. Nhibernate簡寫爲NHB;
2. 本文例子的開發平臺爲win2000xp+sp2, sql server2000, Nhibernate0.9.1.0;
3. 使用SQL Server自帶的羅斯文商貿數據庫(Northwind),中文版;
4. 本文例子是基於測試驅動開發(TDD)的,因此建議使用NUnit2.2和Log4Net (如果你不熟悉NUnit,不要緊啊,趁此機會學習點簡單的應用);

一 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。

測試類代碼如下:

using System;
using NUnit.Framework;
using NHibernate;

namespace NHibernateTest
{
    
/// 
    
/// SessionsFixture 的摘要說明。
    
/// 
    
/// 

    [TestFixture]
    
public class SessionsFixture
    
{
        
public SessionsFixture()
        
{
            
//
            
// TODO: 在此處添加構造函數邏輯
            
//
        }

        [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類.

using System;
using NHibernate;
using System.Reflection;

namespace NHibernateTest
{
    
/// 
    
/// Sessions 的摘要說明。
    
/// 

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

        
public Sessions()
        
{
            
//
            
// TODO: 在此處添加構造函數邏輯
            
//
        }

        
public static ISessionFactory Factory 
               
{
                   
get 
                   

                       
if ( _factory == null ) 
                       
{
                           
lock ( lockObj ) 
                           
{
                               
if ( _factory == null ) 
                               
{
                                   NHibernate.Cfg.Configuration cfg 
= new NHibernate.Cfg.Configuration ();
                                   cfg.AddAssembly( Assembly.GetExecutingAssembly() );
                                   _factory 
= cfg.BuildSessionFactory(); 
                               }

                           }
 
                       }

                       
return _factory;
                   }

               }
 
        
public static ISession GetSession() 
        
{
            
return Factory.OpenSession();
        }


    }

}

 OK,現在編譯可以通過了,啓動NUnit並選擇生成的文件NHibernateTest.exe,運行測試。我們得到了紅色的條,出錯了!原來還沒有加入NHibernate的配置信息(當使用NHibernate時,需要在項目的配置文件中加入NHibernate的配置信息。關於配置信息,在下面有說明)。在項目的配置文件App.Config(如沒有請自行創建一個)中加入以下內容.

稍做解釋,下面的配置文件主要是配置了NHibernate和log4net,例如配置了NHibernate連接數據庫的連接字符串;log4net把日誌記到哪個文件中,本例就是"log.txt"。每次寫日誌向文件中追加,而不是重寫

<xml version="1.0" encoding="utf-8" ?>
<configuration>
        
<configSections>
            
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>
    
    
<nhibernate>
        
<add 
            
key="hibernate.show_sql"
            value
="true"
        
/>
        
<add 
            
key="hibernate.connection.provider"          
            value
="NHibernate.Connection.DriverConnectionProvider" 
        
/>
        
<add 
            
key="hibernate.dialect"                      
            value
="NHibernate.Dialect.MsSql2000Dialect" 
        
/>
        
<add 
            
key="hibernate.connection.driver_class"          
            value
="NHibernate.Driver.SqlClientDriver" 
        
/>
        
<add 
            
key="hibernate.connection.connection_string" 
            value
="Server=127.0.0.1;initial catalog=Northwind;User id =golinjoe;Password=2525775" 
        
/>
        
    </nhibernate>

    
    
<log4net>

            

        
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
            
            
<param name="File" value="log.txt" />
            
<param name="AppendToFile" value="true" />
            
<param name="RollingStyle" value="Date" />
            
<param name="DatePattern" value="yyyy.MM.dd" />
            
<param name="StaticLogFileName" value="true" />

            
<layout type="log4net.Layout.PatternLayout,log4net">
                
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
            </layout>
        </appender>

        
        
        
<root>
            
<priority value="ALL" />
            
<appender-ref ref="rollingFile" />
        </root>

   </log4net>
    

</configuration>

再次運行測試,就可以看見綠色的條了。

在取得會話工廠的代碼中,我使用瞭如下代碼:
if ( _factory == null ) {
   lock ( lockObj ) {
      if ( _factory == null ) {
         // build sessionfactory code;
      }
   }
}
這是一個典型的double lock方式,用來產生線程安全的Singletion(單例)對象。

2. 基本CRUD操作
在很多介紹NHB的文章,包括NHB帶的測試用例中,業務對象只是做爲一個數據實體存在的,它沒有任何操作!這在java中是比較典型的作法。
而我希望我們的業務對象自身就能完成基本的Create/Retrieve/Update/Delete,即CRUD操作,
在羅斯文商貿應用中,存在客戶(customer)業務對象,先來爲它建立一個測試用
例,

using System;
using NHibernate;
using NUnit.Framework;

namespace NHibernateTest
{
    
/// 
    
/// CustomerFixture 的摘要說明。
    
/// 

    [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(); 
// 測試 insert操作,

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

            c2.CompanyName 
= "update name";
            c2.Update(); 
// 測試 update 操作.

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

            c3.Delete(); 
// 測試 delete 操作.
        }

    }

}

接下來創建Customer業務類:

using System;

namespace NHibernateTest
{
    
/// 
    
/// Customer 的摘要說明。
    
/// 

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

        
persistent properties.
    }

}

在Customer類中,沒有實現CRUD操作,這些操作在業務對象基類BizObject中實現,代碼如下:

using System;

namespace NHibernateTest
{
    
/// 
    
/// 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之間的耦合, 以利於持久層間的移植。

using System;
using NHibernate;
using NHibernateTest;

namespace NHibernateTest
{
    
/// 
    
/// ObjectBroker 的摘要說明。
    
/// 

    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.CustomerFixture.TestCRUD : NHibernate.MappingException : Unknown entity class: NHibernateTest.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.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單元測試

using System;
using NUnit.Framework;

namespace NHibernateTest
{
    
/// 
    
/// ProductFixture 的摘要說明。
    
/// 

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

        [Test] 
// 測試Product對象的CRUD操作。
        public void TestCRUD() 
        
{
            Category c 
= null;
            
try 
            
{
                c 
= new Category();
                c.CategoryName 
= "test";
                c.Create();

                Product p 
= new Product();
                p.ProductName 
= "test"
                p.Category 
= c;
                p.SupplierId 
= 3;
                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();
            }

            
finally 
            
{
                
if ( c != null && c.CategoryId > 0 ) c.Delete();
            }


        }

    }


}

// Product對象(注意,註釋部分並不是無意義的,是在不同的情況下做測試用的,本文中的註釋都基本如此)
using System;

namespace NHibernateTest
{
    
/// 
    
/// Product 的摘要說明。
    
/// 

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

        
persistent properties
    }

}


// 映射文件

<xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   
<class name="NHibernateTest.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" />
      
<property name="UnitsInStock" column="unitsInStock" type="Int32" />
      
<property name="UnitsOnOrder" column="unitsOnOrder" type="Int32" />
      
<property name="ReorderLevel" column="reorderLevel" type="Int32" />
      
<property name="Discontinued" column="discontinued" type="Boolean" />

      
      
<property name="SupplierId" column="SupplierId" type="Int32" />
      
      
<many-to-one name="Category" column="categoryId" unique="true" class="NHibernateTest.Category, NHibernateTest" />
   </class> 
</hibernate-mapping>

         編譯並運行測試,檢查錯誤直到單元測試通過。
         注意:因爲在數據庫中,products表與categories表、suppliers表有外鍵約束,必須先刪除這兩個約束,product測試用例才能通過,後面我們再加上這兩個約束。

現在我們已經掌握了NHB的基本CRUD操作了,整個過程應該說是比較簡單吧。呵呵,不再需要使用Connection、DataAdapter、DataSet/DataReader之類的對象了,下面繼續學習NHB中更爲複雜的映射關係。

3. one-to-one
一對一是一種常見的數據模型,它有兩種情況:一種是主鍵(PrimaryKey)關聯;另一種是外健(ForeignKey)關聯,在使用外健的時候要保證其唯一性。
在主鍵關聯的情況下, 必須有一個主鍵是根據別一個主鍵而來的。NHB是通過一種特殊的方式來處理這種情況的, 要注意兩個主健名稱必須同名,而外健關聯需要在one-to-one配置中定義一個property-ref屬性, 這個屬性在當前版本的NHB(這是指的是nhibernate 0.5.3,當前的版本是什麼情況我現在也沒有時間瞭解)中還沒有實現。
在羅斯文商貿應用,不需要使用one-to-one映射,這裏先不對其進行講解,如欲瞭解one-to-one方面的應用,請參考我網站上的文章。

4. many-to-one
many-to-one是描述多對一的一種數據模型,它指定many一方是不能獨立存在的,我個人認爲many-to-one是NHB中保證數據有效性的最有用的一種映射,通過使用many-to-one能有效的防治孤兒記錄被寫入到數據表中。
在羅斯文商貿數據中,Product(產品)與Category(類別)是多對一的關係。下面我們來處理這一映射關係,
首先要讓Category能實現基本的CRUD操作,步驟同上, 這裏只列出測試用例,類和映射文件請參照上面的方式創建。

[TestFixture]
    
public class CategoryFixture 
    
{
        
public CategoryFixture() 
        

        }

        [Test] 
// 測試基本的CRUD操作。
        public void TestCRUD() 
        
{
            Category c 
= new Category();
            c.CategoryName 
= "category1";
            c.Description 
= "category1";
            c.Create();

            Category c2 
= new Category(c.CategoryId);
            c2.CategoryName 
= "testupdated";
            c2.Update();

            Category c3 
= new Category( c.CategoryId);
            Assert.AreEqual( c3.CategoryName, 
"testupdated""update fail! " );
            c3.Delete();
        }

    }


上面的測試用例通過後,接着修改Product的各部分。
Product測試用例修改如下:

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標籤,用於指定與one的一方進行關聯的對象信息。
name指定one一方在對象中的名稱;
column指定映射數據列的名稱;
unique指定one一方是唯一的;
class 指定one一方類的全名,包括程序集名稱;

重新編譯程序,運行測試用例, 看到綠條了嗎?沒有就根據異常去除錯吧!我已經看到綠條了。:)

聲明: 因爲過多的many-to-one使後面的測試代碼變得異常龐大(創建對象時要創建one一方的類),所以在後面的代碼中,我假定Product對象是沒有實現任何many-to-one映射的。如果不怕麻煩,請自行修改後面測試用例。

5. one-to-many
一對多也是一種常見的數據模型,在按範式設計的數據庫中隨處可見。在NHB中通過one-to-many可以非常方便的處理這種模型,同時NHB還提供了級聯更新和刪除的功能,以保證數據完整性。
在羅斯文商貿案例中,Customer與Order(訂單)、Order和OrderItem(訂單項目)就是一對多的關係,值得注意的是,並不是所有一對多關係都應該在NHB中實現,這取決於實際的需求情況,無謂的使用one-to-many映射只會降低NHB的使用性能。

下面先讓Order和OrderItem對象能單獨的完成CRUD操作,按上面處理Customer的方法來創建測試用例和類, 

using System;
using NUnit.Framework;
using System.Collections;

namespace NHibernateTest
{
    
/// 
    
/// OrderFixture 的摘要說明。
    
/// 

    [TestFixture]
    
public class OrderFixture 
    
{
        
public OrderFixture() 
        

        }

        [Test] 
// 測試Order對象的CRUD操作。
        public void TestCRUD() 
        
{
            Order o 
= new Order();
            o.CustomerId 
= "ALFKI";
            o.EmployeeId 
= 1;
            o.RequiredDate 
= new DateTime(2005,9,10);
            o.ShippedDate 
= new DateTime(2005,8,28);
            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() 
        
{
            Order o 
= null;
            Product p 
= null;
            
try 
            
{
                o 
= new Order();
                o.RequiredDate 
= new DateTime(2005,9,10);
                o.ShippedDate 
= new DateTime(2005,8,28);
                o.CustomerId 
= "ALFKI";
                o.EmployeeId 
= 1;
                o.ShipVia 
= 1;
                o.Freight 
= 20.5M;
                o.ShipName 
= "test name";
                o.ShipAddress 
= "test address";
                o.Create();

                p 
= new Product();
                p.ProductName 
= "test";
                p.UnitPrice 
= 11.1M;
                p.SupplierId 
= 3;
                p.Create();

                OrderItem item 
= new OrderItem();
//                item.OrderId = 10248;
                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();
            }

        }

        [Test]
        
public void TestOrderItem() 
        
{
            Product p 
= null;
            Product p2 
= null;
            
try 
            
{
                p 
= new Product();
                p.ProductName 
= "test";
                p.UnitPrice 
= 11.1M;
                p.SupplierId 
= 3;
                p.Create();
                p2 
= new Product();
                p2.ProductName 
= "test2";
                p2.UnitPrice 
= 18.1M;
                p2.SupplierId 
= 3;
                p2.Create();

                Order o 
= new Order(); 
                o.CustomerId 
= "ALFKI";
                o.EmployeeId 
= 1;
                o.RequiredDate 
= new DateTime(2005,9,10);
                o.ShippedDate 
= new DateTime(2005,8,28);
                o.ShipVia 
= 1;
                o.Freight 
= 20.5M;
                o.ShipName 
= "test name";
                o.ShipAddress 
= "test address";
                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();
                item3.Delete();

                Order o3 
= new Order( o.OrderId );
                Assert.AreEqual( o3.Items.Count, 
2"remove item fail! " );

                o3.Delete();
            }

            
finally 
            
{
                
if (p!=null && p.ProductId > 0) p.Delete();
                
if (p2!=null && p2.ProductId >0) p2.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

這個太長了,不貼了,自己看代碼吧。
// Order映射文件

 

[Test] // 測試Product對象的CRUD操作。
        public void TestCRUD() 
        
{
            Category c 
= null;
            
try 
            
{
                c 
= new Category();
                c.CategoryName 
= "test";
                c.Create();

                Product p 
= new Product();
                p.ProductName 
= "test"
                p.Category 
= c;
                p.SupplierId 
= 3;
                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();
            }

            
finally 
            
{
                
if ( c != null && c.CategoryId > 0 ) c.Delete();
            }


        }
<xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   
<class name="NHibernateTest.Order, NHibernateTest" table="Orders"> 
      
<id name="OrderId" column="orderId" type="Int32" unsaved-value="0">
         
<generator class="identity"/>
      </id>
      
<property name="OrderDate" column="orderDate" type="DateTime" />
      
<property name="RequiredDate" column="requiredDate" type="DateTime" />
      
<property name="ShippedDate" column="shippedDate" type="DateTime" />
      
<property name="Freight" column="freight" type="Decimal" />
      
<property name="ShipName" column="shipName" type="String" />
      
<property name="ShipAddress" column="shipAddress" type="String" />
      
<property name="ShipCity" column="shipCity" type="String" />
      
<property name="ShipRegion" column="shipRegion" type="String" />
      
<property name="ShipPostalCode" column="shipPostalCode" type="String" />
      
<property name="ShipCountry" column="shipCountry" type="String" />

      
      
<property name="CustomerId" column="customerId" type="String" />
      
<property name="EmployeeId" column="employeeId" type="Int32" />
      
<property name="ShipVia" column="shipVia" type="Int32" />
      
<bag name="_Items" cascade="all" inverse="true">
        
<key column="orderId"/>
        
<one-to-many class="NHibernateTest.OrderItem, NHibernateTest" />
      </bag>

   </class> 
</hibernate-mapping>

 

// OrderItem

using System;

namespace NHibernateTest
{
    
/// 
    
/// OrderItem 的摘要說明。
    
/// 

    public class OrderItem : BizObject 
    
{
        
public OrderItem() : base() { }
        
public OrderItem( int existingId ) : base( existingId ) { }

        
persistent properties
    }


}

 

// OrderItem映射文件


因爲設計上的原因(業務對象必須爲單主健),我們向OrderDetails加入一個ItemId字段,這是一個自增型的主健,以代替原來的聯合主健。

編譯並運行測試,檢查錯誤直到單元測試全部通過。
注意,在數據庫中要進行如下修改,測試用例才能通過
1.orders表與customers表、employeess表和shippers表有外鍵約束,必須暫時將它們刪除,後面我們再加上這三個約束;
2.將Order Details改名爲OrderDetails,表名中出現空格將導致NHB無法解析;
3.orderDetails表與orders表、products表有外健約束,必須暫時將它們刪除,下面我們將加上這些約束。

現在開始重構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, 需要重構.
private Order Order;
// private int _productId = 0; // many-to-one, 需要重構.
private Product Product;

刪除OrderId和ProductId屬性,並加入以下屬性:
public Order Order {
   get { return _order; }
   set { _order = value; }
}
public Product Product {
   get { return _product; }
   set { _product = value; }
}

編譯項目,確保其能通過,如出現錯誤,請檢查是否有拼寫錯誤。

最後修改OrderItem對象的映射文件,改動如下:



class=”NHibernateTest.Business.Order, NHibernateTest” />

class=”NHibernateTest.Business.Product, NHibernateTest” />
按many-to-one一節中的說明對Order和Product對象設置many-to-one關聯。

重新生成項目,以使改動的資源編譯進程序集中,運行TestItemCRUD測試用例,這時應能得到一個綠條,如果是紅條,請根據錯誤信息進行檢查。

接下來重構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代碼:

   
   


這裏又用到了一個新的標籤bag, bag用於集合映射,在NHB中還有set, list等,它們的元素大致相同,但對應的.NET集合對象卻是不一樣的,後面對它們進行詳細的說明和比較。
bag屬性用於指定集合的名稱和級聯操作的類型;
key元素指定關聯的數據列名稱;
one-to-many指定many一方類的全名,包括程序集名稱。

再次編譯項目並運行測試用例,我得到了一個這樣的診斷錯誤信息:
NHibernateTest.OrderFixture.TestOrderItem : NHibernate.ADOException : could not synchronize database state with session
  ----> System.Data.SqlClient.SqlException : DELETE 語句與 COLUMN REFERENCE 約束 'FK_Order_Details_Products' 衝突。該衝突發生於數據庫 'Northwind',表 'Order Details', column 'ProductID'。
語句已終止。
從源代碼可以得知,當執行Update操作時,級聯操作並不會刪除我們移除的子對象,必須自行刪除!級聯刪除只是指刪除父對象的時候同時刪除子對象。

修改TestOrderItem測試用例代碼如下:
o2.Update(); // 此行不變
item3.Delete(); // 加入此行代碼

6. element(這個我沒有做,自己加入調試吧!)
集合element是一種處理多對多的映射,多對多在數據庫中也是常見的數據模型,像用戶與組,用戶與權限等。多對多關係需要通過一箇中間表實現,element的就是讀取這個中間表中某列的值。
在羅斯文商貿應用中,Employee和Territory之間是多對多的關係,它們通過EmployeeTerritories表進行關聯。 有關Employee和Territory對象的代碼、測試用例和映射文件請自行參照上面的方法創建,這裏就不列出代碼了,下面只列出測試element映射的部分。
[TestFixture]
public class EmployeeFixture() {
// other test....
   [test]
   public TerritoryElementTest() {
      Employee e = new Employee();
      e.FirstName = “first”;
      e.LastName = “last”;
      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一節中介紹的處理方法是相似的。
在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對象的映射文件,加入以下內容:

   
   

在bag標籤中,加入了一個table屬性,它指定一個實現多對多的中間表。在element元素中,指定要讀取的列名及其類型。

四 數據加載

1. Expression

Expression數據加載由ICriteria接口實現, ICriteria在程序中是無法直接構造的,必須通過ISession.CreateCriteria(type)來獲得。ICriteria主要負責存儲一組Expression對象和一組Order對象,當調用List執行查詢時,ICriteria對Expression對象和Order對象進行組合以產生NHB內部的查詢語句,然後交由DataLoader(數據加載器)來讀取滿足條件的記錄。

下面列出ICriteria接口中的一些常用方法:

Add:加入條件表達式(Expression對象),此方法可多次調用以組合多個條件;
AddOrder:加入排序的字段(Order對象);
List:執行查詢, 返回滿足條件的對象集合。
SetMaxResults:設置返回的最大結果數,可用於分頁;
SetFirstResult:設置首個對象返回的位置,可用於分頁;

通過SetMaxResults和SetFirstResult方法,就可以取得指定範圍段的記錄,相當於是分頁,
!!! 要說明的是,對於SQL Server數據庫,它是使用將DataReader指針移到firstResult位置,再讀取maxResults記錄的方式來實現分頁的,在數據量非常大(10w以上)的情況下,性能很難保證。

所有表達式對象都繼承之Expression類,這是一個抽象(abstract)類, 同時也是一個類工廠(Factory Method模式), 用於創建派生的Expression對象,這樣就隱藏了派生類的細節。(又學到一招了吧!)

下面列出幾個常用的Expression對象:

EqExpression :相等判斷的表達式, 等同於 propertyName = value,由Expression.Eq取得;
GtExpression :大於判斷的表達式, 等同於 propertyName > value,由Expression.Gt取得;
LikeExpression :相似判斷的表達式, 等同於 propertyName like value,由Expression.Like取得;
AndExpression :對兩個表達式進行And操作, 等同於 expr1 and expr2,由Expression.And取得;
OrExpression :對兩個表達式進行Or操作, 等同於 expr1 or expr2,由Expression.Or取得;
更多的Expression對象請參考相關文檔或源代碼。

Order對象用於向ICriteria接口提供排序信息,這個類提供了兩個靜態方法,分別是Asc和Desc,顧名思義就是創建升序和降序的Order對象,例如要取得一個按更新日期(Updated)降序的Order對象, 使用Order.Desc("Updated")就可以了。

下面以加載Customer數據爲例來說明Expression的使用:
測試代碼如:

using System;
using NHibernate.Hql;
using NHibernate.Expression;
using NHibernate.Type;
using NUnit.Framework;
using System.Collections;

namespace NHibernateTest
{
    
/// 
    
/// CustomerSystemFixture 的摘要說明。
    
/// 

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

        [Test]
        
public void LoadByNameTest() 
        
{
            ICriterion crit 
= Expression.Eq("CompanyName""company name");
            IList custs 
= ObjectLoader.Find(crit, typeof(Customer));
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadByNamePagerTest() 
        
{
            ICriterion crit 
= Expression.Eq("CompanyName""company name");
            PagerInfo pi 
= new PagerInfo(05);
            IList custs 
= ObjectLoader.Find(crit, typeof(Customer) , pi);
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadByNameAndAddressTest() 
        
{
            ICriterion crit 
= Expression.Eq("CompanyName""company name");
            ICriterion crit2 
= Expression.Eq("Address""address");
            IList custs 
= ObjectLoader.Find(Expression.And(crit, crit2), typeof(Customer));
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadByNameOrAddressTest() 
        
{
            ICriterion crit 
= Expression.Eq("CompanyName""company name");
            ICriterion crit2 
= Expression.Eq("Address""address");
            IList custs 
= ObjectLoader.Find(Expression.Or(crit, crit2), typeof(Customer));
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadAllTest () 
        
{
            
string query = " from Customer ";
            IList custs 
= ObjectLoader.Find( query, null );
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadPagerDataTest() 
        
{
            
string query = " from Customer ";
            PagerInfo pi 
= new PagerInfo( 05 );
            IList custs 
= ObjectLoader.Find( query, null, pi );
        }

        [Test]
        
public void LoadByName2Test() 
        
{
            IList paramInfos 
= new ArrayList();
            
string query = " from Customer c where c.CompanyName = :CompanyName ";
            paramInfos.Add(
new ParamInfo("CompanyName""test name",TypeFactory.GetStringType()) );
            IList custs 
= ObjectLoader.Find( query, paramInfos );
            
// 根據期望的結果集寫Assertion.
        }

        [Test]
        
public void LoadByNameAndAddress2Test() 
        
{
            IList paramInfos 
= new ArrayList();
            
string query = " from Customer c where c.CompanyName = :CompanyName and c.Address = :Address";
            paramInfos.Add( 
new ParamInfo("CompanyName""test name",TypeFactory.GetStringType()));
            paramInfos.Add( 
new ParamInfo("Address""test address",TypeFactory.GetStringType()));
            IList custs 
= ObjectLoader.Find( query, paramInfos );
            
// 根據期望的結果集寫Assertion.
        }


    }


}

在上面的代碼中,給出了四個較簡單的表達式加載的測試用例,它們都通過調用ObjectLoader對象的Find方法來取得數據,ObjectLoader是我們自己的數據加載器,它簡單的封裝了NHB中的數據加載功能。另外,我們還用一個PagerInfo類封裝了分頁數據,以方便數據傳遞。
注意:以前代碼是這麼寫的

[Test]
   public void LoadByNameTest() {
      Expression expr = Expression.Eq( “CompanyName”, “company name” );
      IList custs = ObjectLoader.Find( expr, typeof(Customer) );
      // 根據期望的結果集寫Assertion.
   }

對比下現在的寫法:
       [Test]
        public void LoadByNameTest()
        {
            ICriterion crit
= Expression.Eq("CompanyName", "company name");
            IList custs
= ObjectLoader.Find(crit, typeof(Customer));
           
// 根據期望的結果集寫Assertion.
        }

這個應該是NHibernate的版本引起的變化。
       

 2. HQL

HQL(Hibernate Query Language)是NHB的專用查詢語言,它完全面向對象!就是說只需要知道對象名和屬性名就可以生成HQL了,這樣就再也不用去理會數據表和列了,前面說的Expression查詢最終也會轉換爲HQL。
有兩種方式來執行HQL,一種是直接使用ISession的Find方法,另一種是使用IQuery接口。IQuery接口提供了一些額外的設置,最重要的就是分頁了,這個和ICriteria差不多,另外一些就是設置參數的值了。
NHB中有一組類專門用於完成數據加載,它們分別對應不同的數據加載情況,如實體加載、Criteria加載、OneToMany加載等。

下面同樣以加載Customer數據爲例來說明HQL的使用:
在上面的CustomerSystemFixture類中加入以下幾個測試用例:
public class CustomerSystemFixture {
   [Test]
   public void LoadAllTest () {
      string query = “ from Customer “;
      IList custs = ObjectLoader.Find( query, null );
      // 根據期望的結果集寫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 );
// 根據期望的結果集寫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 );
      // 根據期望的結果集寫Assertion.
   }
}
在上面的測試用例中,我們同樣將數據加載交由ObjectLoader的Find方法來處理,Find有很多重載的版本,都用於數據加載。另外還使用了一個ParameterInfo類來存儲HQL語句的參數信息。
// ParamInfo
public class ParamInfo {
   private string name; // 參數名稱
   private object value; // 參數值
   private IType type; // 參數類型
   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語句創建一個IQuery, 然後加入參數,接着設置分頁數據,最後返回列出的數據。

五 事務(沒有做,自己加吧)

既然而數據庫打交道,那麼事務處理就是必需的,事務能保整數據完整性。在NHB中,ITransaction對象只對.NET的事務對象(實現了IDbTransaction接口的對象)進行了簡單的封裝。

使用NHB的典型事務處理看起來像下面這樣(見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();
}
事務對象由ISession的BeginTransaction取得,同時事務開始,如果執行順利則提交事務,否則回滾事務。

當實現一個業務規則時,而這一規則要改變多個業務對象狀態時,這時就需要使用事務了,事務能保證所有改變要麼全部保存,要麼全部不保存!

在羅斯文商貿案例中,有這樣一個業務規則:
如果客戶對某一產品下了訂單,那麼被訂購產品的已訂購數量(UnitsOnOrder)應該加上客戶的產品訂單量,根據這一業務規則,創建一個測試用例如下:

[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!” );
   }
}

 

 

 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章