ToolsCodeTemplate使用
Posted 长毛象|繁华落尽,勿忘初心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ToolsCodeTemplate使用相关的知识,希望对你有一定的参考价值。
最近学习使用CodeSmith代码生成器
CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。
作用:CodeSmith 是一种基于模板的代码生成工具,它使用类似于ASP.NET的语法来生成任意类型的代码或文本。与其他许多代码生成工具不同,CodeSmith 不要求您订阅特定的应用程序设计或体系结构。使用 CodeSmith,可以生成包括简单的强类型集合和完整应用程序在内的任何东西。
当您生成应用程序时,您经常需要重复完成某些特定的任务,例如编写数据访问代码或者生成自定义集合。CodeSmith 在这些时候特别有用,因为您可以编写模板自动完成这些任务,从而不仅提高您的工作效率,而且能够自动完成那些最为乏味的任务。CodeSmith 附带了许多模板,包括对应于所有 .NET 集合类型的模板以及用于生成存储过程的模板,但该工具的真正威力在于能够创建自定义模板。
应用:CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。
安装CodeSmith 2.6注册后发现有两个可运行程序CodeSmith Studio.exe和CodeSmith Explorer.exe
CodeSmith Studio.exe用来创建自定义模板
CodeSmith Explorer.exe用来导入模板并且生成代码
打开 CodeSmith Studio.exe,新建一个C#模板。发现有如下类似与asp.net的标识符号
<% %>
<%= %>
<%@ %>
<script runat="template"> </script>
官方站点:http://www.codesmithtools.com/
下面是使用CodeSmith常用的方法
1 using System; 2 using SchemaExplorer; 3 using System.Data; 4 using CodeSmith.Engine; 5 using System.Text.RegularExpressions; 6 7 /// <summary> 8 /// CodeSmith公用方法类 9 /// </summary> 10 public class ToolsCodeTemplate:CodeTemplate 11 { 12 #region 获取Molde类名称 13 /// <summary> 14 /// 获取Molde类名称 15 /// </summary> 16 /// <param name="table">表</param> 17 /// <returns>表名称(表名称即为Model类名称)</returns> 18 public string GetModelClassName(TableSchema table) 19 { 20 string result; 21 if ( table.ExtendedProperties.Contains("ModelName") ) 22 { 23 result = (string)table.ExtendedProperties["ModelName"].Value; 24 return MakePascal(result); 25 } 26 27 if (table.Name.EndsWith("s")) 28 { 29 result = MakeSingle(table.Name); 30 } 31 else 32 { 33 result = table.Name; 34 } 35 36 return MakePascal(result); 37 } 38 #endregion 39 40 #region 获取属性名称 41 /// <summary> 42 /// 获取属性名称 43 /// </summary> 44 /// <param name="column"></param> 45 /// <returns></returns> 46 public string GetPropertyName(ColumnSchema column) 47 { 48 return MakePascal(GetNameFromDBFieldName(column)); 49 } 50 #endregion 51 52 #region 获取从数据库字段得到的名称 53 /// <summary> 54 /// 获取从数据库字段得到的名称 55 /// </summary> 56 /// <param name="column"></param> 57 /// <returns></returns> 58 public string GetNameFromDBFieldName(ColumnSchema column) 59 { 60 return column.Name; 61 } 62 #endregion 63 64 #region 获取属性类型 65 /// <summary> 66 /// 获取属性类型 67 /// </summary> 68 /// <param name="column">列</param> 69 /// <returns>属性类型</returns> 70 public string GetPropertyType(ColumnSchema column) 71 { 72 return GetCSharpTypeFromDBFieldType(column); 73 } 74 #endregion 75 76 #region 获取主键名称 77 /// <summary> 78 /// 获取主键名称 79 /// </summary> 80 /// <param name="TargetTable">表</param> 81 /// <returns>主键名称</returns> 82 public string GetPKName(TableSchema TargetTable) 83 { 84 if (TargetTable.PrimaryKey != null) 85 { 86 if (TargetTable.PrimaryKey.MemberColumns.Count == 1) 87 { 88 return TargetTable.PrimaryKey.MemberColumns[0].Name; 89 } 90 else 91 { 92 throw new Exception("This template will not work on primary keys with more than one member column."); 93 } 94 } 95 else 96 { 97 throw new Exception("This template will only work on tables with a primary key."); 98 } 99 } 100 #endregion 101 102 #region 获取主键类型 103 /// <summary> 104 /// 获取主键类型 105 /// </summary> 106 /// <param name="TargetTable">表</param> 107 /// <returns>主键类型</returns> 108 public string GetPKType(TableSchema TargetTable) 109 { 110 if (TargetTable.PrimaryKey != null) 111 { 112 if (TargetTable.PrimaryKey.MemberColumns.Count == 1) 113 { 114 return GetCSharpTypeFromDBFieldType(TargetTable.PrimaryKey.MemberColumns[0]); 115 } 116 else 117 { 118 throw new ApplicationException("This template will not work on primary keys with more than one member column."); 119 } 120 } 121 else 122 { 123 throw new ApplicationException("This template will only work on MyTables with a primary key."); 124 } 125 } 126 #endregion 127 128 #region 类型转化 129 /// <summary> 130 /// 获取数据库类型转化为C#类型 131 /// </summary> 132 /// <param name="column">列</param> 133 /// <returns>C#类型的字符串</returns> 134 public string GetCSharpTypeFromDBFieldType(ColumnSchema column) 135 { 136 if (column.Name.EndsWith("TypeCode")) return column.Name; 137 string type; 138 switch (column.DataType) 139 { 140 case DbType.AnsiString: type= "string";break; 141 case DbType.AnsiStringFixedLength: type= "string";break; 142 case DbType.Binary: type= "byte[]";break; 143 case DbType.Boolean: type= "bool";break; 144 case DbType.Byte: type= "byte";break; 145 case DbType.Currency: type= "decimal";break; 146 case DbType.Date: type= "DateTime";break; 147 case DbType.DateTime: type= "DateTime";break; 148 case DbType.Decimal: type= "decimal";break; 149 case DbType.Double: type= "double";break; 150 case DbType.Guid: type= "Guid";break; 151 case DbType.Int16: type= "short";break; 152 case DbType.Int32: type= "int";break; 153 case DbType.Int64: type= "long";break; 154 case DbType.Object: type= "object";break; 155 case DbType.SByte: type= "sbyte";break; 156 case DbType.Single: type= "float";break; 157 case DbType.String: type= "string";break; 158 case DbType.StringFixedLength: type= "string";break; 159 case DbType.Time: type= "TimeSpan";break; 160 case DbType.UInt16: type= "ushort";break; 161 case DbType.UInt32: type= "uint";break; 162 case DbType.UInt64: type= "ulong";break; 163 case DbType.VarNumeric: type= "decimal";break; 164 default: 165 { 166 type= "__UNKNOWN__" + column.NativeType;//未知 167 break; 168 } 169 } 170 //是否为Null 171 if(column.AllowDBNull && column.SystemType.IsValueType) 172 { 173 type=type+"?"; 174 } 175 return type; 176 } 177 /// <summary> 178 /// 获取数据库类型转化为C#类型 179 /// </summary> 180 /// <param name="dbType">DbType的类型</param> 181 /// <returns>C#类型的字符串</returns> 182 public string GetDBTypeToCSharpType (System.Data.DbType dbType) 183 { 184 switch (dbType) 185 { 186 case DbType.AnsiString:return "string"; 187 case DbType.AnsiStringFixedLength:return "string"; 188 case DbType.Binary:return "byte[]"; 189 case DbType.Boolean:return "bool"; 190 case DbType.Byte:return "byte"; 191 case DbType.Currency:return "decimal"; 192 case DbType.Date:return "DateTime"; 193 case DbType.DateTime:return "DateTime"; 194 case DbType.DateTime2:return "DateTime"; 195 case DbType.DateTimeOffset:return "DateTime"; 196 case DbType.Decimal:return "decimal"; 197 case DbType.Double:return "double"; 198 case DbType.Guid:return "Guid"; 199 case DbType.Int16:return "short"; 200 case DbType.Int32:return "int"; 201 case DbType.Int64:return "long"; 202 case DbType.Object:return "object"; 203 case DbType.SByte:return "sbyte"; 204 case DbType.Single:return "float"; 205 case DbType.String:return "string"; 206 case DbType.StringFixedLength:return "string"; 207 case DbType.Time:return "DateTime"; 208 case DbType.UInt16:return "ushort"; 209 case DbType.UInt32:return "uint"; 210 case DbType.UInt64:return "ulong"; 211 case DbType.VarNumeric:return "decimal"; 212 case DbType.Xml:return "string"; 213 default:return "object"; 214 } 215 } 216 #endregion 217 218 #region 骆驼命名法,帕斯卡命名法和匈牙利命名法 219 /// <summary> 220 /// 获取首字母大写的字符串 221 /// </summary> 222 /// <param name="value">字符串(例如:xiangyisheng)</param> 223 /// <returns>xiangyisheng => Xiangyisheng</returns> 224 public string MakePascal(string value) 225 { 226 return value.Substring(0, 1).ToUpper() + value.Substring(1); 227 } 228 /// <summary> 229 /// 获取首字母小写的字符串 230 /// </summary> 231 /// <param name="value">字符串(例如:Xiangyisheng)</param> 232 /// <returns>Xiangyisheng => xiangyisheng</returns> 233 public string MakeCamel(string value) 234 { 235 return value.Substring(0, 1).ToLower() + value.Substring(1); 236 } 237 /// <summary> 238 /// 获取小写的字符串 239 /// </summary> 240 /// <param name="value">字符串(例如:XiangYiSheng)</param> 241 /// <returns>XiangYiSheng => xiangyisheng</returns> 242 public string MakeSmall(string value) 243 { 244 return value.ToLower(); 245 } 246 /// <summary> 247 /// 获取单数形式的字符串 248 /// </summary> 249 /// <param name="name">字符串(例如:Xiangyishengs)</param> 250 /// <returns>Xiangyishengs => Xiangyisheng</returns> 251 public string MakeSingle(string name) 252 { 253 Regex plural1 = new Regex("(?<keep>[^aeiou])ies$"); 254 Regex plural2 = new Regex("(?<keep>[aeiou]y)s$"); 255 Regex plural3 = new Regex("(?<keep>[sxzh])es$"); 256 Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$"); 257 258 if(plural1.IsMatch(name)) 259 return plural1.Replace(name, "${keep}y"); 260 else if(plural2.IsMatch(name)) 261 return plural2.Replace(name, "${keep}"); 262 else if(plural3.IsMatch(name)) 263 return plural3.Replace(name, "${keep}"); 264 else if(plural4.IsMatch(name)) 265 return plural4.Replace(name, "${keep}"); 266 267 return name; 268 } 269 /// <summary> 270 /// 获取复数形式的字符串 271 /// </summary> 272 /// <param name="name">字符串(例如:Xiangyisheng)</param> 273 /// <returns>Xiangyisheng => Xiangyishengs</returns> 274 public string MakePlural(string name) 275 { 276 Regex plural1 = new Regex("(?<keep>[^aeiou])y$"); 277 Regex plural2 = new Regex("(?<keep>[aeiou]y)$"webstorm代码片段的创建Android课程---Android Studio使用小技巧:提取方法代码片段