体验 CodeSmith
听说 CodeSmith 的大名好久了,但一直都没机会来研究这东西。总算这星期一时心血来潮,花了点时间看了看他的 QuickStart 教程,里面的示例比较简单,对于入门者很适合(一开始就复杂无比的东西,谁还有耐心看下去是不是?^_^)
不用不知道,用了吓一跳(乖乖,这东西确实挺不错的),CodeSmith可以生成C#,VB.NET,TSQL以及其他任何语言代码,而且可以编程灵活控制(它的编程很简单啊,都是.NET语法,编码的模式类似ASP,代码和静态内容混写)
下面是看了 QuickStart 后写的一个小玩意,功能是把给定的数据表中的所有列都检索出来生成一个实体类:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" LinePragmas="True" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
Category="DataSource"
Description="选择一个数据表" %>
<%@ Property Name="Namespace" Type="String"
Category="Main"
Description="输出文件使用的命名空间" %>
<%@ Property Name="DeveloperName" Type="String"
Category="Main"
Description="作者" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
//***********************************************************************
// Created: <%= DateTime.Now.ToShortDateString() %> Author: <%= DeveloperName %>
// File: <%= CodeTemplate.GetFileName() %>
// Description: <%= SourceTable.Name %> 自定义实体类, 使用 CodeSmith 自动生成.
//***********************************************************************
using System;
namespace <%= Namespace %>
{
/// <summary>
/// <%= SourceTable.Name %> class description here.
/// </summary>
public class <%= SourceTable.Name %>
{
#region Property
<% for (int i=0; i<SourceTable.Columns.Count; i++) %>
<% { %>
<% string colName = SourceTable.Columns[i].Name.ToString(); %>
<% string colType = SourceTable.Columns[i].SystemType.ToString(); %>
private <%= colType %> _<%= colName %>;
public <%= colType %> <%= colName %>
{
get
{
return _<%= colName %>;
}
set
{
_<%= colName %> = value;
}
}
<% } %>
#endregion
#region Constructure
public <%= SourceTable.Name %>()
{
//
// TODO: <%= SourceTable.Name %> constructure logic
//
}
#endregion
#region Method
public void Insert()
{
}
public void Save()
{
}
public void Update()
{
}
public void Delete()
{
}
#endregion
}
}
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
Category="DataSource"
Description="选择一个数据表" %>
<%@ Property Name="Namespace" Type="String"
Category="Main"
Description="输出文件使用的命名空间" %>
<%@ Property Name="DeveloperName" Type="String"
Category="Main"
Description="作者" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
//***********************************************************************
// Created: <%= DateTime.Now.ToShortDateString() %> Author: <%= DeveloperName %>
// File: <%= CodeTemplate.GetFileName() %>
// Description: <%= SourceTable.Name %> 自定义实体类, 使用 CodeSmith 自动生成.
//***********************************************************************
using System;
namespace <%= Namespace %>
{
/// <summary>
/// <%= SourceTable.Name %> class description here.
/// </summary>
public class <%= SourceTable.Name %>
{
#region Property
<% for (int i=0; i<SourceTable.Columns.Count; i++) %>
<% { %>
<% string colName = SourceTable.Columns[i].Name.ToString(); %>
<% string colType = SourceTable.Columns[i].SystemType.ToString(); %>
private <%= colType %> _<%= colName %>;
public <%= colType %> <%= colName %>
{
get
{
return _<%= colName %>;
}
set
{
_<%= colName %> = value;
}
}
<% } %>
#endregion
#region Constructure
public <%= SourceTable.Name %>()
{
//
// TODO: <%= SourceTable.Name %> constructure logic
//
}
#endregion
#region Method
public void Insert()
{
}
public void Save()
{
}
public void Update()
{
}
public void Delete()
{
}
#endregion
}
}
总体来说,CodeSmith还是非常不错的,以后再慢慢仔细研究!
CodeSmith 3.0 破解版
下载地址:http://www.zzmine.com/down/CodeSmith3.0.rar