标签:des style blog http io ar color 使用 sp
看了黄聪写的 黄聪:如何使用CodeSmith批量生成代码(原创系列教程),感觉可以马上自己练练手。
写了两个文件:Database.cst,Table.cst。Database引用table,然后生成数据库中所有表的Model类,代码如下:
//模板第一句?,必?的 <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %> //引用程序集 <%@ Assembly Name="SchemaExplorer" %> //加载程序集中的命名空间 <%@ Import Namespace="SchemaExplorer" %> //定义模板参数 <%@ Property Name="Table" Type="SchemaExplorer.TableSchema" Default="" Optional="False" Description=""%> //模板内容 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Fw.Ts.Domain { [ModelExtension] public partial class <%=Table.Name%> { <%foreach(ColumnSchema col in Table.Columns) {%> [ModelColumnExtension("<%=Table.Name%>", "<%=col.Name%>", "<%=Table.Name%>.Id")] public <%=this.DataType2CSharpType(col.DataType)%> <%=col.Name%> { get; set; } <%}%> } } //自定义函数,可直接用this引用 <script runat="Template"> public string DataType2CSharpType(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"; case DbType.Byte: return "byte"; case DbType.Currency: return "decimal"; case DbType.Date: return "DateTime"; case DbType.DateTime: return "DateTime"; case DbType.DateTime2: return "DateTime"; case DbType.DateTimeOffset: return "DateTime"; case DbType.Decimal: return "decimal"; case DbType.Double: return "double"; case DbType.Guid: return "Guid"; case DbType.Int16: return "short"; case DbType.Int32: return "int"; case DbType.Int64: return "long"; case DbType.Object: return "object"; case DbType.SByte: return "sbyte"; case DbType.Single: return "float"; case DbType.String: return "string"; case DbType.StringFixedLength: return "string"; case DbType.Time: return "TimeSpan"; case DbType.UInt16: return "ushort"; case DbType.UInt32: return "uint"; case DbType.UInt64: return "ulong"; case DbType.VarNumeric: return "decimal"; case DbType.Xml: return "string"; default: return "object"; } } </script>
//模板第一句话,必须的 <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %> //引用程序集 <%@ Assembly Name="SchemaExplorer" %> //加载程序集中的命名空间 <%@ Import Namespace="SchemaExplorer" %> //定义模板参数 <%@ Property Name="DataSource" Type="SchemaExplorer.DatabaseSchema" Default="" Optional="False" Description=""%> //注册外部的模板,注册成变量,能在当前的模板中直接使用,如 CodeTemplate Template = new EntityTemplate(); <%@ Register Name="EntityTemplate" Template="Table.cst" MergeProperties="False" ExcludeProperties="" %> //执行模板 <% //运行 this.GenerateEntityClasses(); Console.WriteLine("OK"); %> //自定义函数,可直接用this引用 <script runat="template"> private void GenerateEntityClasses() { CodeTemplate Template =new EntityTemplate(); foreach(TableSchema table in this.DataSource.Tables) { string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs"; Template.SetProperty("Table",table); Template.RenderToFile(FileDirectory,true); } } private string Directory = String.Empty; //设置输出路径,特性[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Optional, NotChecked] [DefaultValue("")] public string OutputDirectory { get { return Directory; } set { if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1); Directory = value; } } </script>
生成结果:
标签:des style blog http io ar color 使用 sp
原文地址:http://www.cnblogs.com/mylct/p/4151642.html