认识CodeSmith

一:下载与激活

1.下载绿色版CodeSmith7.0

http://download.csdn.net/download/laoge/6859701

2.使用激活工具CodesmithKeyGenerator.exe激活CodeSmith7.0

(a)打开软件TemplateEditor.exe,进入CodeSmith Generator窗口,点击Register按钮;

(b)先打开激活工具CodesmithKeyGenerator.exe,进入CodeSmith KeyGenerator窗口,在Prefix里面输入:CS70P-    ,其他的保持默认,点击Generate按钮,产生serial序列号;

(c)复制serial中假的序列号到CodeSmith Generator窗口中serial number下,在CodeSmith Generator窗口填写name;

(d)点击Register按钮,点击Actiave by Entering a Code按钮,记住Machine Code中的序列号;

(e)切换到CodeSmith KeyGenerator窗口,点击Generate Activation按钮;

(f)在Prefix里面输入CS70P-  ,code里面选择时间(大于现在时间就行),手动输入Machine Code中的序列号到Machine Hash Code文本框里面,点击Generate按钮,产生真正的Activate序列号;

(g)把真正的Activate序列号复制到Activation Code文本框里面,点击Activate激活CodeSmith;

(h)运行TemplateEditor.exe,点击“Help—>About CodeSmith Generator”,如果Licensed to下显示你在(c)步输入的name,表示激活成功了。

二:CodeSmith的工作原理

①去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等;

②根据用户自定义好的模板文件,用数据库结构中的关键字替代模板的动态变量;

③输出并保存为我们需要的目标文件。

在CodeSmith中你可以自己定义模板来生成所有你想要的代码,也可以到CodeSmith社区http://community.codesmithtools.com/中去下载模板。

三:实例——使用CodeSmith生成分层架构的实体层

具体参照:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html

1.运行TemplateEditor.exe,新建CSharp Template取名为test.cst,添加代码(声明模板使用哪种语言,使CodeSmit和数据库关联起来);

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Tempalte description here." %>

<%-- 加载访问数据库的组件SchemaExplorer,并声明其使用的命名空间 --%>
<%@ Assembly Name="SchemaExplorer"%>
<%@ Import Namespace="SchemaExplorer"%>

<%-- 数据库 --%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. GettingStarted - Required" Description="Database that the tables views, 
and storedprocedures should be based on. IMPORTANT!!! If SourceTables and SourceViews areleft blank, the Entire Database will then be generated."%>

2.新建一个文件Entity.cst,保存到test.cst所在的文件夹内,编写设计模板代码;

<%@ CodeTemplate Inherits="CodeTemplate" TargetLanguage="Text" Description="NetTiers main template."Debug="True" ResponseEncoding="UTF-8"%>

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

<%-- 要打印的表 --%>
<%@ Property Name="Table" Type="TableSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, 
and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Entity
{
    public partial class <%= Table.Name%>
    {
        <%foreach(ColumnSchema col in Table.Columns){ %>
        public <%= col.DataType %> <%=col.Name %>{ get;set; }
        <% } %>
    }
}

3.模板创建好后,要在test.cst文件中注册一下;


<%-- 注册实体层Entity模板 --%>
<%@ Register Name="EntityTemplate" Template=" Entity.cst"MergeProperties="Flase" ExcludeProperties=""%>

4.在test.cst中设置目标文件的输出目录;

<script runat="template">
    //解决方案输出路径
private string Directory = String.Empty;
    
    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
    [Optional, NotChecked]
    [DefaultValue("")]
    public string OutputDirectory 
    { 
        get
        {
            return Directory;
        }
        set
        {
            if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);
            Directory = value;
        } 
    }
</script>

5.在test.cst中写些函数来完成将数据库架构传递给模板的工作;

<script runat="template">
    //生成实体Entity类
private void GenerateEntityClasses()
    {
        CodeTemplate Template =new EntityTemplate();
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs";
            //生成模板
            Template.SetProperty("Table",table);
            //文件输出
            Template.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory +" 创建成功.");
        }
    }
</script>

6.在test.cst中调用刚刚写好的函数;


<%
//创建实体层Entity类
this.GenerateEntityClasses();

Debug.WriteLine("OK");
%>

7.设置我们要导出的数据库和输出目录;

a.点击CodeSmith主窗体右上角Schema Explorer面板,点击图标添加要连接的数据库,填写三个参数:

Name(要连接的数据库名字),Provider Type(sql server数据库选择SqlSchemaProvider),Connection String(可点击后面的按钮,填写数据库所在服务器名字或ip、登录方式和数据库名)

测试连接成功后,创建数据库成功。

b.点击CodeSmith主窗体右下角Properities面板中SourceDatabase属性栏右侧的…按钮,弹出数据库设置对话框,选择创建好的test数据库;

c.点击CodeSmith主窗体右下角Properities面板中OutputDirectory属性栏右侧的…按钮,选择目标文件输出目录。

 

8.回到test.cst页面,点击Generate自动生成代码成功后,可到输出目录中查看到所有表的实体类文件。

注:也可以在entity.cst模板文件中选择某个table单独生成实体类。

 

更多CodeSmith知识参照:http://terrylee.cnblogs.com/archive/2005/12/28/306254.aspx



posted @ 2015-04-17 21:21  梦可园  阅读(897)  评论(0编辑  收藏  举报