惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - Martin XJ

windows2000 蓝色背景 楚留香mv2 楚留香mv cyberarticle Castle.Facilities.WebserviceIntegration Castle Windsor Wcf Facility的配置及调用IChannel的Close - Martin XJ SharpRush中的跨函数事务实现 - Martin XJ - 博客园 SharpRush中的AOP实现 自己的ORMapping starcraft2 Spring.net的一个小例子 EnterpriseLibrary 数据访问组件windows98不能使用 使用EnterpriseLibrary碰到的问题 好久都没写blog了 MS推荐的命名指南 Sybase DataWindow.net 1.5(Beta) For .Net 发布 IEWebcontrol webctrl_client目录配置 mywallop、orkut、gmail的邀请 无法打开Web项目
CodeSmith使用心得
Martin XJ · 2004-12-01 · via 博客园 - Martin XJ
 

代码生成器大家都用过吧,CodeSmith我想大家也不陌生。最近就用CodeSmith生成了自己的实体类。当然你也可以用它来生成HTML页面以及文档。

下面我就来说一说我生成实体类的步骤:
一、首先介绍一下CodeSmith 2.6CodeSmith 2.6安装后,会有3exe:
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,举个例子:

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

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运行模板  

总结:

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