最近這段時間特忙,公事私事,忙得有時都沒時間打開電腦了,這兩周只能盡量更新,以后再將章節補回來。
直接進入主題,通過上一章節,大家明白了怎么使用模板類編寫T4模板,本章進的是一些簡單技巧的應用
1、首先創建一個Test2.tt模板
2、然后修改模板內容為下面代碼
這些代碼與上一章最后面的那個差不多,只是修改了輸出文件名、命名空間、類名、類屬性(partial)和一個單例獲取函數
1 <#@ template debug="false" hostspecific="True" language="C#" #> 2 <#@ output extension=".cs" encoding="utf-8" #> 3 <#@ include file="SQLServer.ttinclude" #> 4 <#@ include file="MultPRivate static <#=tbl.Name#>Bll _<#=tbl.Name#>Bll = null;29 30 /// <summary>31 /// 獲取本邏輯類單例32 /// </summary>33 /// <returns></returns>34 public static <#=tbl.Name#>Bll GetInstence() {35 if (_<#=tbl.Name#>Bll == null) {36 _<#=tbl.Name#>Bll = new <#=tbl.Name#>Bll();37 }38 return _<#=tbl.Name#>Bll;39 }40 #endregion41 42 }43 44 }45 46 47 <# 48 // 輸出文件結束49 manager.EndBlock();50 } //if(!ExcludeTables.Contains(tbl.Name)) 判斷結束51 52 }// end foreach53 54 // 執行編譯,生成文件55 manager.Process(true); 56 #>
運行模板,測試看看效果
從上面添加函數的方法可以看出,對于常用的函數,都可以在模板中進行添加后,直接生成出來,這樣就減少了程序員很大的工作量了
3、用上面方法確實可以解決很大部分的問題,但對于一些特殊的函數直接這樣寫就不行了,比如我們想生成一個刪除指定外鍵Id的函數,由于有的表有外鍵有的沒有,那么就要用上一些簡單的判斷來處理了
1 <#@ template debug="false" hostspecific="True" language="C#" #> 2 <#@ output extension=".cs" encoding="utf-8" #> 3 <#@ include file="SQLServer.ttinclude" #> 4 <#@ include file="MultipleOutputHelper.ttinclude"#> 5 6 <# 7 //獲取所有表與視圖 8 var tables = LoadTables(); 9 //創建多文件生成實體10 var manager = Manager.Create(Host, GenerationEnvironment); 11 12 //遍歷所有表13 foreach(var tbl in tables){14 //判斷當前表名是否是禁用名稱(禁用的名稱可以在Settings.ttinclude文件的ExcludeTables字符串數據中進行添加)15 if(!ExcludeTables.Contains(tbl.Name))16 {17 // 設置輸出的文件名18 manager.StartNewFile(tbl.ClassName+"Bll.cs");19 #> 20 using System;21 using Solution.Dataaccess.DataModel;22 using Solution.DataAccess.DbHelper;23 24 namespace Solution.Logic {25 26 public partial class <#=tbl.CleanName#>Bll {27 28 #region 單例模式29 //定義單例實體30 private static <#=tbl.Name#>Bll _<#=tbl.Name#>Bll = null;31 32 /// <summary>33 /// 獲取本邏輯類單例34 /// </summary>35 /// <returns></returns>36 public static <#=tbl.Name#>Bll GetInstence() {37 if (_<#=tbl.Name#>Bll == null) {38 _<#=tbl.Name#>Bll = new <#=tbl.Name#>Bll();39 }40 return _<#=tbl.Name#>Bll;41 }42 #endregion43 44 45 <#46 foreach(var col in tbl.Columns){47 //判斷字段名稱中是否包含“_Id”這個字符串,且字段類型為int或long的,則生成對應的刪除函數48 if (col.CleanName.IndexOf("_Id") >= 0 && (col.SysType == "int" || col.SysType == "long"))49 {50 #>51 #region 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄52 /// <summary>53 /// 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄54 /// </summary>55 /// <param name="id">記錄的主鍵值</param>56 public void DeleteBy<#=col.CleanName#>(int id) {57 //刪除58 <#=tbl.Name#>.Delete(x => x.<#=col.CleanName#> == id);59 }60 61 /// <summary>62 /// 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄63 /// </summary>64 /// <param name="id">記錄的主鍵值</param>65 public void DeleteBy<#=col.CleanName#>(int[] id) {66 if (id == null) return;67 //將數組轉為逗號分隔的字串68 var str = string.Join(",", id);69 70 //設置Sql語句71 var sql = "delete from <#=tbl.Name#> where <#=col.CleanName#> in (" + str + ")";72 73 //刪除74 var deleteHelper = new DeleteHelper();75 deleteHelper.Delete(sql);76 }77 #endregion78 79 <#80 }81 }82 #>83 }84 85 }86 87 88 <# 89 // 輸出文件結束90 manager.EndBlock();91 } //if(!ExcludeTables.Contains(tbl.Name)) 判斷結束92 93 }// end foreach94 95 // 執行編譯,生成文件96 manager.Process(true); 97 #>
運行模板,測試看看效果
4、同理,我們可以通過判斷,生成獲取指定名稱的字段值和生成刪除圖片函數等,這些都可以根據你的需要去生成
1 <#@ template debug="false" hostspecific="True" language="C#" #> 2 <#@ output extension=".cs" encoding="utf-8" #> 3 <#@ include file="SQLServer.ttinclude" #> 4 <#@ include file="MultipleOutputHelper.ttinclude"#> 5 6 <# 7 //獲取所有表與視圖 8 var tables = LoadTables(); 9 //創建多文件生成實體 10 var manager = Manager.Create(Host, GenerationEnvironment); 11 12 //遍歷所有表 13 foreach(var tbl in tables){ 14 //判斷當前表名是否是禁用名稱(禁用的名稱可以在Settings.ttinclude文件的ExcludeTables字符串數據中進行添加) 15 if(!ExcludeTables.Contains(tbl.Name)) 16 { 17 // 設置輸出的文件名 18 manager.StartNewFile(tbl.ClassName+"Bll.cs"); 19 #> 20 using System; 21 using Solution.DataAccess.DataModel; 22 using Solution.DataAccess.DbHelper; 23 24 namespace Solution.Logic { 25 26 public partial class <#=tbl.CleanName#>Bll { 27 28 #region 單例模式 29 //定義單例實體 30 private static <#=tbl.Name#>Bll _<#=tbl.Name#>Bll = null; 31 32 /// <summary> 33 /// 獲取本邏輯類單例 34 /// </summary> 35 /// <returns></returns> 36 public static <#=tbl.Name#>Bll GetInstence() { 37 if (_<#=tbl.Name#>Bll == null) { 38 _<#=tbl.Name#>Bll = new <#=tbl.Name#>Bll(); 39 } 40 return _<#=tbl.Name#>Bll; 41 } 42 #endregion 43 44 <# 45 foreach(var col in tbl.Columns){ 46 //判斷字段名稱中是否包含“_Id”這個字符串,且字段類型為int或long的,則生成對應的刪除函數 47 if (col.CleanName.IndexOf("_Id") >= 0 && (col.SysType == "int" || col.SysType == "long")) 48 { 49 #> 50 #region 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄 51 /// <summary> 52 /// 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄 53 /// </summary> 54 /// <param name="id">記錄的主鍵值</param> 55 public void DeleteBy<#=col.CleanName#>(int id) { 56 //刪除 57 <#=tbl.Name#>.Delete(x => x.<#=col.CleanName#> == id); 58 } 59 60 /// <summary> 61 /// 刪除<#=tbl.Name#>表指定<#=col.CleanName#>的字段值記錄 62 /// </summary> 63 /// <param name="id">記錄的主鍵值</param> 64 public void DeleteBy<#=col.CleanName#>(int[] id) { 65 if (id == null) return; 66 //將數組轉為逗號分隔的字串 67 var str = string.Join(",", id); 68 69 //設置Sql語句 70
新聞熱點
疑難解答