初探SchemaExport工具使用(轉(zhuǎn)帖)
本節(jié)內(nèi)容
- 引入
- SchemaExport工具
- SchemaUpdate工具
- 實例分析
- 結(jié)語
引入
我其實都是一直先編寫持久化類和映射文件,然后使用SchemaExport工具生成數(shù)據(jù)庫架構(gòu)。這樣的方式就是領(lǐng)域驅(qū)動設(shè)計/開發(fā)(DDD,Domain Driven Design/Development)。我的理解是系統(tǒng)的設(shè)計應(yīng)該基于對象模型,主要考慮對象的設(shè)計和邏輯上,然后按照對象模型建立數(shù)據(jù)庫關(guān)系模型,這才是現(xiàn)在面向?qū)ο箝_發(fā)的步驟,并不是上一篇先設(shè)計數(shù)據(jù)庫然后再設(shè)計對象。用一幅圖可以形象的說明領(lǐng)域驅(qū)動設(shè)計:
當(dāng)在設(shè)計時,我們的領(lǐng)域模型需要改變,只需修改NHibernate結(jié)構(gòu)和應(yīng)用程序,不需要修改數(shù)據(jù)庫架構(gòu),只要利用SchemaExport工具重新生成數(shù)據(jù)庫架構(gòu)就可以了。但是使用數(shù)據(jù)庫只是其中一種方式,我們也可以使用XML文件來存儲數(shù)據(jù)。
SchemaExport工具
NHibernate的hbm2dll提供SchemaExport工具:給定一個連接字符串和映射文件,不需輸入其他東西就可以按照持久化類和映射文件自動生成數(shù)據(jù)庫架構(gòu),現(xiàn)在SchemaExport工具還不是很強大,但是一般應(yīng)用足夠了,它還是一個相當(dāng)原始的API還在不斷改進(jìn)。
SchemaExport工具就是把DDL腳本輸出到標(biāo)準(zhǔn)輸出,同時/或者執(zhí)行DDL語句。SchemaExport工具提供了三個方法,分別是Drop()、Create()、Execute(),前兩個方法實質(zhì)是調(diào)用Execute()方法。通常使用Execute()方法來生成數(shù)據(jù)庫架構(gòu)的。
SchemaUpdate工具
在NHibernate2.0中新添加SchemaUpdate工具,可以用來更新數(shù)據(jù)庫架構(gòu)。但是我覺得沒有什么作用,因為它不能Drop現(xiàn)有的表或列,也不能更新現(xiàn)有的列,只能添加新的表和列。如果我需要刪除表或者列或者修改其中列,SchemaUpdate工具就顯得無能為力了。
實例分析
知道了上面的知識就好好實戰(zhàn)一下:看看具體怎么使用呢?
1.SchemaExport工具實戰(zhàn)
通常我們使用生成數(shù)據(jù)庫架構(gòu)代碼實例像這樣:
Configuration cfg=new Configuration(); cfg.Configure(); SchemaExport export =new SchemaExport(cfg); export.Execute(....);
1.準(zhǔn)備工作
現(xiàn)在數(shù)據(jù)訪問測試層新建一SchemaExportFixture.cs文件用于測試生成實戰(zhàn)。聲明一個全局變量_cfg,編寫 [SetUp]方法在每個測試方法執(zhí)行之前調(diào)用:
TestFixture] public class SchemaExportFixture { private Configuration _cfg; [SetUp] public void SetupContext() { _cfg = new Configuration(); _cfg.Configure(); } //測試...... }
2.測試Drop(script, export)方法
Test] public void DropTest() { var export = new SchemaExport(_cfg); export.Drop(true, true); }
Drop(script, export)方法根據(jù)持久類和映射文件執(zhí)行刪除數(shù)據(jù)庫架構(gòu)。有兩個參數(shù),第一個為True就是把DDL語句輸出到控制臺,第二個為True就是根據(jù)持久類和映射文件執(zhí)行刪除數(shù)據(jù)庫架構(gòu)操作,經(jīng)過調(diào)試可以發(fā)現(xiàn)Drop(script, export)方法其實質(zhì)是執(zhí)行了Execute(script, export, true, true)方法。
3.測試Create(script, export)方法
Test] public void CreateTest() { var export = new SchemaExport(_cfg); export.Create(true, true); }
Create(script,export)方法根據(jù)持久類和映射文件先刪除架構(gòu)后創(chuàng)建刪除數(shù)據(jù)庫架構(gòu)。有兩個參數(shù),第一個為True就是把DDL語句輸出到控制臺,第二個為True就是根據(jù)持久類和映射文件先執(zhí)行刪除再執(zhí)行創(chuàng)建操作,經(jīng)過調(diào)試可以發(fā)現(xiàn)這個方法其實質(zhì)是執(zhí)行Execute(script,export, false, true)方法。
4.測試Execute(script, export, justDrop, format)方法
Test] public void ExecuteTest() { var export = new SchemaExport(_cfg); export.Execute(true, true, false, false); }
Execute(script, export, justDrop, format)方法根據(jù)持久類和映射文件先刪除架構(gòu)后創(chuàng)建刪除數(shù)據(jù)庫架構(gòu)。有四個參數(shù),第一個為True就是把DDL語句輸出到控制臺;第二個為True就是根據(jù)持久類和映射文件在數(shù)據(jù)庫中先執(zhí)行刪除再執(zhí)行創(chuàng)建操作;第三個為false表示不是僅僅執(zhí)行Drop語句還執(zhí)行創(chuàng)建操作,這個參數(shù)的不同就擴(kuò)展了上面兩個方法;第四個參數(shù)為false表示不是格式化輸出DDL語句到控制臺,是在一行輸出的。
所謂格式化輸出就像這樣:
一行輸出就像這樣:
5.測試Execute(script, export, justDrop, format, connection, exportOutput)方法
Test] public void ExecuteOutTest() { var export = new SchemaExport(_cfg); var sb = new StringBuilder(); TextWriter output = new StringWriter(sb); export.Execute(true, false, false, false, null, output); }
Execute(script, export, justDrop, format, connection, exportOutput)方法根據(jù)持久類和映射文件先刪除架構(gòu)后創(chuàng)建刪除數(shù)據(jù)庫架構(gòu)。有六個參數(shù),第一個為True就是把DDL語句輸出到控制臺;第二個為false就是不執(zhí)行DDL語句;第五個為自定義連接。當(dāng)export為true執(zhí)行語句時必須打開連接。該方法不關(guān)閉連接,null就是使用默認(rèn)連接,最后一個參數(shù)自定義輸出,這里我輸出到TextWriter中。
2.SchemaUpdate工具實戰(zhàn)
現(xiàn)在數(shù)據(jù)訪問測試層新建一SchemaUpdateFixture.cs文件用于測試生成實戰(zhàn)。先聲明一個全局變量_cfg:
private Configuration _cfg;
這里我用另外一種方式配置映射文件,先定義兩個映射XML分別代表舊的和新的這樣才能體現(xiàn)測試更新數(shù)據(jù)庫架構(gòu)的意義。
舊映射XML:這里我使用Product持久化類,由于在之前我們定義了Product持久化類,這里直接模擬定義映射XML:擁有主鍵ProductId和Name字段。
public const string product_xml = "<?xml version='1.0' encoding='utf-8' ?>" + "<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'" + " assembly='DomainModel'" + " namespace='DomainModel'>" + " <class name='DomainModel.Entities.Product,DomainModel'>" + " <id name='ProductId'>" + " <generator class='native'/>" + " </id>" + " <property name='Name'/>" + " </class>" + "</hibernate-mapping>";
新映射XML:更新上面映射XML:主鍵ProductId(沒有改變);Name字段:添加不可為空和長度為50;另外增加了Cost字段,類型為float不可為空。
public const string newproduct_xml = "<?xml version='1.0' encoding='utf-8' ?>" + "<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'" + " assembly='DomainModel'" + " namespace='DomainModel'>" + " <class name='DomainModel.Entities.Product,DomainModel'>" + " <id name='ProductId'>" + " <generator class='native'/>" + " </id>" + " <property name='Name' not-null='true' length='50' />" + " <property name='Cost' type='float' not-null='true'/>" + " </class>" + "</hibernate-mapping>";
測試前利用舊映射XML創(chuàng)建數(shù)據(jù)庫架構(gòu):使用[SetUp]在測試前執(zhí)行,按照舊映射XML創(chuàng)建數(shù)據(jù)庫架構(gòu)并格式化輸出DDL語句:
SetUp] public void SetupContext() { //模擬舊系統(tǒng) _cfg = new Configuration(); _cfg.Configure(); _cfg.AddXml(product_xml); var export = new SchemaExport(_cfg); export.Execute(true, true, false, true); }
測試更新數(shù)據(jù)庫架構(gòu):使用SchemaUpdate類提供的唯一的Execute(script, doUpdate)方法按照新映射XML更新數(shù)據(jù)庫架構(gòu):
Test] public void UpdateExistingDatabaseSchemaTest() { _cfg = new Configuration(); _cfg.Configure().AddXml(newproduct_xml); var update = new SchemaUpdate(_cfg); update.Execute(true, true); }
測試輸出結(jié)果如圖所示,如果你覺得不放心再看看數(shù)據(jù)庫Product表。
看到了嗎?這顯然不是我要求的,首先按照舊映射XML創(chuàng)建了數(shù)據(jù)庫架構(gòu),但是更新數(shù)據(jù)庫架構(gòu)顯得無能為力,僅僅增加了Cost字段,我想更新Name字段屬性為不可為空和長度為50,但是SchemaUpdate工具不能做到!我覺得這個類目前還沒有什么作用,期待下一個版本來完善。
結(jié)語
這篇文章通過實例介紹NHibernate中提供兩個實用工具SchemaExport工具利用持久化類和映射文件生成數(shù)據(jù)庫架構(gòu)。SchemaUpdate工具通過持久化類和映射文件更新數(shù)據(jù)庫架構(gòu)。
posted on 2009-05-04 17:11 清晨 閱讀(517) 評論(0) 編輯 收藏 所屬分類: hibernate 的相關(guān)知識