woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

如何用CodeSmith减少代码重复编写

上次简单介绍了下CodeSmith,今天做些详细介绍(转),希望可以对大家有帮助:

CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。
    
安装CodeSmith 2.6注册后发现有两个可运行程序CodeSmith Studio.exeCodeSmith Explorer.exe
     CodeSmith Studio.exe
用来创建自定义模板

     CodeSmith Explorer.exe
用来导入模板并且生成代码
   
打开 CodeSmith Studio.exe,新建一个C#模板。发现有如下类似与asp.net的标识符号
     <%  %>
     <%= %>
     <%@  %>
    <script runat="template"> </script>
  
下面通过简单的例子说明如何用  CodeSmith 创建模板并生成代码
   
新建一个空的txt文件,在文件上部输入如下一个CodeTemplate指示,
LanguageTargetLanguage分别代表模板语言和创建代码语言,

clip_image001<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a simple class " %>
clip_image001

然后声明几个变量,用来为了能够在以后生成的代码嵌入,这里声明了三个名为NameSpace,ClassName,Contxt的变量。其他参数一目了然就不再说明了,

clip_image001<%@ Property Name="NameSpace" Type="String"
clip_image001      Category="Context"
clip_image001      Description="The namespace to use for this class" %>
clip_image001<%@ Property Name="ClassName" Type="String"
clip_image001      Category="Context"
clip_image001      Description="The name of the class to generate" %>
clip_image001<%@ Property Name="DevelopersName" Type="String"
clip_image001      Category="Context"
clip_image001      Description="The name to include in the comment header" %>

接下来建立将要生成代码的框架,在适当位置引用刚刚声明的变量名

clip_image001using System;
clip_image001namespace <%=NameSpace %>
clip_image002{
clip_image003      /// <summary>
clip_image004     
/// Summary description for <%=ClassName %>.
clip_image005     
/// </summary>
clip_image004
      public class <%=ClassName %>
clip_image003      {
clip_image004            public <%=ClassName %>()
clip_image003           {
clip_image005            }

clip_image005      }

clip_image006}

最后,打开CodeSmith Explorer.exe,加载此模板,并且在属性对话框中任意更改声明的变量名,按Generate按钮生成合适的代码
clip_image007

  以上简单的说明了CodeSmith的功能
 
下面看看在数据库访问中它是如何最小化我们的工作的
 
在这里我们要用到CodeSmith API中一个叫SchemaExplorer的组件,它提供了一系列类来操作数据库的框架,我们可以用它来创建表和存储过程,得到字段类型,字段名等。
 
如下是更新NorthWind数据库中orders表记录的存储过程,我们来看看如何自动生成它

clip_image001CREATE PROCEDURE dbo.UpdateOrders
clip_image001      @OrderID int,
clip_image001      @CustomerID nchar(5),
clip_image001      @EmployeeID int,
clip_image001      @OrderDate datetime,
clip_image001      @RequiredDate datetime,
clip_image001      @ShippedDate datetime,
clip_image001      @ShipVia int,
clip_image001      @Freight money,
clip_image001      @ShipName nvarchar(40),
clip_image001      @ShipAddress nvarchar(60),
clip_image001      @ShipCity nvarchar(15),
clip_image001      @ShipRegion nvarchar(15),
clip_image001      @ShipPostalCode nvarchar(10),
clip_image001      @ShipCountry nvarchar(15)
clip_image001AS
clip_image001 
clip_image001UPDATE [Orders] SET
clip_image001      [CustomerID] = @CustomerID,
clip_image001      [EmployeeID] = @EmployeeID,
clip_image001      [OrderDate] = @OrderDate,
clip_image001      [RequiredDate] = @RequiredDate,
clip_image001      [ShippedDate] = @ShippedDate,
clip_image001      [ShipVia] = @ShipVia,
clip_image001      [Freight] = @Freight,
clip_image001      [ShipName] = @ShipName,
clip_image001      [ShipAddress] = @ShipAddress,
clip_image001      [ShipCity] = @ShipCity,
clip_image001      [ShipRegion] = @ShipRegion,
clip_image001      [ShipPostalCode] = @ShipPostalCode,
clip_image001      [ShipCountry] = @ShipCountry
clip_image001WHERE
clip_image001     
clip_image001      [OrderID] = @OrderID
clip_image001

  第一步还是创建一个CodeTemplate指示,注意TargetLanguage属性改为了T-SQL,因为创建的SQL语言代码

clip_image001<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>

 然后加载SchemaExplorer组件,并导入SchemaExplorer命名空间,看这里是不是和asp.net很像

clip_image001<%@ Assembly Name="SchemaExplorer" %>
clip_image001<%@ Import Namespace="SchemaExplorer" %>

接下来声明变量,因为要从数据库表中读取框架所以 Type 属性为SchemaExplorer.TableSchema

clip_image001<%@ Property   Name="SourceTable"   Type="SchemaExplorer.TableSchema"    Category="Context"   Description="Table that the stored procedures should be based on." %>
clip_image001

 下面就要写实际将要输出代码部分的模板了。
 
先写存储过程的第一行 ,<%%>内为引用前面声明变量名,代表表名

clip_image001CREATE PROCEDURE dbo.Update<%=SourceTable.Name %>

 第二步为存储过程创造将要声明的参数列表

clip_image001 //开始循环遍历每列 
clip_image002
    <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
clip_image004    //
传递每列给GetSqlParameterStatement函数 
clip_image004
    <%= GetSqlParameterStatement(SourceTable.Columns[i]) %>
clip_image004    //
循环结束条件
clip_image003
    <% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
clip_image006    <% }
% >
clip_image001     AS

定义GetSqlParameterStatement方法接受列参数返回参数名,和字段类型,注意函数要放在
<script runat="template"></script>

clip_image001<script runat="template">
clip_image001
clip_image001public string GetSqlParameterStatement(ColumnSchema column)
clip_image001
clip_image002{
clip_image004      string param = "@" + column.Name + " " + column.NativeType;
clip_image004      switch (column.DataType)
clip_image003      {
clip_image004            case DbType.Decimal:
clip_image003           {   
clip_image004             param += "(" + column.Precision + ", " + column.Scale + ")";
clip_image004             break;
clip_image005           }

clip_image004            default:
clip_image003         {
clip_image004               if (column.Size > 0)
clip_image003            {
     
                param += "(" + column.Size + ")";
clip_image005               }

clip_image004             break;
clip_image005           }

clip_image005       }

clip_image004     return param;
clip_image006}

clip_image001</script>
clip_image001

接下来创建存储过程的Update部分,语法类似不再说明

clip_image001UPDATE [<%= SourceTable.Name %>] SET
      <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++)
{ %>
      [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
      <% }
%>
clip_image001//where
条件,注意主键提取方法
clip_image001
WHERE
      <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++)
{ %>
      <% if (i > 0) { %>AND <% } %>
      [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
      <% }
%>
clip_image001

最终为模板为如下形式

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
      Description="Generates a update stored procedure." %>
 
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
      Category="Context"
      Description="Table that the stored procedures should be based on." %>
 
<%@ Assembly Name="SchemaExplorer" %>
 
<%@ Import Namespace="SchemaExplorer" %>
     
<script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
{
      string param = "@" + column.Name + " " + column.NativeType;
 
      switch (column.DataType)
      {
            case DbType.Decimal:
            {
                  param += "(" + column.Precision + ", " + column.Scale + ")";
                  break;
            }
            default:
            {
                  if (column.Size > 0)
                  {
                        param += "(" + column.Size + ")";
                  }
                  break;
            }
      }
 
      return param;
}
</script>
 
CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
      <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
      <%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
      <% } %>
AS
 
UPDATE [<%= SourceTable.Name %>] SET
      <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
      [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
      <% } %>
WHERE
      <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
      <% if (i > 0) { %>AND <% } %>
      [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
      <% } %>

将其用CodeSmith Explorer.exe打开
clip_image008
可以任意选择数据库中的表,这里选择NorthWind中的0rders
clip_image009
然后生成代码,就创建好了存储过程

clip_image001CREATE PROCEDURE dbo.UpdateOrders
clip_image001      @OrderID int,
clip_image001      @CustomerID nchar(5),
clip_image001      @EmployeeID int,
clip_image001      @OrderDate datetime,
clip_image001      @RequiredDate datetime,
clip_image001      @ShippedDate datetime,
clip_image001      @ShipVia int,
clip_image001      @Freight money,
clip_image001      @ShipName nvarchar(40),
clip_image001      @ShipAddress nvarchar(60),
clip_image001      @ShipCity nvarchar(15),
clip_image001      @ShipRegion nvarchar(15),
clip_image001      @ShipPostalCode nvarchar(10),
clip_image001      @ShipCountry nvarchar(15)
clip_image001AS
clip_image001 
clip_image001UPDATE [Orders] SET
clip_image001      [CustomerID] = @CustomerID,
clip_image001      [EmployeeID] = @EmployeeID,
clip_image001      [OrderDate] = @OrderDate,
clip_image001      [RequiredDate] = @RequiredDate,
clip_image001      [ShippedDate] = @ShippedDate,
clip_image001      [ShipVia] = @ShipVia,
clip_image001      [Freight] = @Freight,
clip_image001      [ShipName] = @ShipName,
clip_image001      [ShipAddress] = @ShipAddress,
clip_image001      [ShipCity] = @ShipCity,
clip_image001      [ShipRegion] = @ShipRegion,
clip_image001      [ShipPostalCode] = @ShipPostalCode,
clip_image001      [ShipCountry] = @ShipCountry
clip_image001WHERE
clip_image001     
clip_image001      [OrderID] = @OrderID
clip_image001

同样,我们可以选择其他的表创建Updata存储过程,也可以自定义其他诸如insert delete等的存储过程,是不是很方便?^-^
以上只是CodeSmith的简单应用,还有其他的功能有待大家探索了。

关于CodeSmith社区一些模板资源,大家可以去如下地址选择下载
CodeSmith Peer Support Forum
Codesmith templates library

 

posted on 2010-03-03 13:11 肥仔 阅读(2150) 评论(0)  编辑 收藏 引用 所属分类: 模板引擎


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理