﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-woaidongmao-随笔分类-模板引擎</title><link>http://www.cppblog.com/woaidongmao/category/13173.html</link><description>文章均收录自他人博客，但不喜标题前加-[转贴]，因其丑陋，见谅！~</description><language>zh-cn</language><lastBuildDate>Wed, 03 Mar 2010 05:15:20 GMT</lastBuildDate><pubDate>Wed, 03 Mar 2010 05:15:20 GMT</pubDate><ttl>60</ttl><item><title>利用CodeSmith为SQL Server CE生成项目代码</title><link>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108772.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Wed, 03 Mar 2010 05:14:00 GMT</pubDate><guid>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108772.html</guid><wfw:comment>http://www.cppblog.com/woaidongmao/comments/108772.html</wfw:comment><comments>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108772.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woaidongmao/comments/commentRss/108772.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woaidongmao/services/trackbacks/108772.html</trackback:ping><description><![CDATA[<p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">摘要：<span lang="EN-US">CodeSmith</span>是很多<span lang="EN-US">.NET</span>开发人员至爱的开发辅助工具，它能够使开发人员从大量枯燥无味的重复编码中解脱，集中精力解决实际业务问题和技术问题。本文将介绍如何将<span lang="EN-US">CodeSmith</span>用于<span lang="EN-US">Windows Mobile</span>项目，以<span lang="EN-US">SQL Server Compact Edition</span>数据库作为代码生成的依据生成项目代码。<span lang="EN-US"><br><br></span><strong><span style="font-family: 宋体">什么是代码生成器？</span></strong><span lang="EN-US"><?xml:namespace prefix = o /><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">代码产生器是产生式编程<span lang="EN-US">(Generative Programming)</span>在实际应用中的一种产物。它以系统建模为基础，通过抽象各种系统间的共性与特性，利用代码模板和组件重用技术，自动产生满足客户需求的软件产品。从而降低成本，减少软件推向市场的时间，并且保证更好的产品质量。最终取得规模经济和范围经济的优点。<span lang="EN-US">2004</span>年我还在大学读书的时候曾开发过一个基于模板引擎的代码生成器<span lang="EN-US">——<a href="http://www.cnblogs.com/upto/archive/2005/06/15/RapidTier.html" target="_blank"><span style="color: #56b6e9">RapidTier</span></a></span>，当时这个作品还获得了广东省高校杯软件设计大赛的二等奖。在<span lang="EN-US">.NET</span>的世界里，目前最有名的代码生成器当属<span lang="EN-US">CodeSmith</span>和<span lang="EN-US">MyGeneration</span>。这两款代码生成器都是基于模板引擎的，使用起来方便灵活，而且模板资源非常丰富。<span lang="EN-US"><br><br><strong><span style="font-family: 宋体">CodeSmith </span></strong></span><strong><span style="font-family: 宋体">简介</span></strong><span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image002_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image002_thumb.jpg" width="170" height="55" v:shapes="_x0000_i1025"></a>CodeSmith </span><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">是一个基于模板的代码生成器，你可以利用它来生成任何文本语言的代码。<span lang="EN-US">CodeSmith </span>生成的代码可以通过模板的属性来定制。<span lang="EN-US">CodeSmith </span>模板的语法跟<span lang="EN-US"> ASP.NET </span>非常相似，你可以在模板中使用<span lang="EN-US"> C# </span>或<span lang="EN-US"> VB.NET </span>控制代码的生成。通常，开发人员利用<span lang="EN-US">CodeSmith</span>根据<span lang="EN-US">SQL Server</span>或<span lang="EN-US">Oracle</span>等大型数据库生成各种项目代码（如：存储过程、数据访问层、业务逻辑层、表现层等）。今天将向各位介绍如何利用<span lang="EN-US">CodeSmith</span>为移动数据库<span lang="EN-US">SQL Server Compact Edition</span>生成<span lang="EN-US">Windows Mobile</span>项目的代码。<span lang="EN-US"><br><br></span><strong><span style="font-family: 宋体">配置<span lang="EN-US">SchemaProvider</span></span></strong><span lang="EN-US"> <o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US">CodeSmith</span><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">通过<span lang="EN-US">SchemaProvider</span>来支持各种数据库。在<span lang="EN-US">CodeSmith Professional <?xml:namespace prefix = st1 /><st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">4.1.2</st1:chsdate></span>安装后，默认只支持<span lang="EN-US">SQL Server</span>、<span lang="EN-US">Oracle</span>、<span lang="EN-US">Access</span>、<span lang="EN-US">MySQL</span>等数据库类型。如果需要支持其他类型的数据库，可以到网上搜索相关数据库的<span lang="EN-US">SchemaProvider</span>实现，或者自己手动编写代码实现<span lang="EN-US">ISchemaProvider</span>接口。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">最近，我从网上找到了<span lang="EN-US">SQL Server Compact Edition(3.1)</span>的<span lang="EN-US">SchemaProvider</span>实现，经过代码调整和调试，现在跟大家分享一下。可以从<span lang="EN-US"><a href="http://www.cnblogs.com/Files/upto/SqlCeSchemaProvider.rar"><span style="color: #56b6e9" lang="EN-US"><span lang="EN-US">这里</span></span></a></span>下载到这个<span lang="EN-US">SchemaProvider</span>相关的<span lang="EN-US">DLL</span>，下载后将其解压，并将<span lang="EN-US">SchemaExplorer.SqlCeSchemaProvider.dll</span>和<span lang="EN-US">System.Data.SqlServerCe.dll</span>文件复制到<span lang="EN-US">CodeSmith</span>的<span lang="EN-US">SchemaProvider</span>目录下<span lang="EN-US">(C:\Program Files\CodeSmith\v4.1\SchemaProviders)</span>。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image004_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image004_thumb.jpg" width="430" height="225" v:shapes="_x0000_i1026"></a><br><br></span><strong><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">配置数据源</span></strong><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">启动<span lang="EN-US">CodeSmith</span>，点击左边停靠的<span lang="EN-US">Schema Explorer</span>窗口上方点击<span lang="EN-US">“Manage Data Sources”</span>工具栏按钮，打开<span lang="EN-US">Data Source Manager</span>窗口。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image006_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image006_thumb.jpg" width="276" height="197" v:shapes="_x0000_i1027"></a><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US">Data Source Manager</span><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">窗口打开后，点击<span lang="EN-US">Add</span>按钮添加新的数据源。在<span lang="EN-US">Data Source</span>窗口中，数据源名称<span lang="EN-US">(Name)</span>输入<span lang="EN-US">“Northwind sqlce”</span>，提供程序类型<span lang="EN-US">(Provider Type)</span>选择<span lang="EN-US">SqlCeSchemaProvider</span>，连接语句输入<span lang="EN-US">“data source=c:\Northwind.sdf”</span>，假设你已经将<span lang="EN-US">SQL Server Compact Edition</span>自带的<span lang="EN-US">Northwind.sdf</span>数据库复制到<span lang="EN-US">C</span>盘根目录了。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image008_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image008_thumb.jpg" width="535" height="248" v:shapes="_x0000_i1028"></a><br><br></span><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">你可以通过点击<span lang="EN-US">Test</span>按钮测试一下是否正常连接，然后点击<span lang="EN-US">OK</span>保存数据源，回到<span lang="EN-US">Data Source Manager</span>窗口，现在看到多了一个叫<span lang="EN-US">“Northwind sqlce”</span>的数据源了。<span lang="EN-US"><br><br><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image010_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image010_thumb.jpg" width="543" height="280" v:shapes="_x0000_i1029"></a><br><br>“Northwind sqlce”</span>数据源已经添加成功，点击<span lang="EN-US">Close</span>按钮关闭<span lang="EN-US">Data Source Manager</span>窗口。回到<span lang="EN-US">Schema Explorer</span>窗口，展开<span lang="EN-US">“Northwind sqlce”</span>数据源，浏览各个表的字段和属性。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image012_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image012" border="0" alt="clip_image012" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image012_thumb.jpg" width="254" height="277" v:shapes="_x0000_i1030"></a><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><br></span><strong><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">生成代码</span></strong><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">在<span lang="EN-US">CodeSmith</span>右边停靠的<span lang="EN-US">Template Explorer</span>窗口打开自带的模板<span lang="EN-US">“CodeSmith 4.1 Samples\ActiveSnippets\CSharp\TableProperties.cst”</span>。这个模板可以从一个数据库的表的所有字段生成对应的实体类的所有属性<span lang="EN-US">(Property)</span>定义代码。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; background: #eeeeee; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image013_2.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image013" border="0" alt="clip_image013" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image013_thumb.gif" width="11" height="16" v:shapes="Code_Closed_Image_124500"></a><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image014_2.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image014" border="0" alt="clip_image014" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image014_thumb.gif" width="11" height="16" v:shapes="Code_Open_Image_124500"></a><span style="border-bottom: gray 1pt solid; border-left: gray 1pt solid; padding-bottom: 0cm; padding-left: 0cm; padding-right: 0cm; background: white; border-top: gray 1pt solid; border-right: gray 1pt solid; padding-top: 0cm; mso-border-alt: solid gray .75pt"><span id="Code_Closed_Text_124500">TableProperties.cst</span></span><span style="display: none; mso-hide: all"><br><span id="Code_Open_Text_124500"><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">-- <br>Name: Database Table Properties<br>Author: Paul Welter<br>Description: Create a list of properties from a database table<br>--</span><span style="background: yellow">%&gt;</span><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">@ CodeTemplate Language=</span></span></span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"C#"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> TargetLanguage=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"C#"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Debug=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"False"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Description=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"Create a list of properties from database table."</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;</span><span style="line-height: 150%; display: none; font-family: 宋体; background: yellow; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">%&gt;</span><span style="line-height: 150%; display: none; font-family: 宋体; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">@ </span></span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: blue; font-size: 12pt; mso-hide: all" lang="EN-US">Property</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Name=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"SourceTable"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Type=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"SchemaExplorer.TableSchema"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Category=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"Context"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Description=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"Table that the object is based on."</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;</span><span style="line-height: 150%; display: none; font-family: 宋体; background: yellow; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">%&gt;</span><span style="line-height: 150%; display: none; font-family: 宋体; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">@ Map Name=</span></span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"CSharpAlias"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Src=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"System-CSharpAlias"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"> Description=</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"System to C# Type Map"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;</span><span style="line-height: 150%; display: none; font-family: 宋体; background: yellow; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">%&gt;</span><span style="line-height: 150%; display: none; font-family: 宋体; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">@ Assembly Name=</span></span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"SchemaExplorer"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;</span><span style="line-height: 150%; display: none; font-family: 宋体; background: yellow; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">%&gt;</span><span style="line-height: 150%; display: none; font-family: 宋体; color: black; font-size: 12pt; mso-hide: all" lang="EN-US"><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke">@ Import Namespace=</span></span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: maroon; font-size: 12pt; mso-hide: all" lang="EN-US">"SchemaExplorer"</span><span style="line-height: 150%; display: none; font-family: 宋体; background: whitesmoke; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;</span><span style="line-height: 150%; display: none; font-family: 宋体; background: yellow; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">%&gt;</span><span style="line-height: 150%; display: none; font-family: 宋体; color: black; font-size: 12pt; mso-hide: all" lang="EN-US">&nbsp;<br><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke"> foreach (ColumnSchema column in this.SourceTable.Columns) {&nbsp; </span><span style="background: yellow">%&gt;</span><br>private <span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= CSharpAlias[column.SystemType.FullName] </span><span style="background: yellow">%&gt;</span> _<span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= StringUtil.ToCamelCase(column.Name) </span><span style="background: yellow">%&gt;</span>; <br><br>public <span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= CSharpAlias[column.SystemType.FullName] </span><span style="background: yellow">%&gt;</span>&nbsp;<span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= StringUtil.ToPascalCase(column.Name) </span><span style="background: yellow">%&gt;</span><br>{<br>&nbsp;&nbsp;&nbsp; get { return _<span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= StringUtil.ToCamelCase(column.Name) </span><span style="background: yellow">%&gt;</span>; }<br>&nbsp;&nbsp;&nbsp; set { _<span style="background: yellow">&lt;%</span><span style="background: whitesmoke">= StringUtil.ToCamelCase(column.Name) </span><span style="background: yellow">%&gt;</span> = value; }<br>} <br><br><span style="background: yellow">&lt;%</span><span style="background: whitesmoke"> } </span><span style="background: yellow">%&gt;</span></span></span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">模板被打开后，在<span lang="EN-US">CodeSmith</span>右边停靠的<span lang="EN-US">Properties</span>窗口选择<span lang="EN-US">SourceTable</span>属性，点击旁边的<span lang="EN-US">“...”</span>按钮浏览并选择一个<span lang="EN-US">Northwind.sdf</span>数据库的表。这里我们选择<span lang="EN-US">Categories</span>表，并点击<span lang="EN-US">Select</span>按钮确定。 <span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image016_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image016" border="0" alt="clip_image016" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image016_thumb.jpg" width="397" height="341" v:shapes="_x0000_i1033"></a><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">现在可以按<span lang="EN-US">F5</span>生成代码了，生成的结果如下所示：<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; background: #eeeeee; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image013_4.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image013" border="0" alt="clip_image013" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image013_thumb_1.gif" width="11" height="16" v:shapes="Code_Closed_Image_124931"></a><a href="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image014_4.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image014" border="0" alt="clip_image014" src="http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CodeSmithSQLServerCE_B9EA/clip_image014_thumb_1.gif" width="11" height="16" v:shapes="Code_Open_Image_124931"></a></span><span style="border-bottom: gray 1pt solid; border-left: gray 1pt solid; padding-bottom: 0cm; line-height: 150%; padding-left: 0cm; padding-right: 0cm; display: none; font-family: 宋体; background: white; color: black; font-size: 12pt; border-top: gray 1pt solid; border-right: gray 1pt solid; padding-top: 0cm; mso-border-alt: solid gray .75pt; mso-hide: all"><span id="Code_Closed_Text_124931">生成的代码</span></span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"><br><span id="Code_Open_Text_124931"></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">private</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">int</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _categoryID; <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">public</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">int</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> CategoryID<br>{<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">get</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">return</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _categoryID; }<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">set</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { _categoryID = value; }<br>} <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">private</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">string</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _categoryName; <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">public</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">string</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> CategoryName<br>{<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">get</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">return</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _categoryName; }<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">set</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { _categoryName = value; }<br>} <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">private</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">string</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _description; <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">public</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">&nbsp;</span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">string</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> Description<br>{<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">get</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">return</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _description; }<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">set</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { _description = value; }<br>} <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">private</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> System.Byte[] _picture; <br><br></span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">public</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> System.Byte[] Picture<br>{<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">get</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">return</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> _picture; }<br>&nbsp;&nbsp;&nbsp; </span><span style="line-height: 150%; font-family: 宋体; color: blue; font-size: 12pt" lang="EN-US">set</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US"> { _picture = value; }<br>}</span><o:p></o:p></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">你还可以选择其他的表或其他模板生成代码试一试，体验一下<span lang="EN-US">CodeSmith</span>的强大之处。<span lang="EN-US"><br><br></span><strong><span style="font-family: 宋体">总结</span></strong> <span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">通过上面的介绍和示例演示，相信大家都认同<span lang="EN-US">CodeSmith</span>确实很好很强大。当然，这里只是演示了一个很简单的例子，你可以根据项目的实际需要自己编写模板，按自己的方式去生成项目代码。编写模板最快捷的方法就是基于现有比较类似的模板进行修改。<span lang="EN-US">CodeSmith</span>之所以能够用于<span lang="EN-US">SQL Server Compact Edition</span>数据库的代码生成，除了前面提到的它通过<span lang="EN-US">SchemaProvider</span>支持各种类型的数据库，还有一点很重要的就是<span lang="EN-US">SQL Server Compact Edition</span>支持桌面平台，如果是<span lang="EN-US">SQL Server Mobile</span>就没有办法做到这一点了。<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%; margin: 12pt 0cm 6pt; background: white; word-break: break-all" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: #444444; font-size: 12pt">相关下载：<span lang="EN-US"><a href="http://www.cnblogs.com/Files/upto/SqlCeSchemaProvider.rar">SqlCeSchemaProvider.rar</a><br><br></span>作者：黎波<span lang="EN-US"><br></span>博客：<span lang="EN-US">http://upto.cnblogs.com/<br></span>日期：<st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="29" month="3" year="2008"><span lang="EN-US">2008</span>年<span lang="EN-US">3</span>月<span lang="EN-US">29</span>日</st1:chsdate><span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p>&nbsp;</o:p></span></p><img src ="http://www.cppblog.com/woaidongmao/aggbug/108772.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woaidongmao/" target="_blank">肥仔</a> 2010-03-03 13:14 <a href="http://www.cppblog.com/woaidongmao/archive/2010/03/03/108772.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何用CodeSmith减少代码重复编写</title><link>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108771.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Wed, 03 Mar 2010 05:11:00 GMT</pubDate><guid>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108771.html</guid><wfw:comment>http://www.cppblog.com/woaidongmao/comments/108771.html</wfw:comment><comments>http://www.cppblog.com/woaidongmao/archive/2010/03/03/108771.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woaidongmao/comments/commentRss/108771.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woaidongmao/services/trackbacks/108771.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 上次简单介绍了下CodeSmith，今天做些详细介绍（转），希望可以对大家有帮助： CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器，程序可以自定义模板，从而减少重复编码的劳动量，提高效率。&nbsp;&nbsp;&nbsp;&nbsp; 安装CodeSmith 2.6注册后发现有两个可运行程序CodeSmith Studio.exe和CodeSmith Explorer....&nbsp;&nbsp;<a href='http://www.cppblog.com/woaidongmao/archive/2010/03/03/108771.html'>阅读全文</a><img src ="http://www.cppblog.com/woaidongmao/aggbug/108771.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woaidongmao/" target="_blank">肥仔</a> 2010-03-03 13:11 <a href="http://www.cppblog.com/woaidongmao/archive/2010/03/03/108771.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>