①外部变量:
<%@ PRoperty Name="SampleStringProperty" Default="SomeValue" Type="System.String" %>
表示定义一个string类型的外部变量,在需要在生成的时候才输入,此属性有默认值,也可以由用户在右下角的属性栏里修改属性的值。
还有Optional:是否允许为空(即不输入),Category:是说你声明的这个属性的类别(CodeSmith会按分类分开展示让你输入)。
②与数据库交互
CodeSmith与数据库的联系,在CodeSmith中自带一个程序集SchemaExplorer.dll,这个程序集中的类主要用于获取数据库中各种对象的结构。
1 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="False" Description="源表名" %>2 3 <%@ Property Name="SourceDB" Type="SchemaExplorer.DatabaseSchema" Optional="False" %>4 5 <%@ Assembly Name="SchemaExplorer" %>6 7 <%@ Import Namespace="SchemaExplorer" %>
Assembly:引用程序集,Import:相当于using命名空间。
Type="SchemaExplorer.DatabaseSchema"此类型会在属性栏生成一个数据库的选择框,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>标签内写的是自定义函数(C#代码)④模板部分书写:C#代码要用<%%>包括,值类型要使用<%=%>例如:生成一个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
新闻热点
疑难解答