看nettiers整理出来的开发codesmith模板技术要点

原创 2006年05月18日 11:45:00
stepwin原创,转载请注明来自于www.softboss.com原创

//模板的基础知识

//任何模板都需要的第一句,用来指定模板编辑语言是什么,目标语言是什么:
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>

//接下来写模板需要从外界引入的参数
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database" %>

//在模板里面用到了codesmith的函数和方法,需要引入对应的包,一般是
<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

//所有codesmith函数都在script标签里面定义,包括变量
<script runat="template">
private string _outputDirectory = String.Empty;

</script>

调用用<% %> 括起来,一般有一个主函数来执行整个模板的函数
<%
this.Go();
%>




//一般模板的函数和用法


/// <summary>
/// 拷贝指定文件
/// </summary>
public void SafeCopyFile(string path, string destination)
{
FileInfo file1 = new FileInfo(path);
file1.CopyTo(destination, true);
}

/// <summary>
/// 创建指定目录
/// </summary>
public void SafeCreateDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}


/// <summary>
/// 根据指定模板生成指定文件
/// </summary>
public void RenderToFile(string templateName, string path, bool overwrite)
{
this._CurrentFileName = path;
this.GetTemplate(templateName).RenderToFile(path, overwrite);
this._counter++;
}


/// <summary>
/// 打开文件目录,[Editor]标签表示调用指定的编辑器;category表示参数所属目录;Descript表示对参数的描述;defaultvalue表示缺省值
CodeTemplateProperty表示该参数是可选还是必须的,CodeTemplatePropertyOption.Optional是可选,CodeTemplatePropertyOption.Required是必


/// </summary>
private string _outputDirectory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
{
get
{
if (_outputDirectory.Length == 0)
{
return @"c:/NetTiers/" + (SourceDatabase != null ? SourceDatabase.Name : "Output");
}
else
{
return _outputDirectory;
}
}
set
{
if (value.EndsWith("//")) value = value.Substring(0, value.Length - 1);
_outputDirectory = value;
}
}


//获取当前打开模板所在路径
this.CodeTemplateInfo.DirectoryName


//对于模板,当前打开的模板可以用codeTemplateInfo访问,其他的子模板需要先根据文件名和路径载入,然后编辑模板,最后赋予参数,生成文件。

//设定模板路径
private string[] _templatesFileNames = new string[] {


"vsnet2003.project.cst",


"vsnet2005.project.cst",


"vsnet2003.solution.cst",


"vsnet2005.solution.cst",
"nAnt.cst",


"AssemblyInfo.cst",







"Entities//Enum.cst",


"Entities//IEntity.cst",


"Entities//Filter.cst",


"Entities//EntityHelper.cst",


"Entities//EntityPropertyComparer.cst",


"Entities//GenericTypeConverter.cst",



"Entities//EntityBase.cst",


"Entities//EntityData.cst",


"Entities//Entity.cst",


"Entities//EntityCollectionBase.cst",


"Entities//EntityCollection.cst",



"Entities//Views//EntityViewBase.cst",


"Entities//Views//EntityView.cst",


"Entities//Views//EntityViewCollectionBase.cst",


"Entities//Views//EntityViewCollection.cst",



"DataAccessLayer//App.config.cst",


"DataAccessLayer//cachingConfiguration.config.cst",


"DataAccessLayer//dataconfiguration.config.cst",


"DataAccessLayer//nettiersconfigdata.config.cst",



"DataAccessLayer//App.config.2005.cst",


"DataAccessLayer//cachingConfiguration.config.2005.cst",


"DataAccessLayer//dataconfiguration.config.2005.cst",


"DataAccessLayer//nettiersconfigdata.config.2005.cst",



"DataAccessLayer//Configuration.cst",








"DataAccessLayer//DataRepository.cst",


"DataAccessLayer//Utility.cst",


"DataAccessLayer//TransactionManager.cst",







"DataAccessLayer//Bases//DataProviderBase.cst",



"DataAccessLayer//Bases//EntityProviderBase.cst",




"DataAccessLayer//Bases//Views//EntityViewProviderBase.cst",











"DataAccessLayer.SqlClient//SqlDataRepository.cst",


"DataAccessLayer.SqlClient//SqlDataProvider.cst",


"DataAccessLayer.SqlClient//SqlEntityProviderBase.cst",


"DataAccessLayer.SqlClient//SqlEntityProvider.cst",



"DataAccessLayer.SqlClient//StoredProcedureProvider.cst",


"DataAccessLayer.SqlClient//StoredProceduresXml.cst",





"DataAccessLayer.SqlClient//Views//SqlEntityViewProviderBase.cst",


"DataAccessLayer.SqlClient//Views//SqlEntityViewProvider.cst",



"DataAccessLayer.WebService//WebService.cst",


"DataAccessLayer.WebService//WebInfo.cst",



"DataAccessLayer.WebServiceClient//WsDataProvider.cst",


"DataAccessLayer.WebServiceClient//WsEntityProvider.cst",


"DataAccessLayer.WebServiceClient//WsEntityProviderBase.cst",


"DataAccessLayer.WebServiceClient//Views//WsEntityViewProvider.cst",


"DataAccessLayer.WebServiceClient//Views//WsEntityViewProviderBase.cst",






"UnitTests//EntityRepositoryTest.cst",


"UnitTests//EntityViewRepositoryTest.cst",



"ASP.Net//2.0//AdminEntityUC_Designer.cst",


"ASP.Net//2.0//AdminEntityUC_CodeBehind.cst",



"ASP.Net//2.0//Entty_aspx.cst",


"ASP.Net//2.0//Entty_aspx_cs.cst",


"ASP.Net//2.0//Entty_aspx_resx.cst",
};

//设定编辑好的子模板保存的hashtable,在hashtable里面,key是文件名,所以全套模板不能有重复的文件名
// Compile and load all them in a collection
private System.Collections.Hashtable _CodeTemplates = new System.Collections.Hashtable();

//载入模板
// load all the templates and put them into an hashtable
public void LoadTemplates()
{
foreach(string _templatesFileName in _templatesFileNames)
{
string key = System.IO.Path.GetFileName(_templatesFileName);

if (_CodeTemplates.Contains(key))
{
continue;
}

_CodeTemplates.Add(key, this.CompileTemplate(this.CodeTemplateInfo.DirectoryName + _templatesFileName));

// Set the properties that all the commonsqlcode inherited templates should set
// TODO : use reflection to check that the templates inherits from commonsql
try
{
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("EntityFormat", EntityFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("CollectionFormat",

CollectionFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("ProviderFormat", ProviderFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("InterfaceFormat", InterfaceFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("BaseClassFormat", BaseClassFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("EnumFormat", EnumFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("ManyToManyFormat",

ManyToManyFormat);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("AliasFilePath", AliasFilePath);
((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("StrippedTablePrefixes",

StrippedTablePrefixes);
}
catch(Exception) {}
}
}


//载入的时候都需要编译模板
public CodeTemplate CompileTemplate(string templateName)
{
this._CurrentFileName = templateName;

CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateName);
compiler.Compile();

if (compiler.Errors.Count == 0)
{
return compiler.CreateInstance();
}
else
{
for (int i = 0; i < compiler.Errors.Count; i++)
{
Response.WriteLine(compiler.Errors[i].ToString());
}
return null;
}
}

//获取模板
public CodeTemplate GetTemplate(string templateType)
{
return (CodeSmith.Engine.CodeTemplate)_CodeTemplates[templateType];
}


//给模板赋值
this.GetTemplate("EntityProviderBase.cst").SetProperty("SourceTable", SourceTable);



//数据库属性
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Optional="False" Category="DataSource"

Description="Database that the stored procedures should be based on." %>


//遍历数据库里面的所有数据表
private TableSchemaCollection _sourceTables;
private ViewSchemaCollection _sourceViews;
for (int i=0; i < SourceDatabase.Tables.Count; i++)
{
_sourceTables.Add(SourceDatabase.Tables[i]);
}

//遍历数据库里面的所有视图
for (int i=0; i < SourceDatabase.Views.Count; i++)
{
_sourceViews.Add(SourceDatabase.Views[i]);
}

//获取表格名
SourceTables[i].Name

//遍历表的结构
for (int i=0; i< SourceTable.Columns.Count; i++) {
Response.Write("/""+ SourceTable.Columns[i].Name + "/""); }

//指定打开哪个或者哪几个表
[Category("DataSource")]
[Description("The tables to generate.")]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
public TableSchemaCollection SourceTables
{
get
{
if (this._sourceTables != null && this._sourceTables.Count > 0 )
return this._sourceTables;
else
return null;
}
set
{
this._sourceTables = value;
}
}


我在网上找了半天,发现codesmith遍历文件夹和拷贝文件夹的函数,我知道能够引入system.io这个包,然后自己写这个函数出来,哪位达人写过这样的函数或者有更好的办法可以发过来共享啊

相关文章推荐

一个技术小白整理出来的for循环使用几种方法~~

循环篇: 常见的for循环语法如下: for(初始值;判断条件;迭代变量){ 执行语句······ } 如: 这是常见的写法,今天我们来试试下面一种奇葩写法, 答案如下: A ...

整理出来的android(安卓)开发框架

第一个:Afinal 第二个:KJFrameForAndroid框架 第三个:thinkAndroid 第四个:AndroidAnnotations 第五个:volley...

C++知识要点整理08:类模板

1.类模板    对于功能相同而数据类型不同的一些函数,不必一一定义各个函数,可以定义一个可对任何类型变量进行操作的函数模板,在调用函数时,系统会根据实参的类型,取代函数模板中的类型参数,得到具体的...
  • jukai7
  • jukai7
  • 2013年03月27日 08:18
  • 724

webkit webApp 开发技术要点总结

  • 2013年05月30日 21:46
  • 288KB
  • 下载

CodeSmith Module层 模板

VS 2005 模式: using System; using System.Collections.Generic; using System.Text; namespace MODE...

fd快捷键 整理出来的

  • 2011年11月10日 11:55
  • 12KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:看nettiers整理出来的开发codesmith模板技术要点
举报原因:
原因补充:

(最多只允许输入30个字)