当前位置: 首页 >> 程序设计 >> 写自己的代码自动生成模板
 

写自己的代码自动生成模板

作者:      来源:zz     发表时间:2007-01-14     浏览次数:      字号:    

你软件中有多少代码是自动生成? 也许你会说,自动生成的代码就是重复代码,是程序本身有问题,但是数据库开发,有些代码不是重复代码,但又需要自动生成。需要自动生成的原因是因为数据库本身包含了大量的信息(也许你又会说了,class->database才是正道),不是重复代码的原因是因为每个对象的信息又都是不一样的。

  很多自动代码生成的软件都是通过简单模板来实现的,通过一些自定义的TAG来实现一定的控制,我早期的ceg就是这样的,它伴随着我几个O/R Mapping框架的应用。后来,我的sinolib中的映射XML文件同样需要自动生成,可是ceg的简单模板已经无法再支持映射文件的灵活性了。

  于是CodeDG就不得不出来了,它使用了script control来完成脚本解析,实现了代码生成的真正灵活,并且以统一的接口向脚本提供不同平台的数据库结构对象的信息。

以下是sinolib的XML映射的例子:

<?xml version=”1.0″ encoding = “GB2312″ ?>
<sino-mapping>
   
<class name = “AppRole”>
       
<property name = “uid” extname = “唯一识别码” type=”string”  key=”primary”/>
       
<property name = “code” extname = “简码” type=”string”/>
       
<property name = “name” extname = “名称” type=”string” notnull=”true”/>
       
<property name = “remark” extname = “备注” type=”string”/>
       
<property name = “isdelete” extname = “删除标志” type=”boolean” notnull=”true”/>
   
</class>
</sino-mapping>

这是一CodeDG内置的一个示例脚本:

This is a example script template.
You can write script
as same as what’s in ASP.

<%
test function
function max(a,b)
    max
= iif(a>b,a,b)
end function

max_value = max(1,2)
msgbox max_value

response.write
Table: &  table.name & vbcrlf
response.write
Comment: &  table.description & vbcrlf
response.write
Column count: &  table.columns.count & vbcrlf

response.write ========Traverse all column’s property========= & vbcrlf

for each cm in Table.columns.items

%>column:<%
    response.write vbtab 
& cm.name
    response.write vbcrlf
%
>    Primary Key:<%
    response.write vbtab 
& cm.pk
    response.write vbcrlf
%
>    Auto Identity:<%
    response.write vbtab 
& cm.autoid
    response.write vbcrlf
%
>    DataType:<%
    response.write vbtab 
& cm.datatype
    response.write vbcrlf
%
>    Nullable:<%
    response.write vbtab 
& cm.nullable
    response.write vbcrlf
%
>    Length:<%
    response.write vbtab 
& cm.length
    response.write vbcrlf
%
>    Precision:<%
    response.write vbtab 
& cm.Precision
    response.write vbcrlf
%
>    Scale:<%
    response.write vbtab 
& cm.NumScale
    response.write vbcrlf
%
>    Description:<%
    response.write vbtab 
& cm.Description
    response.write vbcrlf
next 
%
>

generate by cpdedg <%= Now %><%
filepath
= app.path & \test.txt”

set fso = createobject(Scripting.FileSystemObject)
Set ts = fso.CreateTextFile(filepath, True, True)
ts.Write CEG_RESULT
ts.Close

if iif (msgbox (file has saved to & filepath & vbcrlf _
   
& do you want to delete this file?,vbQuestion +vbYesNo )=vbYes ,true,false) then
    fso.DeleteFile filepath ,
True
end if

you must use ’start with all tables’ to fill all tables of schema
b=true
for each tbl in schema.Tables.Items
   
if tbl.columns.count = 0 then
        b
= false
   
end if
next

if b then
   
for each tbl in schema.Tables.Items
        response.write
table name: & vbtab & tbl.name     & vbcrlf
        response.write
column count: & vbtab & tbl.columns.count & vbcrlf
   
next
end if
%
>

然后这是一个生成结果:

This is a example script template.
You can write script
as same as what’s in ASP.

Table:Area
Comment:
Column count:4
========Traverse all columns property=========
column:    Code
    Primary Key:   
True
    Auto Identity:   
False
    DataType:    varchar
    Nullable:   
False
    Length:   
10
    Precision:   
    Scale:   
    Description:    编号
column:    Name
    Primary Key:   
False
    Auto Identity:   
False
    DataType:    varchar
    Nullable:   
False
    Length:   
50
    Precision:   
    Scale:   
    Description:    名称
column:    Remark
    Primary Key:   
False
    Auto Identity:   
False
    DataType:    varchar
    Nullable:   
True
    Length:   
255 Precision: Scale: Description: 说明column: rowguid Primary Key: False
    Auto Identity:   
False
    DataType:    uniqueidentifier
    Nullable:   
False
    Length:   
    Precision:   
    Scale:   
    Description:  

编辑 webmaster

 
 
 
评论更多>>
 
Any-java代码自动生成工具(java模板语言)简介 简单说明: 模板解释性语言,在模板中没有特殊标记的部分,原封不动输出, 标记的部分允许所有的java语法,并解释执行。 用途:代码自动生成 比如:开发完了一个类文件,但还有很多类似的类要做,只是要改变一些个别的地方, 那么,可以把需要改动的地方用java变量标记,然后运行该模板,自动生成多个类。 同时,我们也提供了一些工具类(比如excel读取),则设计书和模板联动,可以大大提高开发速度。 当然,如果没有设计书,也可以直接读取DB的表结构来自动生成代码。 该工具不同与某些java代码自动生成的工具,规定死了模板,只能生成指定的代码, 思路是:把先开发出来的java类,作简单的变量替换,就生成新的模板,从而生成新的java类。 也不只限于java类,任何重复的代码部分都可以做成模板。 起名为“any"的理由 1。简单:只要懂java任何人都可以在2小时内学会。 anyone can use it easily. 2。灵活:任何文件都可以轻易的转化为模板。 anything is template. 3。强大:几乎能实现你所有想自动化的部分,只要你有灵感。anything is real. http://bbs.skystonesoft.com/viewthread.php?tid=1&page=1&extra=
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •