用CodeSmith生成自定义模板
以下是代码片段: <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %> |
以下是代码片段: <%@ Assembly Name="System.Data" %> <%@ Import Namespace="System.Data" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> |
第二步,属性声明,在这里可声明将在模板每次运行时指定的属性,以方便设置一系列需要传入的参数:
以下是代码片段: <%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="aaa" %> |
以下是代码片段: <%@ Property Name="TableName" Type="System.String" Default="" Optional="False" Category="Strings" Description="bbb" %> <%@ Property Name="NameSpace" Type="System.String" Default="" Optional="True" Category="Strings" Description="cccc" %> |
以下是代码片段: <%@ Property Name="Author" Type="System.String" Default="Author" Optional="False" Category="" Description="??" %> <%@ Property Name="Description" Type="System.String" Default="Description" Optional="False" Category="" Description="?????" %> |
第三步,编写C#语言模板类部分代码。CodeSmith范本游戏脚本各式:
以下是代码片段: public class CodeTemplateRule |
以下是代码片段: public ColumnSchemaCollection GetColumnCollection(DatabaseSchema dataBase,string tableName) { TableSchemaCollection tables = new TableSchemaCollection(dataBase.Tables); ColumnSchemaCollection columns=null; for(int i=0;i { if(tables[i].Name.ToUpper()==tableName.ToUpper()) { TableSchema ts=tables[i]; columns=new ColumnSchemaCollection(ts.Columns); } } return columns; } |
以下是代码片段: public string GetTableName(string tableName) { int i=tableName.IndexOf("_"); return tableName.Substring(i+1,tableName.Length-(i+1)); } |
以下是代码片段: public string GetType(ColumnSchema column) { if (column.Name.EndsWith("TypeCode")) return column.Name; switch (column.DataType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"; case DbType.Byte: return "int"; case DbType.Currency: return "decimal"; case DbType.Date: return "DateTime"; case DbType.DateTime: 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"; default: { return "__UNKNOWN__" + column.NativeType; } } } |
以下是代码片段: public string GetDefaultValue(ColumnSchema column) { if (column.Name.EndsWith("TypeCode")) return column.Name; switch (column.DataType) { case DbType.AnsiString: return "\"\""; case DbType.AnsiStringFixedLength: return "\"\""; case DbType.Binary: return "null"; case DbType.Boolean: return "false"; case DbType.Byte: return "0"; case DbType.Currency: return "0"; case DbType.Date: return "DateTime.Parse(\"1900-1-1\")"; case DbType.DateTime: return "DateTime.Parse(\"1900-1-1\")"; case DbType.Decimal: return "0"; case DbType.Double: return "0"; case DbType.Guid: return "Guid.NewGuid().ToString()"; case DbType.Int16: return "0"; case DbType.Int32: return "0"; case DbType.Int64: return "0"; case DbType.Object: return "\"\""; case DbType.SByte: return "0"; case DbType.Single: return "0"; case DbType.String: return "\"\""; case DbType.StringFixedLength: return ""; case DbType.Time: return "DateTime.Parse(\"1900-1-1\")"; case DbType.UInt16: return "0"; case DbType.UInt32: return "0"; case DbType.UInt64: return "0"; case DbType.VarNumeric: return "0"; default: { return "__UNKNOWN__" + column.NativeType; } } } |
以下是代码片段: public string ConvPropertyName(string name) { return name.Substring(0,1).ToUpper() + name.Substring(1); } |
以下是代码片段: using System; namespace PowerEasy.Model.<%= NameSpace %> /// Description:<%= Description %> /// public class <%= rule.ConvPropertyName(rule.GetTableName(TableName)) %>Info { /// ///<%= Description %> /// public <%= rule.ConvPropertyName(rule.GetTableName(TableName)) %>Info() { } <% ColumnSchemaCollection columns=rule.GetColumnCollection(DataBase,TableName); for(int i=0;i { Response.Write(" //"+columns[i].Description+"\r\n"); Response.Write(" private "+rule.GetType(columns[i])+" m_"+columns[i].Name+";\r\n"); } %> #region <% for(int i=0;i { %> /// ///<%= columns[i].Description %> /// public <%= rule.GetType(columns[i]) %> <%= rule.ConvPropertyName(columns[i].Name) %> { get { return m_<%= columns[i].Name %>; } set { m_<%= columns[i].Name %>=value; } } <% } %> #endregion } } |
最后,在CodeSmithStudio.exe电脑运行模板图片 。填写你的参数,运行后就得到了你需要的实体代码:
以下是引用片段: using System; using System.Collections.Generic; using System.Text; namespace AAA /// Description:BBB实体 /// public class OrderInfo { /// ///Description /// public OrderInfo() { } //订单ID #region ///订单ID /// public int OrderId { get { return m_OrderId; } set { m_OrderId = value; } } /// ///订单编号 /// public string OrderNum { get { return m_OrderNum; } set { m_OrderNum = value; } } /// ///用户名 /// public string UserName { get { return m_UserName; } set { m_UserName = value; } } /// ///代理商名 /// public string AgentName { get { return m_AgentName; } set { m_AgentName = value; } } /// ///客户ID /// public int ClientId { get { return m_ClientId; } set { m_ClientId = value; } } /// ///订单总金额 /// public decimal MoneyTotal { get { return m_MoneyTotal; } set { m_MoneyTotal = value; } } /// ///购买商品合计金额 /// public decimal MoneyGoods { get { return m_MoneyGoods; } set { m_MoneyGoods = value; } } /// ///是否需要开发票 /// public bool NeedInvoice { get { return m_NeedInvoice; } set { m_NeedInvoice = value; } } /// ///发表内容,包括抬头、商品名称、金额等 /// public string InvoiceContent { get { return m_InvoiceContent; } set { m_InvoiceContent = value; } } /// ///是否已开发票 /// public bool Invoiced { get { return m_Invoiced; } set { m_Invoiced = value; } } /// ///备注 /// public string Remark { get { return m_Remark; } set { m_Remark = value; } } /// ///已收款 /// public decimal MoneyReceipt { get { return m_MoneyReceipt; } set { m_MoneyReceipt = value; } } /// ///开始服务日期 /// public DateTime BeginDate { get { return m_BeginDate; } set { m_BeginDate = value; } } /// ///录入时间 /// public DateTime InputTime { get { return m_InputTime; } set { m_InputTime = value; } } /// ///受货人姓名 /// public string ContacterName { get { return m_ContacterName; } set { m_ContacterName = value; } } /// ///收货人地址 /// public string Address { get { return m_Address; } set { m_Address = value; } } /// ///邮编 /// public string ZipCode { get { return m_ZipCode; } set { m_ZipCode = value; } } /// ///手机 /// public string Mobile { get { return m_Mobile; } set { m_Mobile = value; } } /// ///联系电话 /// public string Phone { get { return m_Phone; } set { m_Phone = value; } } /// ///EMAIL /// public string Email { get { return m_Email; } set { m_Email = value; } } /// ///付款方式 /// public int PaymentType { get { return m_PaymentType; } set { m_PaymentType = value; } } /// ///送货方式 /// public int DeliverType { get { return m_DeliverType; } set { m_DeliverType = value; } } /// ///订单状态 /// public int Status { get { return m_Status; } set { m_Status = value; } } /// ///物流状态 /// public int DeliverStatus { get { return m_DeliverStatus; } set { m_DeliverStatus = value; } } /// ///是否开通下载 /// public bool EnableDownload { get { return m_EnableDownload; } set { m_EnableDownload = value; } } /// ///返还的现金券 /// public decimal PresentMoney { get { return m_PresentMoney; } set { m_PresentMoney = value; } } /// ///赠送点券 /// public int PresentPoint { get { return m_PresentPoint; } set { m_PresentPoint = value; } } /// ///得到的积分 /// public int PresentExp { get { return m_PresentExp; } set { m_PresentExp = value; } } /// ///付款方式的折扣 /// public double DiscountPayment { get { return m_Discount_Payment; } set { m_Discount_Payment = value; } } /// ///运费 /// public decimal ChargeDeliver { get { return m_Charge_Deliver; } set { m_Charge_Deliver = value; } } /// ///客户姓名 /// public string ClientName { get { return m_ClientName; } set { m_ClientName = value; } } #endregion } } |
CodeSmith 由 Eric J. Smith 编写,下载地址是:
用户登录
还没有账号?
立即注册