CodeSmith模板

重复性的工作交给代码生成器就好,这里分享几套模板,

1.从数据库抓取字段,生成Table元素,这个在web开发中很有用

  1 <%-- 
  2 Name:
  3 Author: 
  4 Description: 
  5 --%>
  6 <%@ Template Language="C#" TargetLanguage="Text" %>
  7 
  8 <%@ Assembly Name="SchemaExplorer" %> 
  9 <%@ Import Namespace="SchemaExplorer" %>  
 10 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 11       Category="Context"
 12       Description="Table that the stored procedures should be based on." %>
 13       <%@ Property Name="SortColsNum" Type="System.Int32" Default="4" Optional="True" Category="Int" Description="每行显示栏数"  %>
 14       
 15 <form id="f_edit">
 16   <table cellpadding="0" cellspacing="0" style=" 100%; text-align: left; table-layout: fixed; border: 1px solid #BABABA; padding: 8px 4px 4px 4px;margin-bottom:4px ">
 17               <% int i=0;%>  
 18 <% foreach (ColumnSchema column in this.SourceTable.Columns) 
 19   { %>
 20   <% i++; %>
 21   <% if (i%SortColsNum==1 || SortColsNum==1)
 22      { %>
 23         <tr>
 24   <% } %>
 25      <td class="myothertdnm"><%=GetName(GetColumnName( column.Name)).ToUpper()%>:</td>
 26   <% if (i%SortColsNum==0)
 27      { %>
 28      <td class="myothertdlast"><input type="text" name="<%=GetName(GetColumnName( column.Name))%>" class="easyui-validatebox" required="true" validtype="length[1,5]" style="96%;" /></td>
 29   <% } %>
 30   <% else
 31      { %>
 32      <td class="myothertdnm"><input type="text" name="<%=GetName(GetColumnName( column.Name))%>" class="easyui-validatebox" required="true" validtype="length[1,5]" style="96%;" /></td>
 33   <% } %>
 34 <% if (i%SortColsNum==0) 
 35      { %>
 36         </tr>
 37   <% } %>
 38 <% } %>
 39 
 40 <% if ((i%SortColsNum!=0 && i%SortColsNum!=1)||(SortColsNum<i&&SortColsNum!=1))
 41      { %>
 42         </tr>
 43   <% } %>
 44   </table>
 45 </form>
 46             
 47 <script runat="template">
 48 public string GetClassName(string name)
 49     {
 50         string returnName="";
 51          
 52         if(name.Length >0)
 53         {
 54             string[] names = name.Split('_');
 55             foreach(string item in names)
 56             {
 57                 
 58                  returnName+= GetFunctionStr(item);
 59             }
 60             returnName ="Ent"+returnName;
 61         }
 62         
 63         
 64         return returnName;
 65         }
 66     public string GetColumnName(string name)
 67     { 
 68         string returnName="";
 69          
 70         if(name.Length >0)
 71         {
 72             string[] names = name.Split('_');
 73             foreach(string item in names)
 74             {
 75                 
 76                  returnName+= GetFunctionStr(item);
 77                 }
 78         } 
 79         return returnName;
 80     }
 81         
 82 public string GetFunctionStr(string name)
 83 {
 84     string nameLower;
 85     if(name.Length >0)
 86     {
 87         nameLower = name.ToLower();
 88         return nameLower.Substring(0,1).ToUpper()+nameLower.Substring(1);
 89         }
 90     return "";
 91     }
 92 
 93 public string GetType(SchemaExplorer.ViewColumnSchema field)
 94 {     
 95             string nativeType  = field.NativeType.ToLower();
 96             string result = "no:"+nativeType;
 97 
 98             switch(nativeType)
 99             {
100             case "nvarchar2": result ="string";break;
101             case "varchar2": result ="string";break;
102             case "char": result ="string";break;
103             case "number": result ="decimal?";break;
104             case "long": result ="double";break;
105             case "date": result ="System.DateTime?";break;
106             } 
107             
108             switch(field.DataType)
109             {
110             case DbType.AnsiString:result= "string" ;break;
111             case DbType.AnsiStringFixedLength:result= "string" ;break;
112             case DbType.Boolean:result= "bool" ;break;
113             case DbType.Date:result= "DateTime?" ;break;
114             case DbType.Double:result="double?" ;break;
115             case DbType.Decimal:result="decimal?" ;break;
116             case DbType.DateTime:result= "DateTime?" ;break;
117             case DbType.Int32:result= "int?" ;break; 
118             
119             }
120             
121             return result;
122 }
123 
124 public string  GetName(string name)
125 {
126      return name;
127 }
128 </script>
从数据库Table生成html

 CodeSmith可以去网上下载,pj版的比较多,涉及版权,我就不放了,

基本上重复性的工作都可以交给它

原文地址:https://www.cnblogs.com/michael90/p/6015689.html