当生成应用程序时,无论是编写数据访问代码还是生成自定义实体自定义集合,你会发现经常需要重复完成某些特定的任务。最近用codesmith编写了界面层到数据库访问层的模板以及用于生成存储过程的模板,感觉codesmith非常不错,不仅有利于提高团队的工作效率,自动完成那些最为乏味的任务,而且有利于让大家的代码保持一定的一致性和规范性。下面详细介绍一下如何生成自定义模板(以自定义实体为列),以作为总结,也希望对您有所帮助。
第一步是添加模板头,声明模板的语言、目标语言以及简要模板说明:
以下是代码片段: <%@ codetemplate language="c#" targetlanguage="text" src="" inherits="" debug="false" description="template description here." %> |
指明这是一个c#语言的模板。codesmith 包括一个名为 schemaexplorer 的特殊的程序集,可用来从表、存储过程或几乎任何其他 sql server对象生成模板。下面引入该集合和命名空间:
以下是代码片段: <%@ assembly name="system.data" %> <%@ import namespace="system.data" %> <%@ assembly name="schemaexplorer" %> <%@ import namespace="schemaexplorer" %> |
第二步,属性声明,在这里可声明将在模板每次运行时指定的属性,以方便设置一系列需要传入的参数:
以下是代码片段: <%@ property name="database" type="schemaexplorer.databaseschema" category="context" description="aaa" %> |
如上边所示,在进行代码生成时,在codesmith explorer中选择模板后生成代码的窗口中,变量的名称为database,类型是schemaexplorer.databaseschema,类别是context,当用户选中这个属性时对于属性的描述description。
以下是代码片段: <%@ 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" %> |
如上边所示,变量的名称为tablename,namespace,类型是string,类别是strings,当用户选中这个属性时对于属性的描述description。
以下是代码片段: <%@ property name="author" type="system.string" default="author" optional="false" category="" description="??" %> <%@ property name="description" type="system.string" default="description" optional="false" category="" description="?????" %> |
如上面所示,变量的名称为author,description,类型是string,类别为空(显示为“杂项”),当用户选中这个属性时对于属性的描述description。
相应属性界面显示如下图所示:
第三步,编写c#语言模板类部分代码。codesmith模板脚本格式:
以下是代码片段: public class codetemplaterule |
实例化类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; } |
函数作用:输入数据库名和表名,得到列集合信息。用内部函数columnschemacollection可以得到某张表的所有列的集合。
以下是代码片段: public string gettablename(string tablename) { int i=tablename.indexof("_"); return tablename.substring(i 1,tablename.length-(i 1)); } |
函数作用:对输入的表名格式化,得到去掉前缀的表名,如pe_user,返回user。
以下是代码片段: 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); } |
函数作用:对输入的名称格式化,得到首字母为大写的名称,如user,返回user。
第四步,编写实际生成模板主体的代码,根据你需要的格式输出。代码如下:
以下是代码片段: 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 编写,下载地址是: