微型項目實踐(2):用測試驅動代碼產生

來源:互聯網
上載者:User

上一篇文章中,我們分析了系統的兩個實體類,並且給出了對應的實體XML。今天我們來看看如何根據 這個XML進行代碼產生。

  1: <?xml version="1.0" encoding="utf-8" ?>  2: <Entities xmlns="http://it.ouc.edu.cn/EntityDescription/V2">  3:  <Entity title="日誌" name="Blog" module="Blogs">  4:   <Item title="標題" name="Title" type="text" require="true"/>  5:   <Item title="內容" name="Content" type="longtext" require="false"/>  6:   <Item title="所屬分類" name="BlogClass" type="entity" entityName="BlogClass" require="false"/>  7:   <Item title="建立時間" name="CreateDateTime" type="datetime" require="true"/>  8:   <Item title="更新時間" name="UpdateDateTime" type="datetime" require="true"/>  9:  </Entity> 10:  <Entity title="日誌分類" name="BlogClass" module="Blogs"> 11:   <Item title="名稱" name="Name" type="text" require="true"/> 12:   <Item title="描述" name="Description" type="text" require="false"/> 13:  </Entity> 14: </Entities>

這裡我們主要是產生兩種最重要的代碼,一是資料庫的建庫指令碼,二是實體的CS類。

我們使用測試來驅動代碼產生,在項目DongBlog.Test中添加一個新的單元測試:Util.cs。對應的代 碼如下:

  1: using ... 12:  13: namespace DongBlog.Test 14: { 15:   /// <summary> 16:   /// 資料庫相關的工具方法 17:   /// </summary> 18:   [TestClass] 19:   public class DatabaseUtil 20:   { 21:     /// <summary> 22:     /// 構造建庫指令碼 23:     /// </summary> 24:     [TestMethod, Description("構造建庫指令碼")] 25:     public void Util_CreateDatabaseScript() 26:     { 27:       var entities = getEntities(); 28:       string sqlText = MsSqlServerScriptBuilder.getSqlScript(entities); 29:  30:       Debug.WriteLine(""); 31:       Debug.WriteLine("The following is the database creating script:"); 32:       Debug.WriteLine("=============================彪悍的分割線=============================================================================="); 33:       Debug.WriteLine(""); 34:       Debug.WriteLine(""); 35:       Debug.WriteLine(sqlText); 36:       Debug.WriteLine(""); 37:       Debug.WriteLine(""); 38:       Debug.WriteLine("=============================又見彪悍的分割線=========================================================================="); 39:     } 40:  41:     private Entity[] getEntities() 42:     { 43:       XmlDocument document = new XmlDocument(); 44:       document.Load(Gobal.EntityXmlFileName); 45:       return EntityXmlParser.ParseXml(document); 46:     } 47:   } 48: }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.