国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

CodeSmith自己動手寫模板

2019-11-17 01:53:52
字體:
供稿:網(wǎng)友

CodeSmith自己動手寫模板

CodeSmith學(xué)習(xí)筆記------

1.新建一個(gè)Code Smith Generator Template(C sharp)

2.一些常見標(biāo)簽的解釋:

①外部變量:

<%@ PRoperty Name="SampleStringProperty" Default="SomeValue" Type="System.String" %>

表示定義一個(gè)string類型的外部變量,在需要在生成的時(shí)候才輸入,此屬性有默認(rèn)值,也可以由用戶在右下角的屬性欄里修改屬性的值。

還有Optional:是否允許為空(即不輸入),Category:是說你聲明的這個(gè)屬性的類別(CodeSmith會按分類分開展示讓你輸入)。

②與數(shù)據(jù)庫交互

CodeSmith與數(shù)據(jù)庫的聯(lián)系,在CodeSmith中自帶一個(gè)程序集SchemaExplorer.dll,這個(gè)程序集中的類主要用于獲取數(shù)據(jù)庫中各種對象的結(jié)構(gòu)。

1 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="False" Descr
Assembly:引用程序集,Import:相當(dāng)于using命名空間。
Type="SchemaExplorer.DatabaseSchema"此類型會在屬性欄生成一個(gè)數(shù)據(jù)庫的選擇框,Type="SchemaExplorer.TableSchema"即表的選擇框。③自定義方法:
1 <script runat="template">2 // My methods here.3 public string SampleMethod()4 {5   return "Method output.";6 }7 </script>
<script runat="template"></script>標(biāo)簽內(nèi)寫的是自定義函數(shù)(C#代碼)④模板部分書寫:C#代碼要用<%%>包括,值類型要使用<%=%>例如:生成一個(gè)Model層的模板
 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4  5 Namespace Model 6 { 7     Class <%=TargetTable.Name%> 8     { 9         <%  for (int i=0;i<TargetTable.Columns.Count;i++)10         {11             SchemaExplorer.ColumnSchema col = TargetTable.Columns[i];%>12             public <%=col.SystemType%> <%=col.Name%>{get;set;}13       <%}%>14     }15 }

⑤一份完整的DAL的模板示例:

  1 <%@ CodeTemplate Language="C#" TargetLanguage="C#"   2     Src="ToolsCodeTemplate.cs" Inherits="ToolsCodeTemplate"%>  3 <%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>  4 <%@ Property Name="ModelsNamespace" Default="MyOffice.Models" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>  5 <%@ Property Name="DALNamespace" Default="MyOffice.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>  6 <%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>  7 <%@ Assembly Name="SchemaExplorer" %>  8 <%@ Assembly Name="System.Data" %>  9 <%@ Import Namespace="SchemaExplorer" %> 10 <%@ Import Namespace="System.Data" %> 11 <%@ Import Namespace="System.Text.RegularExpressions" %> 12 <% PrintHeader(); %> 13 using System; 14 using System.Collections.Generic; 15 using System.Text; 16 using System.Data; 17 using System.Data.SqlClient; 18 using <%= ModelsNamespace %>; 19  20 namespace <%= DALNamespace %> 21 { 22     public partial class <%= GetDALClassName() %> 23     { 24         <%-- public static Book AddBook(Book book) --%> 25         public <%= GetModelClassName() %> Add 26             (<%= GetModelClassName() %> <%= GetModelParamName() %>) 27         { 28             <%if(IsIdentityPK()) 29             {%> 30                 string sql ="<%= GetAutoIncInsertSQLLine()%>"; 31                 SqlParameter[] para = new SqlParameter[] 32                     { 33                         <% 34                         for(int i=0; i<TargetTable.NonPrimaryKeyColumns.Count; i++) 35                         { 36                             ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i]; 37                              38                         %> 39                         new SqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>)), 40                         <% 41                         } 42                         %> 43                     }; 44                      45                 <%= GetPKPropertyType() %> newId = (<%= GetPKPropertyType() %>)SqlHelper.ExecuteScalar(sql, para); 46                 return GetBy<%= GetPKPropertyName() %>(newId); 47             <%}else 48             {%> 49                 string sql ="<%= GetCommonInsertSQLLine()%>"; 50                 SqlParameter[] para = new SqlParameter[] 51                     { 52                         <% 53                         for(int i=0; i<TargetTable.Columns.Count; i++) 54                         { 55                             ColumnSchema column = TargetTable.Columns[i];                             56                         %> 57                         new SqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>)), 58                         <% 59                         } 60                         %> 61                     }; 62                 SqlHelper.ExecuteNonQuery(sql, para); 63                 return <%= GetModelParamName() %>;                 64             <%}%> 65         } 66  67         <%-- public static bool DeleteBookById(int id) --%> 68         public int DeleteBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>) 69         { 70             string sql = "DELETE <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>"; 71  72            SqlParameter[] para = new SqlParameter[] 73             { 74                 new SqlParameter("@<%= GetPKName() %>", <%= GetPKParamName() %>) 75             }; 76          77             return SqlHelper.ExecuteNonQuery(sql, para); 78         } 79          80                  81         <%-- public static bool ModifyBook(Book book) --%> 82         public int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>) 83         { 84             string sql = 85                 "UPDATE <%= TargetTable.Name %> " + 86                 "SET " + 87             " <%= TargetTable.NonPrimaryKeyColumns[0].Name %> = @<%= TargetTable.NonPrimaryKeyColumns[0].Name %>"  88             <% 89             for(int i=1; i<TargetTable.NonPrimaryKeyColumns.Count; i++) 90             { 91                 ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i];                 92             %> 93                 +", <%= column.Name %> = @<%= column.Name %>"  94             <% 95             } 96             %> 97                 98             +" WHERE <%= GetPKName() %> = @<%= GetPKName() %>"; 99 100 101             SqlParameter[] para = new SqlParameter[]102             {103                 new SqlParameter("@<%= GetPKName() %>", <%= GetModelParamName() %>.<%= GetPKName() %>)104                 <%105                 for(int i=0; i<TargetTable.NonPrimaryKeyColumns.Count; i++)106                 {107                     ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i];108                 %>109                     ,new SqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>))110                 <%111                 }112                 %>113             };114 115             return SqlHelper.ExecuteNonQuery(sql, para);116         }        117         118         <%-- public static Book GetBookById(int id) --%>119         public <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)120         {121             string sql = "SELECT * FROM <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";122             using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@<%= GetPKPropertyName() %>", <%= GetPKParamName() %>)))123             {124
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凯里市| 巢湖市| 万盛区| 宜春市| 高平市| 耿马| 即墨市| 西畴县| 炎陵县| 巴中市| 喀喇沁旗| 墨竹工卡县| 南康市| 皋兰县| 安新县| 临夏县| 格尔木市| 凤翔县| 南充市| 周宁县| 翼城县| 山阳县| 石河子市| 南充市| 金山区| 梁河县| 临湘市| 宁蒗| 抚州市| 吴川市| 贵定县| 佛坪县| 石林| 江口县| 莎车县| 民乐县| 株洲县| 梅河口市| 墨江| 九江市| 易门县|