现在的位置: 首页 > 综合 > 正文

CodeSmith使用体验和心得

2013年12月01日 ⁄ 综合 ⁄ 共 5893字 ⁄ 字号 评论关闭
CodeSmith相关文章:
CodeSmith使用体验和心得
Struts+Spring+Hibernate练习(完整)
struts+spring+hibernate之间的关系与差别(转)
史上最简单的struts+spring+hibernate配置实例 修订版
配置Springframework与hibernate连接多数据库的事务
Spring+Hibernate连接Mysql的(Tomcat)配置
JDO,EJB/CMP,Hibernate和Amber比较(翻译附原文)
最全的C/C++面试题集(C/C++试题和部分答案)
最全的C/C++面试题集(最全的C/C++试题集和答案)(续)

对于代码生成工具的迷恋好象从使用IBuySpy开发asp.net项目起就开始了。用过QuickCode,也自己写过Cool Coder,但是却一直没有使用曾被推荐为《每个开发人员现在应该下载的十种必备工具 》的CodeSmith,不能说不是遗憾,今天就看看它有什么特别之处。

相对于其他代码生成器而言,CodeSmith的最大的特点就是它有自己的编程语言,可以自己开发各种模版,特别是它对于基于数据库的代码开发,也是吸引我的主要原因,不过今天我们先看个简单的例子,看看它是如何来生成代码的。

程序代码 程序代码
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Description="Generates a class including a special informational header" %>

<%@ Property Name="NameSpace" Type="String"
Category="Context"
Description="The namespace to use for this class" %>

<%@ Property Name="ClassName" Type="String"
Category="Context"
Description="The name of the class to generate" %>

<%@ Property Name="DevelopersName" Type="String"
Category="Context"
Description="The name to include in the comment header" %>

///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright © <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %> <%= DevelopersName%> original Version
///////////////////////////////////////////////////////////////////////////////////////

using System;

namespace <%=NameSpace %>
{
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName %>
{
public <%=ClassName %>()
{
//
// TODO: Add constructor logic here
//
}
}
}

看着上面的代码,是不是感觉很熟悉?是的,它的语法很像asp.net的语法,这样,大家就不需要花费太多时间来学习了。

其生成结果如下:

程序代码 程序代码
/**////////////////////////////////////////////////////////////////////////////////////////
// File: Hello.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright c 2005 Our Client
// ---------------------
// History
// 2005-1-18 Tim Wang original Version
/**////////////////////////////////////////////////////////////////////////////////////////

using System;

namespace ASPCOOL
{
/**//// <summary>
/// Summary description for Hello.
/// </summary>
public class Hello
{
public Hello()
{
//
// TODO: Add constructor logic here
//
}
}
}

CodeSmith官方介绍:
CodeSmith is a template-based code generator that can produce code for any text-based language. Whether your target language is C#, Visual Basic .NET, T-SQL, Java or even FORTRAN, CodeSmith can help you produce higher-quality, more consistent code in less time than writing code by hand. CodeSmith's familiar ASP.NET-based template syntax means that you can be writing your first templates within minutes of installing the package. The advanced CodeSmith Studio integrated development environment (IDE) helps you create and test new templates in a rapid development setting. You can also join in CodeSmith's active online community to download hundreds of ready-made templates for such common development tasks as building strongly-type collection classes or creating data access layers.

CodeSmith also includes a console version that you can easily integrate into your automated build process, flexible strategies for merging generated code with custom code, the SchemaExplorer API for integration with relational data sources, and the ability to hook up your own custom metadata sources.

CodeSmith使用心得

最近就用CodeSmith生成了自己的实体类。当然你也可以用它来生成HTML页面以及文档。
下面我就来说一说我生成实体类的步骤:
一、首先介绍一下CodeSmith 2.6CodeSmith 2.6安装后,会有3个exe:
1. CodeSmith.exe即为CodeSmith Explorer,可视化生成代码工具,免费,没有时间限制
2. CodeSmithConsole.exe 在Command模式下运行,免费,没有时间限制
3. CodeSmithStudio.exe编辑模板的工具的工具,可以校验模板语法是否正确。试用30天。
CodeSmith模板支持C#、VB.Net语法,可以调用.net类库,语法和.net基本上是一样的。

二、制作模板
1.在这里我选择了C#作为模板的开发语言。
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Template description here." %>
2.要生成数据库的实体类,数据连接和表名不可缺少的。在CodeSmith中通过SchemaExplorer.dll来帮你和数据库打交道。
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
在模板中导入SchemaExplorer。
然后设置一系列的需要传入的参数:
<%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="数据库连接." %>
<%@ Property Name="TableName" Type="System.String" Default="" Optional="False" Category="" Description="表名" %>

3.CodeSmith模板脚本格式:
<script runat="template">
CodeTemplateRule rule=new CodeTemplateRule();
</script>

或者:
/// <summary>
/// 作用:<%= Description %>
/// 作者:<%= Author %>
/// 日期:<%= DateTime.Now.ToString() %>
///</summary>

4.我自己写了一个dll来存放自己的函数 CodeTemplateRule.dll ,其中引用到了SchemaExplorer,举个例子:

程序代码 程序代码
public ColumnSchemaCollection GetColumnCollection(DatabaseSchema dataBase,string tableName)
{
TableSchemaCollection tables = new TableSchemaCollection(dataBase.Tables);
ColumnSchemaCollection columns=null;
for(int i=0;i<tables.Count;i++)
{
if(tables[i].Name.ToUpper()==tableName.ToUpper())
{
TableSchema ts=tables[i];
columns=new ColumnSchemaCollection(ts.Columns);
}
}
return columns;
}

这段代码的含义就是取数据库中某张表所有列的集合。

见Demo文件:
模板文件:entity.cst
自己写的.net程序集:CodeTemplateRule.cs
生成后的代码效果:AccountBookEntity.cs

三、运行
1. 用CodeSmith.exe运行模板,CodeSmith会弹出对话框来你来填写你的参数。
2. 用CodeSmithConsole.exe运行模板,参数可以放在xml文件中。例如:

程序代码 程序代码
<?xml version="1.0" encoding="utf-8" ?>
<codeSmith>
<propertySets>
<propertySet>
<property name="SampleStringProperty1">string111111111</property>
<property name="SampleBooleanProperty1">false</property>
</propertySet>
</propertySets>
</codeSmith>

然后用命令执行:

3. 在CodeSmithStudio.exe运行模板

CodeSmith template模版资源:
在对codeSmith初步了解
以后,对于要还要编程才能生产代码实在有点失望(飞鹰的懒惰程度有此可见了)。幸好,网上有许多现成的模版可供下载,现搜集如下:
CodeSmith Tempalate share 论坛
Codesmith templates library
DotNet DAL Library

总结:
代码生成器CodeSmith给我们编程工作带来了很大的便利,不需要做很多重复性的工作。

 

抱歉!评论已关闭.