上例介绍了使用CodeSmith编写代码模板的基本方法,本例实现一个较为实用的代码模板,通过数据库自动为Yii Framework生成所需要的ActiveRecord 类。
本例通过修改Yii Framework 开发教程(26) 数据库-Active Record示例,原例是手工编写Employee.php ActiveRecord。
首先为工程添加一个C#项目(任意类型,我们只是利用这个项目来包含CodeSmith项目),然后添加一个CodeSmith项目和一个CodeSmith模板。然后参考CodeSmith 使用教程(1): 概述使用Schema Explorer 添加一个数据连接,本例连接到Chinook数据库:
创建的代码模板PhpActiveRecord.cst 定义个属性TableName(数据库表名),复制Yii Framework 开发教程(26) 数据库-Active Record示例中Employee.php的定义并使用属性,代码如下:
<%@ Template Language="C#" TargetLanguage="PHP" Debug="False" %>
<%@ Property Name="TableName" Type="System.String" Description="Table name" %>
<?php
class <%= TableName %> extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '<%= TableName %>';
}
}
?>
<script runat="template">
public override string GetFileName() {
return TableName + ".php" ;
}
</script>
这时就可以通过定义TableName 的属性给任意数据表生成对应的ActiveRecord PHP类了。 不过这还是要手工来一个一个来配置表名。 本例通过一个主模板和一个从模板的方式通过连接数据库自动为所有的表生成对应的ActiveRecord使用主从模板的具体用法后面再介绍,简单的说子模板相当于子函数,主模板类似于主函数可以调用子函数,主模板通过调用子模板,传给子模板属性从而可以生成多个文件。创建一个代码模板YiiDataModel.cst 作为主模板,使用子模板首先需要在主模板中进行注册才能使用:
<%@ Register Name="ActiveRecord" Template="PhpActiveRecord.cst" MergeProperties="false" %>
完整代码如下:
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
Description="List all database tables" %>
<%@ Import Namespace="System.IO" %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
Category="Context" Description="Database containing the tables." %>
<%@ Register Name="ActiveRecord" Template="PhpActiveRecord.cst"
MergeProperties="false" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
public string FirstLetterToUpper(string str)
{
if (str != null)
{
if(str.Length > 1)
return char.ToUpper(str[0]) + str.Substring(1);
else
return str.ToUpper();
}
return str;
}
</script>
<% for (int i = 0; i < SourceDatabase.Tables.Count; i++) { %>
<% string name= FirstLetterToUpper(SourceDatabase.Tables[i].Name); %>
<% string filename= @"../ActiveRecordDemo/protected/models/"+name+".php"; %>
// instantiate the sub-template
<% ActiveRecord activeRecord = this.Create<ActiveRecord>();%>
<% activeRecord.TableName= name; %>
<% activeRecord.RenderToFile(filename,true); %>
<% } %>
FirstLetterToUpper 为C#函数,主要是把数据库表名的第一个字母变为大写(纯C#代码)。 SchemaExplorer 为CodeSmith提供的数据库访问库,可以用来获取数据库Schema的信息,如包含的表名,字段属性,主键外键等(后面具体介绍)
在主模板中,通过ActiveRecord 来访问子模板(名字ActiveRecord为注册子模板时定义) ,使用this.create创建子模板实例,然后传入TableName属性,调用RenderToFile 将子模板的结果写道指定的文件中。
此时在CodeSmith.csp 中添加主模板,配置数据库为Chinook,然后生成代码
Rendering output 'YiiDataModel'...
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Album.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Artist.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Customer.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Employee.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Genre.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Invoice.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Invoiceline.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Mediatype.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Playlist.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Playlisttrack.php
Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Track.php
Generated: D:\tmp\ActiveRecordDemo\CodeSmith\YiiDataModel.txt
Done rendering outputs: 1 succeeded, 0 failed, 0 skipped (1111ms).
刷新项目可以看到自动生成的代码文件
本例只是为每个数据表生成最简单的ActiveRecord,如果需要生成关联ActiveRecord,可以进一步根据表之间的关系为每个ActiveRecord 生成所需的relations 方法,后面有时间进一步介绍。
本例下载
分享到:
相关推荐
前言11第 1 章第 1 章概述概述44第 2 章第 2 章编写第一个代码模板编写第一个代码模板11第 3 章第 3 章自动生成Yii Framework Ac
CodeSmith 生成工具 6.5 (可生成Nhibernate)。内含key。
CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf ...
CodeSmith .net 2.0 自动生成三层模板
CodeSmith使用教程,开发资料,PDF高清淅版,对CodeSmith熟悉的朋友都知道,CodeSmith是一个基于模板的代码生成工具,它使用类似于ASP.NET的语法来生成代码或文本,本教程将带你掌握CodeSmith的使用方法和原理等...
CodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdf
内部包含两个学习文件:CodeSmith.docx和CodeSmith使用手册及实例.pdf
使用CodeSmith进行简单的属性配置,即可批量生成表的创建脚本(sqlserver)
CodeSmith自动生成实体类,数据访问层/.................
codesmith 3层代码生成模板 codesmith 3层代码生成模板 codesmith 3层代码生成模板
CodeSmith自动生成三层代码,方便快捷,节约时间
ASP.NET 中 CodeSmith可以自动生成三层代码 接下来还有生成三层代码的模板
CodeSmith使用基础教程 非常详细的网页教程
codesmith编程完整教程 所用工具: CodeSmith Professional v5.1.3.8510,代码示例全部是以C#为例。
codesmith 代码模板,帮助开发生成代码使用
1、Oledb 包括使用access数据库时三层代码的自动生成。 2、SQL 包括使用Sql Server数据库时三层代码的自动生成。 3、Procdure 包含一个根据表生成存储过程的模板 4、Model 包含一个一次生成所有Model中实体类的模板...
CodeSmith 是一种基于模板的代码生成工具,它使用类似于 ASP.NET 的语法来生成任意类型的代码或文本。与其他许多代码生成工具不同,CodeSmith 不要求您订阅特定的应用程序设计或体系结构。使用 CodeSmith,可以生成...
使用办法: 找到CodeSmith的SchemaProviders目录,替换文件,即可。压缩包中也有.cs文件 如果打开失败重新选择一下驱动就可以了
CodeSmith安装包已经模板代码和使用教程,原版程序+注册机+教程(包含图文教程),视频教程,三层模板的通用代码生成模板
CodeSmith 基础教程 word版,比较简单